问题描述
我有映射的ISBN号的图书库存模型,但ISBN不在这两个主键。图书属于书店和库存是一组书店(BookstoreChain)的。清单是由属于一个BookstoreChain所有书店共享
我使用书上侧的Hibernate @OneToOne映射通过加入书号列来获取库存信息。不知何故,休眠生成左外连接正确的查询,但库存为空Book对象上。它不懒加载无论是。无视书店连锁,我怎么做一对一或多对一连接,并获取库存图书时被取出?
类Book {
@Id
龙ID
@柱
字符串ISBN;
@柱
字符串称号;
@OneToOne(可选=真)
@JoinColumn(NAME =书号,referencedColumnName =书号,插入=假,可更新= FALSE)
库存清单;
}
一流的库存{
@Id
龙ID
@柱
字符串chainId
@柱
字符串ISBN
@柱
龙availableQty
}
您必须命名您的加盟参考别的东西。书号已经是一列。试试这个:
@OneToOne(可选=真)
@JoinColumn(NAME =盘点,referencedColumnName =书号,插入=假,可更新= FALSE)
库存清单;
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
}
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;
这篇关于ORM:非主键一对一映射连接列 - 书和清单映射的ISBN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!