3

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  
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Computist
  • 145
  • 1
  • 8
  • 1
    One book can have multiple authors, but that's not the point. And as the error says, where is your primary-key column. – We are Borg Feb 15 '16 at 12:52
  • You mean with Hibernate API it "works" and with JPA API it throws an exception because the JPA API insists that you have certain conditions met. Spring is bugger all to do with anything here – Neil Stockton Feb 15 '16 at 14:13
  • I tried the same trio of annotations with bidirectional one-to-one in Spring Data to get the same result. Separate ID field and @MapsId on author wouldn't break application context from loading, but it still fails for me with an entirely different error. Maybe it could work for you though. – Vlasec Feb 24 '16 at 10:56

1 Answers1

2

Try this way

@Entity
public class Book {

    @Id
    @OneToOne
    @PrimaryKeyJoinColumn
    Author author;

    ...
}
dasunse
  • 2,839
  • 1
  • 14
  • 32