JPA-OneToOne外键作为主键 [英] JPA - OneToOne Foreign key as primary key
本文介绍了JPA-OneToOne外键作为主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表,该表需要将其主键用作其他表的外键,因此是单向一对一关系.每本书只有一位作者,像这样:
I have a table which requires its primary key as a foreign key to other table, so an unidirectional one-to-one relationship. There will be only an author per book, like this:
@Entity
public class Author {
@Id
String code;
//getters and setters ...
}
@Entity
public class Book {
@Id
@OneToOne
@JoinColumn(name="code", columnDefinition="DOM_COD5")
Author author;
//getters and setters
}
使用普通的Hibernate和JPA批注可以很好地工作,但是通过Spring Data使用它,我一直收到此错误:
With plain Hibernate and JPA annotations it works well, but using using it through Spring Data I keep getting this error:
Caused by: java.lang.IllegalArgumentException: This class [class com.xxxx.api.catalogo.domain.Book] does not define an IdClass
推荐答案
尝试这种方式
@Entity
public class Book {
@Id
@OneToOne
@PrimaryKeyJoinColumn
Author author;
...
}
这篇关于JPA-OneToOne外键作为主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文