2
過去幾周我一直在學習Hibernate,我已經獲得了大部分我學會的工作,但對一對多映射的效率有疑問。它的工作原理,但我很確定它可以調整很多。保存時,我注意到有三個查詢被執行,一個是「Parent」對象的插入,一個是「Child」對象的插入,然後是更新父對象外鍵的子查詢。我的假設是有一種更有效的方式來映射這種關係,以便只有兩個插入。我在映射中丟失了一些相對明顯的東西嗎?休眠一對多級聯效率
這裏是我的代碼:
家長:
@Entity
@Table(name="Punch")
public class Punch implements Serializable
{
private Long id;
private DateTime punchDate;
private Integer userId;
private List<PunchTimes> punches= new ArrayList<PunchTimes>();
private static final long serialVersionUID=2010010611;
...Various getters & setters...
@OneToMany
@JoinColumn(name="punchId_fk")
@OrderBy("pid")
@Cascade(org.hibernate.annotations.CascadeType.ALL)
public List<PunchTimes> getPunches()
{
return punches;
}
}
兒童:
@Entity
@Table(name = "PunchTimes")
public class PunchTimes implements Serializable{
private Long id;
private Long pid;
private DateTime inTime;
private DateTime outTime;
private Double adjustedTime;
private static final long serialVersionUID = 20100106;
private Punch punch;
...Various getters & setters...
@ManyToOne
@JoinColumn(name = "punchId_fk", insertable = false, updatable = false)
public Punch getPunch(){
return punch;
}
}
SQL輸出:
insert into Punch (punchDate, employeeId)
values (?, ?)
insert into PunchTimes (adjusted, inTime, outTime, punchId_fk)
values (?, ?, ?, ?)
update PunchTimes
set punchId_fk=?
where inoutId=?