Maifee Ul Asad
Maifee Ul Asad

Reputation: 4607

Foreign key problem in Spring Boot - h2 database

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 ?

Upvotes: 0

Views: 1771

Answers (1)

Andronicus
Andronicus

Reputation: 26046

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.

Upvotes: 1

Related Questions