Hibernate 一对多级联效率
过去几周我一直在学习 Hibernate,我已经得到了大部分所学的知识,但对一对多映射的效率有疑问。它有效,但我很确定它可以进行很多调整。保存时,我注意到执行了三个查询,一个是“父”对象的插入,一个是“子”对象的插入,然后是一个子对象的更新查询,用于更新父对象的外键。我的假设是有一种更有效的方法来映射这种关系,以便只有两个插入。我在映射中是否遗漏了一些相对明显的东西?
这是我的代码:
父:
@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=?
I have been learning Hibernate for the past few weeks, I have gotten most of what I learned to work but have a question on the efficiency of a One-to-Many mapping. It works, but I am pretty sure that it could be tweaked quite a bit. When saving, I notice that there are three queries that get executed, an insert for the "Parent" object, an insert for the "Child" object and then an update query for the child that updates the foreign key for the parent object. My assumption is that there is a more efficient way to map this relationship so that there is only the two inserts. Am I missing something relatively obvious in my mapping?
Here is my code:
Parent:
@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;
}
}
Child:
@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 Output:
insert into Punch (punchDate, employeeId)
values (?, ?)
insert into PunchTimes (adjusted, inTime, outTime, punchId_fk)
values (?, ?, ?, ?)
update PunchTimes
set punchId_fk=?
where inoutId=?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了摆脱更新,我必须使外键(pid)insertable = false,updatable = false
To get rid of the update I had to make the foreign-key (pid) insertable=false, updatable=false