Hibernate One-to-Many级联效率

时间:2010-01-12 15:57:25

标签: hibernate performance mapping one-to-many

过去几周我一直在学习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=?

1 个答案:

答案 0 :(得分:1)

import java.io.Serializable;
import javax.persistence.*;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;

@Entity
@Table(name = "PunchTimes")
public class PunchTimes implements Serializable
{
    private static final long serialVersionUID = 20100106;
    private Long id;
    private Long pid;
    private DateTime inTime;
    private DateTime outTime=null;
    private Double adjustedTime=null;
    private Boolean adjustedByOperator=false;
    private Boolean overtimeAuthorized;
    private Punch punch;

    public PunchTimes()
    {
    }

    @Column(name = "adjusted")
    public Double getAdjustedTime()
    {
        return adjustedTime;
    }

    public void setAdjustedTime(Double adjustedTime)
    {
        this.adjustedTime = adjustedTime;
    }

    @Id
    @GeneratedValue
    @Column(name = "inoutId")
    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    @Column(name = "inTime")
    @Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
    public DateTime getInTime()
    {
        return inTime;
    }

    public void setInTime(DateTime inTime)
    {
        this.inTime = inTime;
    }

    @Column(name = "outTime")
    @Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
    public DateTime getOutTime()
    {
        return outTime;
    }

    public void setOutTime(DateTime outTime)
    {
        this.outTime = outTime;
    }

    @Column(name = "punchId_fk" ,insertable = false, updatable = false)
    public Long getPid()
    {
        return pid;
    }

    public void setPid(Long pid)
    {
        this.pid = pid;
    }

    @ManyToOne
    @JoinColumn(name = "punchId_fk" )
    public Punch getPunch()
    {
        return punch;
    }

    public void setPunch(Punch punch)
    {
        this.punch = punch;
    }

要摆脱更新,我必须使外键(pid)insertable = false,updatable = false