代码之家  ›  专栏  ›  技术社区  ›  Tushar Ahirrao

对象引用未保存的临时实例-刷新前保存临时实例

  •  471
  • Tushar Ahirrao  · 技术社区  · 14 年前

    使用Hibernate保存对象时收到以下错误

    object references an unsaved transient instance - save the transient instance before flushing
    
    24 回复  |  直到 6 年前
        1
  •  640
  •   Bozho    14 年前

    cascade="all" cascade=CascadeType.ALL

        2
  •  183
  •   nbro kai    7 年前

    @OneToOne @OneToMany Child Parent "object references an unsaved transient instance - save the transient instance before flushing"

    cascade = {CascadeType.ALL} Parent's

    @OneToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name = "performancelog_id")
    public PerformanceLog getPerformanceLog() {
        return performanceLog;
    }
    
        3
  •  50
  •   Cookalino    11 年前

        4
  •  24
  •   Haris Osmanagić    11 年前

    import org.hibernate.annotations.Cascade;
    import org.hibernate.annotations.CascadeType;
    
    ...
    
    @Cascade({CascadeType.SAVE_UPDATE})
    private Set<Child> children;
    
        5
  •  16
  •   rtruszk David Brunelle    9 年前

    CascadeType @ManyToOne CascadeType.ALL @OneToMany

    @OneToMany(cascade = CascadeType.ALL, mappedBy="globalConfig", orphanRemoval = true)
    private Set<GlobalConfigScope>gcScopeSet;
    

    @ManyToOne
    @JoinColumn(name="global_config_id")
    private GlobalConfig globalConfig;
    

    CascadeType.PERSIST

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name="global_config_id")
    private GlobalConfig globalConfig;
    
        6
  •  14
  •   dukethrash    10 年前

        7
  •  10
  •   rtruszk David Brunelle    9 年前

    X x2 = new X();
    x.setXid(memberid); // Error happened here - x was a previous global entity I created earlier
    Y.setX(x2);
    

    String xid catch

    {
       code block that performed the operation
    } catch (Exception e) {
       e.printStackTrace(); // put a break-point here and inspect the 'e'
       return ERROR;
    }
    
        8
  •  6
  •   Joabe Lucena    10 年前

    object.SetYouColection(null);

        9
  •  5
  •   Tiina    7 年前

    Cascade.All Role Permission manyToMany

        Permission p = new Permission();
        p.setName("help");
        Permission p2 = new Permission();
        p2.setName("self_info");
        p = (Permission)crudRepository.save(p); // returned p has id filled in.
        p2 = (Permission)crudRepository.save(p2); // so does p2.
        Role role = new Role();
        role.setAvailable(true);
        role.setDescription("a test role");
        role.setRole("admin");
        List<Permission> pList = new ArrayList<Permission>();
        pList.add(p);
        pList.add(p2);
        role.setPermissions(pList);
        crudRepository.save(role);
    

        10
  •  4
  •   manikanta    8 年前

    null

    Employee emp = new Employee();
    emp.setDept(new Dept(deptId)); // --> when deptId PKID is null, same error will be thrown
    // calls to other setters...
    em.persist(emp);
    

    deptId

        11
  •  3
  •   acpuma    9 年前

    getSession().save(object)
    

    getSession().saveOrUpdate(object) 
    
        12
  •  3
  •   Hossein Nasr    8 年前

    merge

    merge(A);
    B.setA(A);
    persist(B);
    

    A

    A=merge(A);//difference is here
    B.setA(A);
    persist(B);
    
        13
  •  1
  •   Jeremy Goodell    8 年前

    this.lastName = lastName;
    this.isAdmin = isAdmin;
    this.accountStatus = "Active";
    this.setNewPassword(password);
    this.timeJoin = new Date();
    create();
    

    this.lastName = lastName;
    this.isAdmin = isAdmin;
    this.accountStatus = "Active";
    this.timeJoin = new Date();
    create();
    this.setNewPassword(password);
    
        14
  •  1
  •   Hossein Nasr    8 年前

    A B C

        15
  •  1
  •   DevWithSigns    7 年前

    @Transactional

        16
  •  1
  •   Ralf    6 年前

    org.hibernate.TransientPropertyValueException 
    

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

        17
  •  1
  •   Du-Lacoste    6 年前

    Exception in thread "main" java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.model.Car_OneToMany
    

        @OneToMany(cascade = {CascadeType.ALL})
        @Column(name = "ListOfCarsDrivenByDriver")
        private List<Car_OneToMany> listOfCarsBeingDriven = new ArrayList<Car_OneToMany>();
    

    @OneToMany(cascade = {CascadeType.ALL})

        18
  •  0
  •   Ingo Karkat    8 年前

    insert into Department (name, numOfemp, Depno) values (?, ?, ?)
    Hibernate: insert into Employee (SSN, dep_Depno, firstName, lastName, middleName, empno) values (?, ?, ?, ?, ?, ?)
    
    Session session=sf.openSession();
            session.beginTransaction();
            session.save(dep);
            session.save(emp);
    
        19
  •  0
  •   pheromix    7 年前

    Department dept = (Department)session.load(Department.class, dept_code); // dept_code is from the jsp form which you get in the controller with @RequestParam String department
    employee.setDepartment(dept);
    
        20
  •  0
  •   Ravindra Kumar    7 年前

        @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;
            }
    
        }
    
        21
  •  0
  •   Tadele Ayelegn    6 年前

        22
  •  0
  •   CrisMon_01    6 年前

        23
  •  0
  •   Harsh Vardhan Singh Solanki    6 年前

    public class Reference implements Serializable {
    
        @Id
        @Column(precision=20, scale=0)
        private BigInteger id;
    
        @Temporal(TemporalType.TIMESTAMP)
        private Date modifiedOn;
    
        @OneToOne(mappedBy="reference")
        private ReferenceAdditionalDetails refAddDetails;
        . 
        .
        .
    }
    

    public class ReferenceAdditionalDetails implements Serializable{
    
        private static final long serialVersionUID = 1L;
    
        @Id
        @OneToOne
        @JoinColumn(name="reference",referencedColumnName="id")
        private Reference reference;
    
        private String preferedSector1;
        private String preferedSector2;
        .
        .
    
    }
    

    .
    .
    reference.setRefAddDetails(null);
    reference = referenceDao.create(reference);
    entityManager.flush();
    .
    .
    
        24
  •  0
  •   mad_fox    6 年前

    @Transactional

    @Transactional
    public someService() {
        Entity someEntity = new Entity();
        AssocaiatedEntity associatedEntity = new AssocaitedEntity();
        someEntity.setAssociatedEntity(associatedEntity);
        associatedEntity.setEntity(someEntity);
    
        // Performing any query was causing hibernate to attempt to persist the new entity. It would then throw an exception
        someDao.getSomething();
    
        entityDao.create(someEntity);
    }