Hibernate 一对多级联效率

发布于 2024-08-17 18:29:49 字数 1555 浏览 10 评论 0原文

过去几周我一直在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朕就是辣么酷 2024-08-24 18:29:49
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

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;
    }

To get rid of the update I had to make the foreign-key (pid) insertable=false, updatable=false

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文