@Entity
@Table(name = "ITEM",
indexes = @Index(columnList = "CODE", unique = true))
public class Item implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
protected Long id;
@Column
protected String code;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
protected Set<ItemRelation> lowerItemRelations = new LinkedHashSet<>();
@OneToMany(mappedBy = "child", cascade = CascadeType.ALL, orphanRemoval = true)
protected Set<ItemRelation> higherItemRelations = new LinkedHashSet<>();
...
}
@Entity
@Table(name = "ITEM_RELATION",
indexes = @Index(columnList = "PARENT_ID, CHILD_ID", unique = true))
public class ItemRelation implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
protected Long id;
@ManyToOne(optional = false)
@JoinColumn(name = "PARENT_ID")
private Item parent;
@ManyToOne(optional = false)
@JoinColumn(name = "CHILD_ID")
private Item child;
@Column(nullable = false, columnDefinition = "INT DEFAULT 0 NOT NULL")
private int quantity = 1;
...
}