Hibernate generatig multiple queries when try to fetch record for multi level mapping. Inventroy has Line Item and status. And Each Line Item has Groups and Orders. I need to get all the class populated so I have written hql query SELECT DISTINCT chgbk, 1 FROM Inventory invnt RIGHT OUTER JOIN invnt.item as litem JOIN litem.gp as group WHERE litem.lineItmName = :_LINE_ITEM_NAME. But Hibernate created 3 queries behind and this cause some issue in production. 1. select distinct invnt0.INVENT_ID as col_0_0_, 1 as col_1_0_ from Inventory invnt0 2.select lineItm0.LINE_ITM_ID,lineItm0.LINE_ITEM_NAME,order0.PRC_ORDER_ID,order0.ORD_NAME,grop0.LINE_ITM_GRP_ID,grop0.GRP_NAME from LineItem lineItm0 left outer join Group grop0 on lineItm0.D_GRP_ID=grop0.LINE_ITM_GRP_ID left outer join Order order0 on lineItm0.LINE_ITEM_PRD_NM=order0.PRC_ORDER_ID where lineItm0.LINE_ITM_ID = ? 3.select status0_.STATUS_ID as STATUS1_2_0_, status0_.GRP_NAME as MCOM2_2_0_ from STATUS status0_ where status0_.STATUS_ID=? Hibernate automatically generates the 2 adn 3rd queries. But I need to add another condition in the where clause of the 2nd query. But the second query is automatically generate I can not alter it. 1.Is any possible way to add another condition in the where clause of the 2nd query. 2.If not possible is there a way to modify the main query so that only one query executes which load all the objects. ########################################INVENTROY Class############################ @Entity(name = "Inventory") @Table(name = "Inventory") public class Inventory {
private String id""; private LineItem item; private String lineItemId; private Status statusKey; private String statCode; @Id @Column(name = "INVENT_ID") public String getId() {} @OneToOne @JoinColumn(name = "LINE_ITEM_ID", nullable = true) public LineItem getItem() {} @Column(name = "LINE_ITEM_ID", insertable = false, updatable = false, nullable = true) public String lineItemId() {} @ManyToOne @JoinColumn(name = "INVNT_STAT_CODE", insertable = false, updatable = false) public Status getStatusKey() {} @Column(name = "INVNT_STAT_CODE") public String getStatCode() {}
} ################################# LINE ITEM Class################################### @Entity(name = "LineItem") @Table(name = "LineItem") public class LineItem {
private String id; private String lineItmName; private Order ord; private Group gp; private String lineItmPrdId; private String grpId; @Id @Column(name = "LINE_ITM_ID", nullable = false) public String getId() {} @Column(name = "LINE_ITEM_NAME") public String getLineItmName() {} @OneToOne @JoinColumns({ @JoinColumn(name="LINE_ITEM_PRD_NM", referencedColumnName = "PRC_ORDER_ID", insertable = false, updatable = false ) }) public Order getOrd() {} @Column(name = "LINE_ITEM_PRD_NM") public String getLineItmPrdId() {} @ManyToOne @JoinColumn(name = "D_GRP_ID") public Group getGp() {} @Column(name = "D_GRP_ID", insertable = false, updatable = false) public String getGrpId() {} } ######################################ORDER CLASS ###################################################
@Entity(name = "Order") @Table(name = "Order") public class Order {
private String ordId; private String ordName; @Id @Column(name = "PRC_ORDER_ID") public String getOrdId() {} @Column(name = "ORD_NAME") public String getOrdName() {}
} ############################################### GROUP Class####################### @Entity(name = "Group") @Table(name = "Group") public class Group {
pubilc String lineItmPrdNum; public String grpName; @Id @Column(name = "LINE_ITM_GRP_ID") public String getSeqNo() {} @Column(name = "GRP_NAME") public String getGrpName () {} } ############################################### Status Class####################### @Entity(name = "STATUS") @Table(name = "STATUS") public class Status {
pubilc String statusCd; public String statName; @Id @Column(name = "STATUS_ID") public String getStatusCd() {} @Column(name = "GRP_NAME") public String getStatName () {} }
|