hbnusr wrote:
[Explaining about the id field of an object/class]… we usually don't manipulate the identity of an object, hence the setter method should be private. Only Hibernate will assign identifiers when an object is saved. You can see that Hibernate can access public, private, and protected accessor methods, as well as (public, private, protected) fields directly. The choice is up to you and you can match it to fit your application design.
The best practice in Hibernate is to use technical ids. So, you should want to hide the associated getter getId() and setter setId() to the outside world : you set them private and it could be only accessed via particular mechanism (i.e. reflection, see below).
That's how Hibernate will access the id property.
hbnusr wrote:
[Only Hibernate will assign identifiers when an object is saved.] – Assign identifiers by inserting directly into the DB id field and not assign to the field in the Java object instance ?
No, it will also set the entity id with the generated one.
hbnusr wrote:
[You can see that Hibernate can access public, private, and protected accessor methods, as well as (public, private, protected) fields directly.] – I don’t understand how a hibernate class can access the private methods and fields in my persistence pojo class. I thought NO class outside of a class could access private fields and methods ? Isn’t this the purpose of the keyword “private” ? To prevent outside access ?
Basically yes. The visibility keywords are designed for this. But, this isolation level can be bypassed by using something in Java which is called Reflection. This function is not specific to Hibernate, the Java language lets you manipulate and analyze the content of a class. For example, you can retrieve very simply all the methods of a given class, display return types, even the private ones, etc..
These functions can be found in the java.lang.reflect => This kind of programming is called Reflection.
You can see some examples here for example :
http://www.onjava.com/pub/a/onjava/2003 ... ction.html
So Hibernate just uses the Java API and some Helper classes to do it (typically PropertyAccessor, and so on).
OK ?