Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Hibernate can access private fields and methods ? How come ?

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically Hibernate uses Reflection which can gain access to private instance vairables as well as private methods.

Mark
 
Gundum Hoi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the helpful replies.
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic