Spring Boot中的外键问题-H2数据库 [英] Foreign key problem in Spring Boot - h2 database

查看:338
本文介绍了Spring Boot中的外键问题-H2数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的spring boot应用程序中,我有User这样的类:

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;
}

和另一个类LoginCreadential一样:

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:未知,映射于:com.mua.cse616.Model.LoginCredential.user,引用属性未知:com.mua.cse616.Model.User.user

这是什么错误?该如何解决?

What is the error here? How this can be resolved ?

推荐答案

这是因为mappedBy必须具有一个值,该值是包含这些实体之间的映射的字段的名称.

It's because mappedBy must have a value which is the name of the field that contains mapping between these entities.

在您的示例中,该值应为mappedBy = "loginCredential",因为包含mappedBy@OneToOne注释了User.另一方面,User使用loginCredential字段上的@JoinColumn(name = "userID",referencedColumnName = "userID")定义了这些实体之间的映射,因此定义了mappedBy的值.

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.

这篇关于Spring Boot中的外键问题-H2数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆