JPA/hibernate OneToOne relation to a Composite key entity target using source primary key
I have the following mapping with a n to n table with composite key:
@Entity
class Reader {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id:Long?=null
@OneToMany(mappedBy="reader")
val readings=mutableListOf()
@OneToOne
var bestReading:Reading=...
}
@Entity
class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id:Long?=null
@OneToMany(mappedBy="book")
val readings=mutableListOf()
}
@Entity
@IdClass(Reading.Id::class)
class Reading(
@Id
@ManyToOne
val book:Book,
@Id
@ManyToOne
val reader:Reader
) {
data class Id(val book:Book,val reader:Reader) : Serializable
var comment:String=""
}
I want the 'bestReading' relation of Reader to use just the primary key of reader (id) and a book_id field which is both the primary key of the Book and the book_id of the Reading. Is it possible and how?