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=?