ORM:非主键连接列上的一对一映射 - 按 ISBN 映射的图书和库存
我有一个由 ISBN 编号映射的图书模型和库存模型,但 ISBN 都不是其中的主键。 书籍属于书店,库存是针对一组书店(BookstoreChain)的。 库存由属于连锁书店的所有书店共享。
我在图书端使用 Hibernate @OneToOne 映射通过加入 ISBN 列来获取库存信息。 不知何故,Hibernate 正确生成左外连接查询,但 Book 对象上的 inventory 为空。 它也不是延迟加载的。 忽略书店和连锁店,在获取书籍时如何进行一对一或多对一连接并获取库存?
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
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我怀疑这与这个问题有任何关系,但我想我无论如何都会提出它,只是为了确保它不是一个被忽视的问题:
参考:[http:// /docs.jboss.org/ejb3/app-server/HibernateAnnotations/reference/en/html_single/index.html][1]
我知道你说正在生成 LEFT OUTER JOIN,但我认为如果未指定,则默认获取为 LAZY。 也许,明确指定获取模式可能会产生不同的结果。
最后,如果您使用 HQL 查询,也许将其与任何其他实体类一起发布将有助于社区解决问题。
I doubt that this has anything to do with the issue, but I thought I would bring it up anyways just to ensure that it isn't a gotcha that was overlooked:
Reference: [http://docs.jboss.org/ejb3/app-server/HibernateAnnotations/reference/en/html_single/index.html][1]
I know you said that the LEFT OUTER JOIN is being generated, but I thought that the default fetch was LAZY if it was not specified. Perhaps, explicilty specifying the fetch mode may yield different results.
Lastly, if you are using an HQL query, perhaps posting that with any other entity classes will help the community help resolve the issue.
根据您在此处显示的内容,您的数据库架构没有意义。 书籍和库存之间的关系应该是一对多 - 假设同一本书在多个库存中,这意味着您不能仅将书籍和库存与 isbn 关联起来。 由于 isbn 在各个库存中不是唯一的,因此库存中会有多行具有相同的 isbn 但不同的 chainId - 哪一行是给定书籍/isbn 的正确行? 图书应该有一个指向 inventory.id 的外键,而不是指向 inventory.isbn。
Your database schema doesn't make sense, based on what you've shown here. The relationship between book and inventory should be one to many - presumably, the same book is in several inventories, which means you can't associate books and inventories just with the isbn. Since the isbn is not unique across inventories, you'll have multiple rows in inventory with the same isbn but different chainIds - which row is the right row for a given book/isbn? Book should have a foreign key to inventory.id, not inventory.isbn.
只是猜测:name = 'ISBN' 是否需要与 Inventory 中的字段相同?
just a guess: does name = 'ISBN' need to be the same case as the field in Inventory?
您必须将您的连接引用命名为其他名称。 isbn 已经是一个列。 尝试这个:
You have to name your join reference something else. isbn is already a column. Try this: