java ORM:非主键连接列上的 OneToOne 映射 - ISBN 映射的图书和库存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/670741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:19:12  来源:igfitidea点击:

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

javahibernateormactiverecordone-to-one

提问by Sathish

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.

我有一个按 ISBN 号映射的 Book 模型和 Inventory 模型,但 ISBN 不是其中的主键。书籍属于书店,库存是一组书店(BookstoreChain)。库存由属于 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?

我在书本上使用 Hibernate @OneToOne 映射通过加入 ISBN 列来获取库存信息。不知何故,Hibernate 正确生成了左外连接查询,但 Book 对象上的库存为空。它也不是懒加载。忽略书店和连锁店,我如何在获取书籍时执行 OneToOne 或 ManyToOne join 并获取库存?

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
}

回答by Jesse

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

您必须将您的连接引用命名为其他名称。isbn 已经是一列了。试试这个:

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

回答by Matt Sidesinger

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:

我怀疑这与这个问题有什么关系,但我想我还是会提出来,以确保它不是一个被忽视的问题:

Note that when using referencedColumnName to a non primary key column, the associated class has to be Serializable.

请注意,当对非主键列使用 referencedColumnName 时,关联的类必须是可序列化的。

Reference: [http://docs.jboss.org/ejb3/app-server/HibernateAnnotations/reference/en/html_single/index.html][1]

参考:[ 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.

我知道您说正在生成 LEFT OUTER JOIN,但我认为如果未指定默认提取是 LAZY。也许,明确指定获取模式可能会产生不同的结果。

Lastly, if you are using an HQL query, perhaps posting that with any other entity classes will help the community help resolve the issue.

最后,如果您使用的是 HQL 查询,也许将其与任何其他实体类一起发布将有助于社区帮助解决问题。

回答by Steve

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.

根据您在此处显示的内容,您的数据库架构没有意义。账簿和库存之间的关系应该是一对多的 - 大概,同一本书在多个库存中,这意味着您不能将账簿和库存仅与 isbn 相关联。由于 isbn 在库存中不是唯一的,您将在库存中有多行具有相同的 isbn 但不同的 chainIds - 哪一行是给定书籍/isbn 的正确行?Book 应该有一个到inventory.id 的外键,而不是inventory.isbn。

回答by Martlark

just a guess: does name = 'ISBN' need to be the same case as the field in Inventory?

只是猜测: name = 'ISBN' 是否需要与 Inventory 中的字段相同?