JPA - OneToOne外键作为主键

时间:2016-02-15 12:40:30

标签: hibernate jpa spring-data spring-data-jpa

我有一个表,它需要将其主键作为其他表的外键,因此是一对一的单向关系。每本书只有一位作者,如下:

@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使用它我一直收到这个错误:

Caused by: java.lang.IllegalArgumentException: This class [class com.xxxx.api.catalogo.domain.Book] does not define an IdClass  

1 个答案:

答案 0 :(得分:0)

尝试这种方式

@Entity
public class Book {

    @Id
    @OneToOne
    @PrimaryKeyJoinColumn
    Author author;

    ...
}