简体   繁体   English

JPA Embeddeble PK和可为空字段

[英]JPA Embeddeble PK and nullable field

I have a table called "Attributes" which has a PK of 3 fields which can be null. 我有一个名为“ Attributes”的表,该表的3个字段的PK可以为空。 In this case Style_no is not null but item_no and size_no are null. 在这种情况下,Style_no不为null,但item_no和size_no为null。

Is it possible to have a Embeddeble PK where fields can be null? 是否可以使用Embeddeble PK,其中字段可以为空?

@Entity
@Table(name="ATTRIBUTE")
public class Attribute {

    @EmbeddedId
    private AttributePK attrPK;
    ...

    @Embeddable
    public static class AttributePK implements Serializable{

        private static final long serialVersionUID = -2976341677484364274L;

        @Column(name="STYLE_NO", nullable=true)
        protected String styleNo;

        @Column(name="ITEM_NO", nullable=true)
        protected String itemNo;

        @Column(name="SIZE_NO", nullable=true)
        protected String sizeNo;
    ...

When i try to reference over one field eg style_no the result amount is 0. 当我尝试引用一个字段,例如style_no时,结果量为0。

@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, orphanRemoval=true, mappedBy="attrPK.styleNo")
@MapKey(name="attrPK.name")
public Map<String,Attribute> attributesX;

OR 要么

@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name="STYLE_NO", referencedColumnName="STYLE_NO")
private List<Attribute> attributes;

When i remove item_no and size_no as pk im receiving a valid result. 当我删除item_no和size_no作为pk im收到有效结果时。

Edit: To make my question more specific. 编辑:使我的问题更具体。 Is per JPA guideline or "common sense" not allowed to use nullable fields for EmbeddebedId? 是否按照JPA准则或“常识”不允许将空值字段用于EmbeddebedId? If not, what annotions or logic do i need to add to make it work without adding another PK? 如果没有,我需要添加哪些注释或逻辑以使其在不添加其他PK的情况下起作用? Once filling the nullable field in the PK with values. 一旦用值填充PK中的可为空字段。 The result is corrct. 结果是正确的。

Thank you very much! 非常感谢你!

Since you must not use null in your PK , this is what you should do: 由于您不得在PK中使用null ,因此应这样做:

  1. Add a surrogate primary key . 添加代理主键
  2. You can still achieve the uniqueness constraint and the default PK index with a parial index (in PostgreSQL): 您仍然可以使用局部索引(在PostgreSQL中)实现唯一性约束和默认PK索引:

    CREATE UNIQUE INDEX style_item_size_idx ON my_table (Style_no, item_no, size_no) WHERE (item_no IS NOT NULL AND size_no IS NOT NULL); 在my_table上创建唯一索引style_item_size_idx(Style_no,item_no,size_no)在哪里(item_no不为空且size_no不为空);

在这里看一下似乎(NULL == NULL)-> false的答案与您的问题有关。

Usually a compound pk is not allowed to have null-values in the database. 通常,复合pk在数据库中不允许具有空值。

Technically we would say: Why not, a null can be a value too. 从技术上讲,我们会说:为什么不可以,所以null也可以是一个值。

The DB-Analyst would ask: Ok, if i sort 1,2,3,4, null ,5. 数据库分析师会问:好吧,如果我对1,2,3,4进行排序,则为null ,5。 Would you say null is before 1 or after 5? 您会说null在1之前还是5之后?

Therefore PostgreSQL, Oracle, MySQL will not support null-values in compound primary keys. 因此,PostgreSQL,Oracle,MySQL在复合主键中将不支持空值。

JPA guideline or "common sense" not allowed to use nullable fields for EmbeddebedId? JPA准则或“常识”不允许为EmbeddebedId使用可为空的字段? If not, what annotions or logic do i need to add to make it work without adding another PK? 如果没有,我需要添加哪些注释或逻辑以使其在不添加其他PK的情况下起作用?

answer is as below 答案如下

@EmbeddedId
private AttributePK attrPK;

does not allow the primary key to be null. 不允许主键为null。

so to make it happen use another annotation like below 为了实现这一点,请使用下面的另一个注释

@IdClass(AttributePK.class)
 private AttributePK attrPK;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM