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:
Hibernate:
insert
into
Punch
(punchDate, employeeId)
values
(?, ?)
Hibernate:
insert
into
PunchTimes
(adjusted, inTime, outTime, punchId_fk)
values
(?, ?, ?, ?)
Hibernate:
update
PunchTimes
set
punchId_fk=?
where
inoutId=?