JPA - OneToOne Foreign key as primary key

℡╲_俬逩灬. 提交于 2020-01-23 03:45:26

问题


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
}

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  

回答1:


Try this way

@Entity
public class Book {

    @Id
    @OneToOne
    @PrimaryKeyJoinColumn
    Author author;

    ...
}


来源:https://stackoverflow.com/questions/35409656/jpa-onetoone-foreign-key-as-primary-key

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