问题
In my spring boot application, I have User class something like this :
public class User {
@Id @GeneratedValue Long userID;
@OneToOne(fetch = FetchType.LAZY,targetEntity = LoginCredential.class)
@JoinColumn(name = "userID",referencedColumnName = "userID")
private LoginCredential loginCredential;
}
And another class LoginCreadential like this :
public class LoginCredential {
@Id @GeneratedValue Long userID;
@OneToOne(mappedBy = "user", fetch = FetchType.LAZY)
User user;
}
My application was running fine before I tried to add these relations. Now it doesn't run. It gives me error (a lot), but the important portion is here :
org.hibernate.AnnotationException: Unknown mappedBy in: com.mua.cse616.Model.LoginCredential.user, referenced property unknown: com.mua.cse616.Model.User.user
What is the error here? How this can be resolved ?
回答1:
It's because mappedBy must have a value which is the name of the field that contains mapping between these entities.
In your example this should be mappedBy = "loginCredential", because @OneToOne containing mappedBy annotates User. User on the other hand defines mapping between those entities using @JoinColumn(name = "userID",referencedColumnName = "userID") over loginCredential field, hence the value of mappedBy.
来源:https://stackoverflow.com/questions/57694576/foreign-key-problem-in-spring-boot-h2-database