Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORM: OneToOne mapping on Non Primary-Key Join column - Book and Inventory mapped by ISBN

I have a Book model and Inventory model mapped by ISBN number, but ISBN is not the primary key in either. Books belong to Bookstores and Inventory is for a group of Bookstores(BookstoreChain). Inventory is shared by all Bookstores belonging to a BookstoreChain.

I'm using Hibernate @OneToOne mapping on the book side to fetch inventory info by joining the ISBN column. Somehow, Hibernate generates the left outer join query correctly, but inventory is null on the Book object. Its not lazy loaded either. Ignoring the Bookstore and Chain, how do i do a OneToOne or ManyToOne join and fetch inventory when Books are fetched?

class Book{
@Id
Long id

@Column
String isbn;

@Column
String title;

@OneToOne(optional = true)
@JoinColumn(name = "isbn", referencedColumnName = "isbn",insertable = false, updatable = false)
Inventory inventory;
}

class Inventory{
@Id
Long id

@Column
String chainId

@Column
String isbn

@Column
Long availableQty
}
like image 891
Sathish Avatar asked Mar 22 '09 09:03

Sathish


1 Answers

You have to name your join reference something else. isbn is already a column. Try this:

@OneToOne(optional = true)
@JoinColumn(name = "inventory", referencedColumnName = "isbn",insertable = false, updatable = false)
Inventory inventory;
like image 167
Jesse Avatar answered Sep 28 '22 04:09

Jesse