0

I am asking because I would like to use code generation for the getters/setter.

And also because I would prefer the mapping annotations to appear at the top of the class, where I declare the fields.

I wonder if this approach is correct:

@Entity
@Table(name = "test")
// @Access(AccessType.PROPERTY) // cannot use this, because hibernate complains that no 
public class Test implements Serializable
{
  @Id
  // I hope this results in property access for all of the other
  // properties as well, but I am not sure how to confirm this...
  @Access(AccessType.PROPERTY) 
  @Column(name = "id")
  private long                id         = 0;

  @Column(name = "test_string")
  private String              testString = null;

  ...

Update: I just tested the above example and it looks like the 'testString' property is accessed using field access. I added logging statements to the getter/setter and they were never called. On the other hand, when I added @Access(AccessType.PROPERTY) also to the 'testString' field, the getter and setter methods were called.

So at the moment it looks like my only option is to write "@Access(AccessType.PROPERTY)" in front of every field :-(

Reto Höhener
  • 5,419
  • 4
  • 39
  • 79

1 Answers1

0

1) Property access isn't the right way to persist entities. You persist state, and state is stored in fields.

2) You can use the @Access annotation to set the access type to use for a class.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • 1) there seem to be pros and cons, see e.g http://stackoverflow.com/questions/594597/hibernate-annotations-which-is-better-field-or-property-access/2869468#2869468. 2) I cannot use @Access to annotate the class because then hibernate complains "No identifier specified for entity". – Reto Höhener Feb 16 '13 at 22:41
  • 1) I'm aware of the arguments for property access. My stated opinion is that they're wrong because they're ignoring the basic fact that persistence is about state. 2) Maybe you'd have to annotate every getter individually to get property access, then. [The docs](http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e535) aren't very specific in this area. – Ryan Stewart Feb 16 '13 at 23:57
  • 2) yes, that's what I have done up to now. But it spreads the hibernate annotations all over the class. Not to mention that I had to tell the code generator to stop generating the getters and maintain them myself... – Reto Höhener Feb 17 '13 at 00:01