A bean proxy is a stand-in for a bean.
What are the major uses of proxies?
1. They may be used if you do not have the class for the bean available
It might well be that you do not have the class for a given bean available. This is no problem since a you may simply fetch the proxy and use that.
2. They may be used when fetching is a very expensive operation
A partial fetch will resolve basic types (e.g. strings, dates, etc.) but will not resolve any beans. Beans are represented as proxies and will have their contents only loaded at the first access to a property.
3. They are maps
Proxies implement Java's Map<String, Object> interface. The keys are the names of the properties.
Operations with proxies
To fetch a proxy from a session, simply invoke fetchBeanProxy(Resource subject). A specific call for fetching is required since Java does not allow overloading of return types. All other calls such as save, update, refetch etc. work without change.
Setting a property requires that you use the put(String key, Object value) method on the proxy. The key, again, is the case-sensitive name of the property. The value may be any standard type or a proxy.
To get the value of the property invoke get(String key) which will return a standard type or proxy.
Directly creating a proxy
Since proxies are maps, you may create them and populate them directly.
BeanProxy book = new BeanProxy(); book.put("title", "Anti-disestablishmentarianism in early post-Georgian Sussex"); book.put("price", 42); BeanProxy author = new BeanProxy(); author.put("firstName", "Leonard"); author.put("lastName", "Skinner"); book.put("author", author);
You can see that property values are put into the book proxy and in the author proxy.
At this point though, these are just. In order to use one with a bean session and get the benefits of automatic serialization, you must supply its state and register it with your session.
Session state and proxies.
Proxies have session information with them. If you fetch a proxy, this is automatically set for you.
- getSubject gets the RDF subject for this object
- getType returns the RDF type for this this object (from the mapping)
- getBeanMapping returns the bean mapping active for this proxy
If you have created a proxy, you need to set the subject and mapping. A list of all known properties is available from
getPropertyNames
Now you may use the bean session APIs against them as if they were beans.