I have Hibernate Entities that look something like this (getters and setters left out):
@Entity
public class EntityA {
@ManyToOne(fetch = FetchType.LAZY)
As I understand that call should NOT make a roundtrip to the database, as the Id is stored in the EntityA table, and the proxy should only return that value.
Use property access type. The behavior you're experiencing is a "limitation" of field access type. Here is how Emmanuel Bernard explained it:
That is unfortunate but expected. That's one of the limitations of field level access. Basically we have no way to know that getId() indeed only go and access the id field. So we need to load the entire object to be safe.
So change your code into:
@Entity
public class EntityA {
private EntityB parent;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
public EntityB getParent() {
return parent;
}
...
}
@MappedSuperclass
public class SuperEntity {
private long itemId;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
public long getItemId() {
return itemId;
}
...
}