Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Direct field access in hibernate

Thread view: 
brian.oconnor101@gmail.com - 10 May 2006 11:29 GMT
I have heard that fields can be set directly using hibernate without
the need for getters or setters.

Would anyone have an example of this please as I dont seem to have any
luck finding information on it on the web.

Thanks,
Brian
Bart Cremers - 10 May 2006 11:39 GMT
class User {
   private long id;
   private String name;

   public long getId() {
       return id;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }
}

<class name="User" table="USERS">
   <id name="id" column="ID" type="long" access="field">
       <generator class="increment" />
   </id>
   <property name="name" column="USER_NAME" not-null="true"
unique="true" />
</class>

As you can see the ID field has no setter. By setting the 'access'
method to 'field' you can tell hibernate to work directly on the field
instead of using the get/set method. The getter method can also be
removed of course, but most of the time doesn't make sense to remove
the getter in real life code (except when a field is only to be used in
internal calculations).

Regards,

Bart
Adam Maass - 11 May 2006 05:52 GMT
> class User {
>    private long id;
[quoted text clipped - 27 lines]
> the getter in real life code (except when a field is only to be used in
> internal calculations).

There is, of course, the train of thought that most classes should not
expose getters. Instead, they should expose methods that do internally what
is necessary.

When using Hibernate, I prefer the direct field access style. It makes the
persistence-capable classes so much more natural.

-- Adam Maass
Oliver Wong - 11 May 2006 19:08 GMT
> When using Hibernate, I prefer the direct field access style. It makes the
> persistence-capable classes so much more natural.

   Hibernate persists the states of objects.

   By using direct field access, you'd be persisting the
implementation-state, as opposed to the publicly visible apparent state, of
the object.

   Sometimes that's what you want, but sometimes it isn't.

   - Oliver
Darryl L. Pierce - 12 May 2006 16:26 GMT
> I have heard that fields can be set directly using hibernate without
> the need for getters or setters.
>
> Would anyone have an example of this please as I dont seem to have any
> luck finding information on it on the web.

With Hibernate Annotations, annotating the field rather than the mutator
methods implies field access.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.