Foreign key problem in Spring Boot - h2 database

我是研究僧i 提交于 2020-01-16 10:02:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!