My recommendation is to provide getters and setter for each field. There is also the possibility to specify a custom strategy by setting the "access" attribute of a <property> tag to the name of a class that implements
org.hibernate.property.PropertyAccessor. I have case which implements a Map-like setter/getter.
In the class I have the following:
Code:
public String get(String name)
{
return map.get(name);
}
public void set(String name, String value)
{
map.put(name, value);
}
In my hbm.xml file:
Code:
<property
name="first"
column="first"
type="string"
access="my.PropertyAccessor"
/>
<property
name="second"
column="second"
type="string"
access="MyPropertyAccessor"
/>
Implementing a PropertyAccessor is not very difficult. It is only used to create a Getter and a Setter object for each property. You need to provide Getter and Setter implementations also.
Code:
public class MyPropertyAccessor
{
public Getter getGetter(Class class, String property)
{
return new MyGetter(property);
}
// ... and a similar getSetter()
}
public class MyGetter
{
private String property;
public MyGetter(String property)
{
this.property = property;
}
public Object get(Object target)
{
return ((MyObject)target).get(property);
}
// ... and some more methods
}
// ... and a similar MySetter class
The MyPropertyAccess.getGetter() and MyPropertyAccessor.getSetter() will be called once for each <property> in the hbm.xml file when Hibernate starts up. After that Hibernate will use the MyGetter.get() and MySetter.set() to get and set values.
Check the Hibernate API documentation (javadoc) for more information.