I'm trying to represent a parent/child relationship between items who only differ through a single value in a special item_type column(with the value: NONE,PARENT,CHILD), Parents and Childs have the same "meta" article_id. I want to reuse the same table and entity for all items because there isnt a difference in the fields between Parent/Child/None Items. The sql I want hibernate to do and return as List is something like this:
SELECT *
FROM item i1
JOIN item i2 ON i1.article_id = i2.article_id
WHERE i1.item_type = 'PARENT' AND i2.item_type = 'CHILD';
This is my Entity:
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "item", schema = "public", catalog = "item_db")
@DynamicUpdate
public class Item extends BaseDatesEntity {
@Id
@SequenceGenerator(name="item_id_seq", sequenceName = "item_id_seq", allocationSize = 5)
@GeneratedValue(strategy = SEQUENCE, generator = "item_id_seq")
@Column(name = "id", insertable = true, updatable = false, nullable = false, unique = true)
protected Long id;
@NotNull
@Column(name = "user_service_id", nullable = false, insertable = true, updatable = false)
private Long userServiceId;
@NotNull
@Column(name = "article_id", nullable = false, insertable = true, updatable = false)
private Long articleId;
@Size(min = 1, max = 100)
@Column(name = "title", nullable = false, insertable = true, updatable = true)
private String title;
@Column(name = "current_price", nullable = false, insertable = true, updatable = true)
private Integer currentPrice;
@Column(name = "shipping_costs", nullable = false, insertable = true, updatable = true)
private Integer shippingCosts;
@Min(0)
@Column(name = "quantity", nullable = false, insertable = true, updatable = true)
private Integer quantity;
@Column(name = "item_type", nullable = false, insertable = true, updatable = true)
private ItemType itemType;
@OneToMany(fetch = LAZY)
@JoinColumns({
@JoinColumn(name = "article_id", referencedColumnName = "article_id"),
@JoinColumn(name = "user_service_id", referencedColumnName = "user_service_id")
})
@Where(clause = "item_type = 'CHILD'")
private List<Item> childs;
}
This is the Enum which shows if the item is a parent a child or a simple item without childs:
@AllArgsConstructor(access = PRIVATE)
public enum ItemType {
NONE,
PARENT,
CHILD;
}
and the database table:
CREATE TABLE item
(
id BIGINT DEFAULT nextval('item_id_seq'::regclass) PRIMARY KEY NOT NULL,
user_service_id BIGINT NOT NULL,
article_id BIGINT NOT NULL,
title VARCHAR(100) NOT NULL,
current_price INTEGER NOT NULL,
shipping_costs INTEGER NOT NULL,
quantity INTEGER NOT NULL,
item_type ITEM_TYPE DEFAULT 'NONE'::item_type NOT NULL,
);
The problem is i can't seem to find a way to get the both where condition i1.item_type = 'PARENT'
in hibernate with annotations in the entity.
Another approach was to join the table with itself:
@JoinTable(
name="item",
joinColumns={
@JoinColumn(name = "article_id", referencedColumnName = "article_id"),
@JoinColumn(name = "user_service_id", referencedColumnName = "user_service_id")
}
)
@WhereJoinTable(clause = "item_type = 'CHILD'")
@Where(clause = "item_type = 'PARENT'")
private List<Item> childs;
A view or another table for the childs isn't an option. A single-table solution is demanded.
I hope my problem is clear and thanks in advance.