Direct field access in hibernate

B

brian.oconnor101

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
 
B

Bart Cremers

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
 
A

Adam Maass

Bart Cremers said:
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).

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
 
O

Oliver Wong

Adam Maass said:
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
 
D

Darryl L. Pierce

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.

--
Darryl L. Pierce <[email protected]>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"

*** ***
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top