Hello!
Sorry for my bad English.
There are a set of classes:
DBObject annotated @MappedSuperclass is not a separate entity, but it inherited from other classes.
Code:
@MappedSuperclass
public class DBObject {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
protected Integer id;
@Column(name = "correctdt")
@Temporal(TemporalType.TIMESTAMP)
protected Date correctDate;
/* Getters, setters and other properties, shared for all Entities */
}
BuildingList - class "List". It contains a collection of "List items".
Code:
/*Class with collection*/
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "TB_BUILDING_LIST")
@SequenceGenerator(name = "seq",
sequenceName = "sq_building_list_id",
allocationSize = 1)
public class BuildingList extends DBObject implements Serializable {
@Column
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "list")
@LazyCollection(LazyCollectionOption.EXTRA)
//@Fetch(FetchMode.JOIN)
private Collection<BuildingListEntry> entries;
/* Getters, setters and other properties */
}
BuildingListEntry - class "list item".
Code:
/*Main class*/
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "TB_BUILDING_LIST_ENTRY")
@SequenceGenerator(name = "seq",
sequenceName = "sq_building_list_entry_id",
allocationSize = 1)
public class BuildingListEntry extends DBObject implements Serializable {
@Column
protected String comments;
@Column
protected Integer priority;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "list_id")
protected BuildingList list;
@ManyToOne
@JoinColumn(name = "correctuid")
protected User correctUser;
/* Getters, setters and other properties */
}
BLEHome and BLENonLiving expand BuildingListEntry class.
Code:
/* Child class */
@Entity
@Table(name = "TB_BLE_HOME")
public class BLEHome extends BuildingListEntry {
@Column(name = "has_ctv")
protected Integer hasCtv;
/* Getters and setters */
}
Using inheritance strategy "Table Per Subclass".
In class BuildingList collection of BuildingListEntry as Lazy.
When accessing collections after loading the list, formed sql-query with the Cartesian product, where there is no connection between the tables.
Code:
SELECT entries0_.list_id AS list_id8_20_0_,
entries0_.id AS id1_20_0_,
entries0_.id AS id1_20_1_,
entries0_.correctdt AS correctdt2_20_1_,
entries0_.building_id AS building_id6_20_1_,
entries0_.comments AS comments3_20_1_,
entries0_.correctuid AS correctuid7_20_1_,
entries0_.creation_date AS creation_date4_20_1_,
entries0_.list_id AS list_id8_20_1_,
entries0_.priority AS priority5_20_1_,
entries0_.resp_person_id AS resp_person_id9_20_1_,
entries0_1_.has_ctv AS has_ctv1_12_1_,
entries0_2_.crm_client_id AS crm_client_id6_13_1_,
entries0_2_.delay_reason AS delay_reason1_13_1_,
entries0_2_.delayed AS delayed2_13_1_,
entries0_2_.due_date AS due_date3_13_1_,
entries0_2_.install_info AS install_info4_13_1_,
CASE
WHEN entries0_1_.id IS NOT NULL
THEN 1
WHEN entries0_2_.id IS NOT NULL
THEN 2
WHEN entries0_.id IS NOT NULL
0 THEN
END AS clazz_1_
FROM TB_BUILDING_LIST_ENTRY entries0_,
TB_BLE_HOME entries0_1_,
TB_BLE_NONLIVING entries0_2_
WHERE entries0_.list_id =?
What am I doing wrong?
If collection annotated @Fetch (FetchMode.JOIN), it will be correctly loaded immediately upon loading the object.
But I would like it to load on demand, as collection has in most cases not needed.