Java Hibernate 映射具有空值的复合键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/70909/
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
Hibernate mapping a composite key with null values
提问by
With Hibernate, can you create a composite ID where one of the columns you are mapping to the ID can have null values?
使用 Hibernate,您能否创建一个复合 ID,其中您映射到该 ID 的列之一可以具有空值?
This is to deal with a legacy table that has a unique key which can have null values but no primary key.
这是为了处理具有唯一键的旧表,该键可以具有空值但没有主键。
I realise that I could just add a new primary key column to the table, but I'm wondering if there's any way to avoid doing this.
我意识到我可以向表中添加一个新的主键列,但我想知道是否有任何方法可以避免这样做。
采纳答案by Andreas Bakurov
No. Primary keys can not be null.
否。主键不能为空。
回答by Manrico Corazzi
Why would you want to do that? Your composite ID should map the primary key of your table, and it doesn't sound wise to put null values in a key, does it?
你为什么想这么做?您的复合 ID 应该映射表的主键,将空值放在键中听起来并不明智,是吗?
EDIT:Hibernate does not allow to do so; you might put the property outside the key and tweak the DAO a little to take the field into account wherever necessary
编辑:Hibernate 不允许这样做;您可以将属性放在密钥之外并稍微调整 DAO 以在必要时考虑该字段
回答by Paul Shannon
This is not advisable. Could you use a view and map that instead? You could use COALESCE to supply a default if you are stuck with legacy data. We had lots of trouble with composite keys and I imagine null values will cause even more issues.
这是不可取的。你可以使用视图并映射它吗?如果您坚持使用遗留数据,您可以使用 COALESCE 来提供默认值。我们在复合键上遇到了很多麻烦,我想空值会导致更多问题。
回答by Andreas Bakurov
For composite keys (assumed that database allows nulls in PKs) you can have maximum number_of_cols^2 - 1 entries containing nulls, (for example for composite key of 2 columns you can have 3 rows having in their primary key null, the fourth is the PK without nulls).
对于复合键(假设数据库允许 PK 中的空值),您可以拥有最大 number_of_cols^2 - 1 个包含空值的条目(例如,对于 2 列的复合键,您可以有 3 行的主键为空,第四个是PK 没有空值)。
回答by Rakesh Patil
You wont get error but Hibernate wont be able to map those rows with NULL value for composite column to your Entity. That means you get entity with NULL values in result.
您不会收到错误,但 Hibernate 将无法将复合列的那些具有 NULL 值的行映射到您的实体。这意味着您会在结果中获得具有 NULL 值的实体。
回答by BlondCode
Unfortunatly, no. I either had to use a workaround:
不幸的是,没有。我要么不得不使用一种解决方法:
I used composit Id for a view(! not table) where rows can be identified by 2 cols exactly (A, B). Although one of the cols (B) can have null values as well as positive integers. So my workaround is that i created a new col in the view: "BKey" and my view is written as if B is null then value of BKey is -1 else BKey = B. (Only positive integers occour in B and null). I also changed my composit id implementation to use BKey instead of B. Hope it helps for somebody..
我将复合 Id 用于视图(!不是表),其中行可以由 2 个列准确标识(A,B)。尽管 cols (B) 之一可以具有空值以及正整数。所以我的解决方法是我在视图中创建了一个新列:“BKey”并且我的视图被写成好像 B 是空然后 BKey 的值是 -1 否则 BKey = B。(只有正整数出现在 B 和 null 中)。我还更改了我的复合 ID 实现以使用 BKey 而不是 B。希望它对某人有所帮助..