views:

299

answers:

3

I got the impression that if we use persistent fields, there is no need for getter methods since the entity manager references the instance variables directly. However, when I removed the getter and setter methods from an entity to have persistent fields, the values for the corresponding instance variable was not retrieved from the database!

Does that mean we must have getter and setter methods even though we have persistent fields?

A: 

You don't mention what JPA implementation you are using. Hibernate certainly allows you to access fields directly. However, it is not the default setting, so you have to specify this behavior in the mapping files with access=field. (with annotation based configuration, I would imagine you just need to annotate the fields directly ...)

alasdairg
A: 

What provider is that? I'd expect it to work. Anyway, just create protected getter and setter methods as a workaround if your provider needs that.

candiru
I am using oracle toplink.
abracadabra
+2  A: 

If the entity class uses persistence, fields Persistence accesses the entity class instance variables directly at runtime.

While on persistence property, there is a getter and a setter method for each property.

What you said should have worked on hibernate.

A link about it

Diego Dias