代码之家  ›  专栏  ›  技术社区  ›  Luke SpringWalker

休眠-OneToMany仅检索一行

  •  2
  • Luke SpringWalker  · 技术社区  · 7 年前

    我有下面一节课 @OneToMany 关系,但它只返回两行中的一行:

    @Entity
    @javax.persistence.Table(name = "as_itm_ext")
    public class ItemExtensionEntity implements java.io.Serializable {
    
        @OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
        @JoinColumns({ @JoinColumn(name = "id_itm", referencedColumnName = "id_itm", insertable = false, updatable = false) })
        public Set<ItemAttributeEntity> getItemAttributeEntities() {
            return this.itemAttributeEntities;
        }
    
        public void setItemAttributeEntities(Set<ItemAttributeEntity> itemAttributeEntities) {
            this.itemAttributeEntities = itemAttributeEntities;
        }
    }
    

    以及要检索的类:

    @Entity
    @javax.persistence.Table(name = "as_itm_att")
    public class ItemAttributeEntity implements java.io.Serializable {
    
        private static final long serialVersionUID = 1L;
    
        /**
         * AttributeKey,The key of the attribute.
         */
        private ItemAttributeEntityId id;
    
        /**
         * AttributeValue,The value of the attribute.
         */
        private String attributeValue;
    
        public ItemAttributeEntity() {
        }
    
        public ItemAttributeEntity(ItemAttributeEntityId id, String attributeValue) {
            this.id = id;
            this.attributeValue = attributeValue;
        }
    
        /**
         * AttributeKey,The key of the attribute.
         */
        @EmbeddedId
        @AttributeOverrides({
                @AttributeOverride(name = "itemId", column = @Column(name = "id_itm", nullable = false, length = 32)),
                @AttributeOverride(name = "attributeKey", column = @Column(name = "att_key", nullable = false, length = 32)) })
        public ItemAttributeEntityId getId() {
            return this.id;
        }
    
        public void setId(ItemAttributeEntityId id) {
            this.id = id;
        }
    
        /**
         * AttributeValue,The value of the attribute.
         */
        @Column(name = "att_value", nullable = false, length = 32)
        public String getAttributeValue() {
            return this.attributeValue;
        }
    
        public void setAttributeValue(String attributeValue) {
            this.attributeValue = attributeValue;
        }
    
    }
    

    我已经试着改变 Set List 但是运气不好(读了一点他们之间的差异之后,改变它是没有意义的)。

    我不知道发生了什么,也不确定它是否总是这样。

    谢谢

    更新 :已尝试实施 equals ,什么也没发生。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Anton Tupy    7 年前

    尝试使用 fetch=FetchType。懒惰的 在…上 项目属性属性 .

        2
  •  1
  •   Ankit    7 年前

    根据我的理解,您需要添加以下引用(如@manytone) ItemExtensionEntity 在您的 ItemAttributeEntity 类别和 在实体的@OneToMany注释中添加mappedby属性 项目扩展性

    参考号: Can someone please explain mappedBy in hibernate?

    @Entity
    @javax.persistence.Table(name = "as_itm_ext")
    public class ItemExtensionEntity implements java.io.Serializable {
    
        @OneToMany(mappedBy="parentEntity" ,fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
        //@JoinColumns({ @JoinColumn(name = "id_itm", referencedColumnName = "id_itm", insertable = false, updatable = false) })
        public Set<ItemAttributeEntity> getItemAttributeEntities() {
            return this.itemAttributeEntities;
        }
    
        public void setItemAttributeEntities(Set<ItemAttributeEntity> itemAttributeEntities) {
            this.itemAttributeEntities = itemAttributeEntities;
        }
    }
    
    
    @Entity
    @javax.persistence.Table(name = "as_itm_att")
    public class ItemAttributeEntity implements java.io.Serializable {
    
        private static final long serialVersionUID = 1L;
    
        /**
         * AttributeKey,The key of the attribute.
         */
        private ItemAttributeEntityId id;
    
        /**
         * AttributeValue,The value of the attribute.
         */
        private String attributeValue;
    
        @ManyToOne
        @JoinColumn(name="<columnname>", nullable=false)
        private ItemExtensionEntity parentEntity;
    
        //add getter and setters
    
        public ItemAttributeEntity() {
        }
    
        public ItemAttributeEntity(ItemAttributeEntityId id, String attributeValue) {
            this.id = id;
            this.attributeValue = attributeValue;
        }
    
        /**
         * AttributeKey,The key of the attribute.
         */
        @EmbeddedId
        @AttributeOverrides({
                @AttributeOverride(name = "itemId", column = @Column(name = "id_itm", nullable = false, length = 32)),
                @AttributeOverride(name = "attributeKey", column = @Column(name = "att_key", nullable = false, length = 32)) })
        public ItemAttributeEntityId getId() {
            return this.id;
        }
    
        public void setId(ItemAttributeEntityId id) {
            this.id = id;
        }
    
        /**
         * AttributeValue,The value of the attribute.
         */
        @Column(name = "att_value", nullable = false, length = 32)
        public String getAttributeValue() {
            return this.attributeValue;
        }
    
        public void setAttributeValue(String attributeValue) {
            this.attributeValue = attributeValue;
        }
    
    }