Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA - OneToOne Foreign key as primary key

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  
like image 503
Computist Avatar asked Dec 28 '25 16:12

Computist


1 Answers

Try this way

@Entity
public class Book {

    @Id
    @OneToOne
    @PrimaryKeyJoinColumn
    Author author;

    ...
}
like image 135
dasunse Avatar answered Dec 31 '25 08:12

dasunse