-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Hibernate can access private fields and methods ?
PostPosted: Wed Jun 28, 2006 1:36 pm 
Newbie

Joined: Wed Jun 28, 2006 1:03 am
Posts: 14
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.


Top
 Profile  
 
 Post subject: Re: Hibernate can access private fields and methods ?
PostPosted: Wed Jun 28, 2006 2:08 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
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 ?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject: What kind of choices is the documentation talking abt here ?
PostPosted: Wed Jun 28, 2006 10:18 pm 
Newbie

Joined: Wed Jun 28, 2006 1:03 am
Posts: 14
Thanks for the reply...

Hibernate documentation :

Quote:
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 3:28 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
It means that you can choose the visibility you want for your classes. You don't have to set everything public because Hibernate should have access to it.

So, this means that you can design your classes (your object models) without breaking it from an outside point of view for Hibernate (adding an id property, for example).

Imagine a Person class which has this definition
Code:
public class Person
{
   String name;
   String firstName;
   public String getFirstName(){ return firstName; }
   public void setFirstName(String firstName){this.firstName = firstName;}
   public String getName() {return name;}
   public void setName(String name){this.name = name;}
}


For Hibernate, you should add an id, but you can do it entirely private, so it doesn't show to outside world, although Hibernate could access it without problem :
Code:
public class Person
{
   String name;
   String firstName;
   public String getFirstName(){ return firstName; }
   public void setFirstName(String firstName){   this.firstName = firstName; }
   public String getName() {return name;}
   public void setName(String name){this.name = name;}
   
   String id;
   private String getId()
   {
      return id;
   }
   private void setId(String id)
   {
      this.id = id;
   }
}


Clearer ?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.