Issue
When I'm trying to insert a row in the following table, Hibernate does a SELECT
before each insert and I'd like to avoid that. I think that Hibernate does this SELECT
before the insert to see if that object is new or not. That's why I have implemented Persistable
interface and I have overridden the methods of this.
The entity has combine id.
select groupreleq0_.equipment_id as equipmen3_22_0_, groupreleq0_.program_group_id as program_4_22_0_, groupreleq0_.description as descript1_22_0_, groupreleq0_.quantity as quantity2_22_0_ from group_m2m_equipment groupreleq0_ where groupreleq0_.equipment_id=? and groupreleq0_.program_group_id=?
select groupreleq0_.equipment_id as equipmen3_22_0_, groupreleq0_.program_group_id as program_4_22_0_, groupreleq0_.description as descript1_22_0_, groupreleq0_.quantity as quantity2_22_0_ from group_m2m_equipment groupreleq0_ where groupreleq0_.equipment_id=? and groupreleq0_.program_group_id=?
select groupreleq0_.equipment_id as equipmen3_22_0_, groupreleq0_.program_group_id as program_4_22_0_, groupreleq0_.description as descript1_22_0_, groupreleq0_.quantity as quantity2_22_0_ from group_m2m_equipment groupreleq0_ where groupreleq0_.equipment_id=? and groupreleq0_.program_group_id=?
select groupreleq0_.equipment_id as equipmen3_22_0_, groupreleq0_.program_group_id as program_4_22_0_, groupreleq0_.description as descript1_22_0_, groupreleq0_.quantity as quantity2_22_0_ from group_m2m_equipment groupreleq0_ where groupreleq0_.equipment_id=? and groupreleq0_.program_group_id=?
insert into group_m2m_equipment (description, quantity, equipment_id, program_group_id) values (?, ?, ?, ?)
insert into group_m2m_equipment (description, quantity, equipment_id, program_group_id) values (?, ?, ?, ?)
insert into group_m2m_equipment (description, quantity, equipment_id, program_group_id) values (?, ?, ?, ?)
insert into group_m2m_equipment (description, quantity, equipment_id, program_group_id) values (?, ?, ?, ?)
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@Embeddable
@Getter
@Setter
public class GroupEquipmentPK implements Serializable {
protected Long programGroupId;
protected Long equipmentId;
}
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Table(name = "group_m2m_equipment")
public class GroupRelEquipment implements Serializable, Persistable<GroupEquipmentPK> {
@EmbeddedId
private GroupEquipmentPK groupEquipmentPK = new GroupEquipmentPK();
@ManyToOne(cascade = {CascadeType.PERSIST})
@JsonIgnore
@MapsId("equipmentId")
private Equipment equipment;
@ManyToOne(cascade = {CascadeType.PERSIST})
@JsonIgnore
@MapsId("programGroupId")
private ProgramGroup programGroup;
@Column(name = "description")
private String description;
@Column(name = "quantity ")
private Integer quantity;
@Transient
private boolean isNew = true;
public GroupRelEquipment(GroupEquipmentPK groupEquipmentPK, Equipment equipment, ProgramGroup programGroup, String description, Integer quantity) {
this.groupEquipmentPK = groupEquipmentPK;
this.equipment = equipment;
this.programGroup = programGroup;
this.description = description;
this.quantity = quantity;
}
@Override
public GroupEquipmentPK getId() {
return this.groupEquipmentPK;
}
@Override
public boolean isNew() {
return isNew;
}
@PrePersist
@PostLoad
void markNotNew() {
this.isNew = false;
}
}
Solution
Hibernate does a SELECT query before the INSERT to check if this value is already exists... I found a solution for that by calling EntityManager object and use .persist() method insteed .save() and that's how I avoided those SELECTS
Answered By - Ugleethyn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.