wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hibernate can access private fields and methods ? How come ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate can access private fields and methods ? How come ?" Watch "Hibernate can access private fields and methods ? How come ?" New topic
Author

Hibernate can access private fields and methods ? How come ?

Gundum Hoi
Ranch Hand

Joined: Nov 27, 2005
Posts: 34
I need some clarification for the text in the documentation.
Below is extracted from �Hibernate Reference Documentation Version: 3.1.3� Section 1.2.1 :

[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.

[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 ?

[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 ?

Thanks a lot.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Basically Hibernate uses Reflection which can gain access to private instance vairables as well as private methods.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Gundum Hoi
Ranch Hand

Joined: Nov 27, 2005
Posts: 34
Thanks for the reply...

Hibernate documentation :

The choice is up to you and you can match it to fit your application design.


What kind of choices is the documentation talking about here ?

Thanks.
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
What the documentation is referring to is your choice as to whether to make the identity setter method public or private.

Hibernate will set the identity only when the object is saved and it can do that even if the identity setter is private because it's using reflection.

You may choose to make the identity setter private so that the rest of your application doesn't try to update the identity.

Hibernate can handle it either way.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

To add to Scott's response. You can set the ID generator to be set to "assigne" which means your application logic will define the ID, which means you will need the application code to call setId() which you would then want set to public. But if you want to make sure that your application doesn't do anything nasty like change the Primary Key that is already set in the database. This can cause identity problems and duplicate rows being entered. And even if you were to assign it in code say with the value 10, and you have a Sequence Table which is currently at 5. Then 5 inserts later, using the sequence number would try to insert a new record with ID 10, which already exists and throw an Exception. By keeping the setId() method to private you protect against this.

Mark
Gundum Hoi
Ranch Hand

Joined: Nov 27, 2005
Posts: 34
Thanks for the helpful replies.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hibernate can access private fields and methods ? How come ?
 
Similar Threads
Annotations on instance declaration or get method using JPA in entities?
How does hibernate use private methods ?
Architectural Suggestion Needed
inheritence and the use of private & protected memebrs
cloning