Hibernate getId loads an object, even if it's lazy

I remember that there is an annotation in JPA or in sleep mode to tell hibernate to use the getId / setId method instead of the property (we will annotate our properties). Without this setting, getId leads to getting into the database and filling out all the fields of this object, which I don’t want. Does anyone know what an annotation is?

Example:

public void Project { @Id //Other annotation forcing hibernate to use property get/settter public Long id; } public Ticket { @ManyToOne(lazy=true) public Project project; } 

Thus, ticket.getProject.getId () causes the database to hit in order to get the project when the identifier is already in the proxy object of the hibernate project. Annotations will fix what I remember.

thanks dean

+7
source share
1 answer

You need to tell Hibernate to access the ID using property access rather than field access:

 @Id @Access(AccessType.PROPERTY) private Long id; 

You really shouldn't post your fields.

+12
source

All Articles