2

I have the following class which has a @OneToMany relationship, but it only returns one of the two rows:

@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;
    }
}

And the class to be retrieved:

@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;
    }

}

I already tried to change the Set to List but no luck (and after reading a bit about the differences between them it make no sense to change it).

I have no clue of what is happening, and not sure if it always worked like that.

Thanks!

UPDATE: Tried implementing equals, nothing happened.

Luke SpringWalker
  • 1,600
  • 3
  • 19
  • 33

2 Answers2

1

As per my understanding, you need to add reference(as @ManyToOne) of ItemExtensionEntity in your ItemAttributeEntity class and Add mappedby attribute inside @OneToMany annotation on your Entity ItemExtensionEntity

Reference: 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;
    }

}
Ankit
  • 2,126
  • 4
  • 33
  • 53
1

Try to use fetch = FetchType.LAZY on itemAttributeEntities.

Anton Tupy
  • 951
  • 5
  • 16
  • 1
    In case of eager loading of subcollection hibernate do sql with join of two tables (main table and table of subcollection objects) and not very correct processes sql's resultset – Anton Tupy Jan 04 '18 at 19:02