Originally posted at http://stackoverflow.com/questions/21379405/jpa-nested-composite-pk
I'm trying to configure the JPA classes for the following PK requirements:
A Writer class / table with a PK of [Long id], for example [1], [2], [3]...
A Book class / table with a composite PK of [Writer Long id and Long index] (thus each writer has his own book indexes), for example [1, 1], [1, 2], [1, 3] (three books for writer #1), [2, 1], [2, 2], [2, 3], [2, 4] (four books for writer #2)...
A Page class / table with a composite PK of [Writer Long id, Book Long index and Long number] (thus each book has its own page numbers), for example [1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 1, 4], [1, 1, 5] (five pages for writer #1 and his book index #1)...
What I need from a Book instance is to be able to call book.getWriter() and get a Writer instance; also, page.getBook() would return a Book instance. Of course, ALL three classes could easily have a simple Long PK and then, for example, a book would have the Writer as a not-nullable, @ManyToOne association outside the PK, but it doesn't suite our needs.
I've exhausted any configuration using @Embedded(|Id), @Embeddable, @IdClass, @MapsId, etc. I could think of or Googled, but Hibernate would always complain one way or the other, especially about the Page class where things are getting “nested”. Ideally, a later Paragraph class / table with a composite PK of [Writer Long id, Book Long index, Page Long number and Long paragraph index] should be configured likewise.
I hope someone has any good hints about this.
Thank you!
|