views:

48

answers:

2

According to my JPA 2.0 book (and online documentation), I should be able to mix field and property access within a single entity or entity hierarchy. The annotation of @Access on the class specifies the default access. When placed on a field or property getter @Access can specify that the default should be overridden for this field.

@Entity
@Access(AccessType.FIELD)
Class Foo {

  @Id
  int id;

  @Column(name = "myfield")
  String myField;

  @Column(name = "myProp")
  @Access(AccessType.PROPERTY)
  public int getMyProp () {
    return 3;
  }

  public void setMyProp (int p) {
    // do nothing
  }
}

This class should result in a table with three columns. However it doesn't with Hibernate...the "myProp" column is missing from the table because apparently Hibernate takes its field vs property cue from the entity ID and runs with it...totally ignoring the JPA spec with regards to @Access.

Can anyone confirm this or did I make a stupid mistake somewhere?

+1  A: 

I've seen similar (not the same but similar) issues like HHH-5004 so I wouldn't exclude that this might be a new one (the TCK doesn't seem exhaustive). But what version of Hibernate are you using? Did you try with the latest?

Pascal Thivent
Hibernate 3.5.3-FINAL -- the latest release as of June 17th. I saw HHH-5004 and while it is not about the issue at hand, it certainly makes me think that "@Access" is not use at all by the Hibernate community. Think I should open a bug over there?
HDave
@HDave: This is what I would do (I usually double check the behavior with another implementation, e.g. EclipseLink) and then mention the relevant part of the spec and the fact that it works with the RI. A test case is often welcome and helps at getting things fixed faster.
Pascal Thivent
+1  A: 

Based on the docs your code seems to be right. The @Access(AccessType.FIELD) annotation on top is unnecessary, because you annotated the field int id; This tells hibernate to use field access. I tried a very similar example with annotations and xml config mixed. This leads to the same behaviour, so it's probably a bug in hibernate.

I tried with hibernate 3.5.3

ambassador86