mappedBy reference an unknown target entity property - hibernate error mappedBy reference an unknown target entity property - hibernate error spring spring

mappedBy reference an unknown target entity property - hibernate error


By default, when you define your entity you can either use field-based or property-based access, but not both. "Access type" basically means where your JPA provider looks to determine the state of your entity. If field access is used, it looks at the instance variables. If property access is used, it looks at the getters.

In your case, you didn't explicitly define an access type, so JPA tries to figure it out by looking at where you placed your annotations. I think Hibernate decides based on the placement of the @Id annotation. Since your @Id annotation is placed on a getter, Hibernate is using property-based access for RolesMap.

With property-based access, you do not have a property named rUser because you don't have a getter named getRUser().

The spec states that you should not mix your placement of annotations like that:

All such classes in the entity hierarchy whose access type is defaulted in this way must be consistent in their placement of annotations on either fields or properties, such that a single, consistent default access type applies within the hierarchy

What I suggest doing to solve the problem:

Place your annotations consistently so that there is no ambiguity (e.g. always put your annotations on the instance variables). This would result in the following changes to RolesMap:

@Entity@Table(name = "roles_map")public class RolesMap {    @Id    @Column(name = "RM_ID", unique = true, nullable = false)    private int rm_id;    @Column(name = "USERNAME_A", unique = true)    private String username_a;    @Column(name = "USERNAME_L", unique = true)    private String username_l;    @Column(name = "PASSWORD", unique = true, nullable = false)    private String password;    @Column(name = "ROLE_ID", unique = true, nullable = false)    private int role_id;    @ManyToOne    @JoinColumn(name="username_u", nullable=false)    private User rUser;    // ... constructor(s), getters/setters, etc ...}