How to fix the Hibernate “object references an unsaved transient instance - save the transient instance before flushing” error

前端 未结 30 1109
既然无缘
既然无缘 2020-11-22 07:11

I receive following error when I save the object using Hibernate

object references an unsaved transient instance - save the transient instance before flushi         


        
30条回答
  •  情歌与酒
    2020-11-22 07:19

    There are so many possibilities of this error some other possibilities are also on add page or edit page. In my case I was trying to save a object AdvanceSalary. The problem is that in edit the AdvanceSalary employee.employee_id is null Because on edit I was not set the employee.employee_id. I have make a hidden field and set it. my code working absolutely fine.

        @Entity(name = "ic_advance_salary")
        @Table(name = "ic_advance_salary")
        public class AdvanceSalary extends BaseDO{
    
            @Id
            @GeneratedValue(strategy = GenerationType.IDENTITY)
            @Column(name = "id")
            private Integer id;
    
            @ManyToOne(fetch = FetchType.EAGER)
            @JoinColumn(name = "employee_id", nullable = false)
            private Employee employee;
    
            @Column(name = "employee_id", insertable=false, updatable=false)
            @NotNull(message="Please enter employee Id")
            private Long employee_id;
    
            @Column(name = "advance_date")
            @DateTimeFormat(pattern = "dd-MMM-yyyy")
            @NotNull(message="Please enter advance date")
            private Date advance_date;
    
            @Column(name = "amount")
            @NotNull(message="Please enter Paid Amount")
            private Double amount;
    
            @Column(name = "cheque_date")
            @DateTimeFormat(pattern = "dd-MMM-yyyy")
            private Date cheque_date;
    
            @Column(name = "cheque_no")
            private String cheque_no;
    
            @Column(name = "remarks")
            private String remarks;
    
            public AdvanceSalary() {
            }
    
            public AdvanceSalary(Integer advance_salary_id) {
                this.id = advance_salary_id;
            }
    
            public Integer getId() {
                return id;
            }
    
            public void setId(Integer id) {
                this.id = id;
            }
    
            public Employee getEmployee() {
                return employee;
            }
    
            public void setEmployee(Employee employee) {
                this.employee = employee;
            }
    
    
            public Long getEmployee_id() {
                return employee_id;
            }
    
            public void setEmployee_id(Long employee_id) {
                this.employee_id = employee_id;
            }
    
        }
    

提交回复
热议问题