Hibernate merge org.hibernate.id.IdentifierGenerationException:尝试从null一对一属性中分配id

时间:2014-07-15 11:06:17

标签: java hibernate hibernate-mapping

我遇到Hibernate合并的奇怪问题,一切看起来都不错。

实体豆:

@Entity
@Table(name = "EMPLOYEE")
@Access(value=AccessType.FIELD)
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "emp_id")
private long id;

@Column(name = "emp_name")
private String name;

@Column(name = "emp_salary")
private double salary;

@OneToOne(mappedBy = "employee")
@Cascade(value = org.hibernate.annotations.CascadeType.ALL)
private Address address;

//getter setters
}


@Entity
@Table(name = "ADDRESS")
@Access(value=AccessType.FIELD)
public class Address {

@Id
@Column(name = "emp_id", unique = true, nullable = false)
@GeneratedValue(generator = "gen")
@GenericGenerator(name = "gen", strategy = "foreign", parameters = { @Parameter(name = "property", value = "employee") })
private long id;

@Column(name = "address_line1")
private String addressLine1;

@Column(name = "zipcode")
private String zipcode;

@Column(name = "city")
private String city;

@OneToOne
@PrimaryKeyJoinColumn
private Employee employee;

主类代码段:

    Employee emp9 = getTestEmployee();
    Session session4 = sessionFactory.openSession();
    Transaction tx9 = session4.beginTransaction();
    Employee emp10 = (Employee) session4.merge(emp9); // throws Exception
    System.out.println(emp9 == emp10); // 
    emp9.setName("Test");
    emp10.setName("Kumar");
    tx9.commit();

public static Employee getTestEmployee() {
    Employee emp = new Employee();
    Address add = new Address();
    emp.setName("Test Emp");
    emp.setSalary(1000);
    add.setAddressLine1("Test address1");
    add.setCity("Test City");
    add.setZipcode("12121");
    emp.setAddress(add);
    add.setEmployee(emp);
    return emp;
}

正如您所看到的,我在Address对象中设置了employee字段,所以我不确定为什么它会抛出异常。

Hibernate: insert into EMPLOYEE (emp_name, emp_salary) values (?, ?)
Exception in thread "main" org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.journaldev.hibernate.model.Address.employee]
    at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:98)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
    at org.hibernate.event.internal.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:236)
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:216)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:154)
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:886)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:868)
    at org.hibernate.engine.spi.CascadingActions$6.cascade(CascadingActions.java:277)
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118)
    at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:460)
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:220)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:154)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76)
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:876)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:858)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:863)
    at com.journaldev.hibernate.main.HibernateSaveExample.main(HibernateSaveExample.java:107)

注意:如果我使用persist,它可以正常工作。

1 个答案:

答案 0 :(得分:0)

是的,因为合并除了id之外。您的实体未保存,因此未生成ID。您必须在修改之前保存实体。