keule wrote:
I think you have to supply a getter and a setter method for every attribute Hibernate has to persist.
But I think you can write in your getter and setter method whatever you want.
Why not make something like this:
Code:
public class Person{
public final static String AGE = "age";
protected HashMap attributes = new HashMap();
public void setAge(Integer years) {
this.setAttribute(AGE, years);
}
public void setAttribute(String attribute, Object value) {
attributes.put(attribute, value);
}
}
This way you have both ways to access your "fields". Getter methods should work analoguely.
Thank you, Keule. You are right !!! This solution works, but I have the same value twice (one in HashMap and the other in the age attribute) and have to synchronize the HashMap with the POJO attributes. If Hibernate use direct access to the fields (access="field") the synchronization is lost.
I could solve the redudance problem writing a big If-else block in the setAttribute method, but it is not my objective.
Suppose that I have 80 POJOS, each one with 150 properties. I would prefer to use hibernate class and methods to get and set properties into the POJOS . I think that hibernate uses the
java.lang.reflect package to invoke getters and setters methods, but "HOW" is the question . If I could also use the java.lang.reflect, but I don't know if hibernate will notice the changes.
I really need this solution, because I am at the beginning of a big project and I have to consider the best solution using best practices with minimum writng of code, with the best performance and best use of resources.
I would really appreciate other comments to this topic.
Thank you for your help !!!
Best regards,
Joao Araújo