uiiooo
uiiooo
采纳率33.3%
2017-07-16 16:03 阅读 1.8k

hibernate一对多级联保存问题

20

这是两个类
这是一端

 @Entity
public class Payroll implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column private long pid;

    @Column private long sid;
    //结账日期
    @Column private Timestamp date;
    //累结
    @Column private double totalSum;
    //结余
    @Column private double balance;
    //伙食费用
    @Column private double boardWages;
    //扣款
    @Column private double deduct;
    //欠款
    @Column private double debt;
    ///预支
    @Column private double advance;
    //薪水细则
    @OneToMany(mappedBy="payroll",cascade = CascadeType.ALL, fetch = FetchType.LAZY)  
    @Column private List<PayrollRow> payrollRow;

    public long getSid() {
        return sid;
    }
    public void setSid(int sid) {
        this.sid = sid;
    }
    public double getTotalSum() {
        return totalSum;
    }
    public void setTotalSum(double totalSum) {
        this.totalSum = totalSum;
    }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public double getBoardWages() {
        return boardWages;
    }
    public void setBoardWages(double boardWages) {
        this.boardWages = boardWages;
    }
    public double getDeduct() {
        return deduct;
    }
    public void setDeduct(double deduct) {
        this.deduct = deduct;
    }
    public double getDebt() {
        return debt;
    }
    public void setDebt(double debt) {
        this.debt = debt;
    }
    public double getAdvance() {
        return advance;
    }
    public void setAdvance(double advance) {
        this.advance = advance;
    }
    public Timestamp getDate() {
        return date;
    }
    public void setDate(Timestamp date) {
        this.date = date;
    }
    public long getPid() {
        return pid;
    }
    public void setPid(long pid) {
        this.pid = pid;
    }
    public List<PayrollRow> getPayrollRow() {
        return payrollRow;
    }

    public void setPayrollRow(List<PayrollRow> payrollRow) {
        this.payrollRow = payrollRow;
    }
    public void setSid(long sid) {
        this.sid = sid;
    }

}

这是多端

 @Entity
@Table(name = "payroll_row")
public class PayrollRow implements Serializable{


    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @Column private long prid;
    @Column(name="style_number") private String styleNo;
    @Column private String process;
    @Column private int count;
    @Column private double cost;
    @Column private double amount;
    @ManyToOne(cascade=CascadeType.REFRESH,optional=false,fetch = FetchType.LAZY)  
    @JoinColumn(name="pid")  
    private Payroll payroll;

    public Payroll getPayroll() {
        return payroll;
    }
    public void setPayroll(Payroll payroll) {
        this.payroll = payroll;
    }
    public String getStyleNo() {
        return styleNo;
    }
    public void setStyleNo(String styleNo) {
        this.styleNo = styleNo;
    }
    public String getProcess() {
        return process;
    }
    public void setProcess(String process) {
        this.process = process;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    public double getCost() {
        return cost;
    }
    public void setCost(double cost) {
        this.cost = cost;
    }
    public double getAmount() {
        return amount;
    }
    public void setAmount(double amount) {
        this.amount = amount;
    }
    public long getPrid() {
        return prid;
    }
    public void setPrid(long prid) {
        this.prid = prid;
    }



}

一开始将PayrollRow的list赋值给Payroll一端只保存一端失败
后来把PayrollRow里面的Payroll进行赋值 两者关联起来 结果还是只保存了Payroll的数据 请问何解 还有我如果想只通过一端保存来保存两个表的信息该怎么做 谢谢

按一楼修改后执行如下语句

 payroll.setPid(30);
        List<PayrollRow> pwl = payroll.getPayrollRow();
        for (int i = 0;i<pwl.size();i++) {
            pwl.get(i).setPayroll(payroll);

        }
        payroll.setPayrollRow(pwl);
        payrollService.save(payroll);

结果控制台输出的sql语句没有insert
只有这
Hibernate:
select
payrollrow_.prid,
payrollrow_.amount as amount2_5_,
payrollrow_.cost as cost3_5_,
payrollrow_.count as count4_5_,
payrollrow_.payroll_id as payroll_7_5_,
payrollrow_.process as process5_5_,
payrollrow_.style_number as style_nu6_5_
from
payrollrow payrollrow_
where
payrollrow_.prid=?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • qq_34581334 为梦想增值 2017-07-17 01:23

    Payroll p=new Payroll ();

    p.setSid(xxx);

    .

    .

    .



    List pw=p.getPayrollRow();

    pw.setCount(xxx);

    .

    .

    .

    p.setPayrollRow(pw);

                最后再把Payroll提交保存。
    


    最后把 @ManyToOne(cascade=CascadeType.REFRESH,optional=false,fetch = FetchType.LAZY)

                改成   @ManyToOne(cascade=CascadeType.ALL,optional=false,fetch = FetchType.LAZY)
    
    点赞 评论 复制链接分享
  • pandahii pandahii 2017-07-17 03:17

    你好!
    失败的最好把信息贴出来,因为不知道是不是你配置问题!
    其次就是cascade类型 eg.

    @OneToMany(mappedBy="group",
           cascade={CascadeType.ALL},
            fetch=FetchType.EAGER
             )      
    cascade 的注解作用是:group对象的增删改关联到user对象 
    fetch是读取查询关联
    
    fetch和lazy是用来定义级联查询的方式
    cascade和inverse是用来管理级联插入和修改
    
    CascadeType取值
    ALL              Cascade all operations所有情况
    MERGE        Cascade merge operation合并(merge=save+update)
    PERSIST       Cascade persist operation存储 persist()
    REFRESH     Cascade refresh operation刷新
    REMOVE     Cascade remove operation删除
    
    点赞 评论 复制链接分享

相关推荐