2

我有以下实体在休眠插入期间工作正常。

用户.java

@Entity
@Table
@Inheritance(strategy = InheritanceType.JOINED)
public class User extends PersistentObject {

    private static final long serialVersionUID = -1803375723276957167L;

    // commenting this association allows update to work successfully.
    @OneToOne(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
    private ContactInfo contactInfo;
    private String name;
}

然后,我尝试对我认为已分离的实体执行更新,并在cascadeOnUpdate.

例外

java.lang.NullPointerException
    at org.hibernate.engine.spi.EntityEntry.getLoadedValue(EntityEntry.java:274)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:236)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:132)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.cascadeOnUpdate(DefaultSaveOrUpdateEventListener.java:361)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:335)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:239)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:109)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:724)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:716)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:712)
    at com.myapp.dao.GenericHibernateDAO.save(GenericHibernateDAO.java:279)
    at com.myapp.service.StaffInfoServiceImpl.storeInfo(StaffInfoServiceImpl.java:74)
    at com.myapp.service.StaffInfoServiceImpl.storeInfo(StaffInfoServiceImpl.java:1)

更新

联系信息.java

@Entity
@Table
@DynamicUpdate
public class ContactInfo implements Serializable {

    private static final long serialVersionUID = 4485388013971134422L;

    @Column
    @Length(min = 3, max = 50)
    private String currentCity;

    @ManyToOne
    @JoinColumn(name = "current_country_id")
    private Country currentCountry;

    @Column(columnDefinition = "char(10)")
    private String currentPostCode;

    @Column
    @Length(min = 3, max = 50)
    private String currentState;

    @Column
    @Length(min = 3, max = 50)
    private String currentStreetOne;

    @Column
    @Length(min = 3, max = 50)
    private String currentStreetThree;

    @Column
    @Length(min = 3, max = 50)
    private String currentStreetTwo;

    @Column
    @Email
    private String email;

    @Column
    @Length(max = 20)
    private String fax;

    @Column
    @Length(max = 20)
    private String homePhone;

    @GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "user"))
    @Id
    @GeneratedValue(generator = "generator")
    @Column(name = "user_id", unique = true, nullable = false)
    private Long id;

    @Column
    @Length(max = 20)
    private String mobilePhone;

    @Column
    @Length(max = 20)
    private String officePhone;

    @Column
    @Length(min = 3, max = 50)
    private String permanentCity;

    @ManyToOne
    @JoinColumn(name = "permanent_country_id")
    private Country permanentCountry;

    @Column(columnDefinition = "char(10)")
    private String permanentPostCode;

    @Column
    @Length(min = 3, max = 50)
    private String permanentState;

    @Column
    @Length(min = 3, max = 50)
    private String permanentStreetOne;

    @Column
    @Length(min = 3, max = 50)
    private String permanentStreetThree;

    @Column
    @Length(min = 3, max = 50)
    private String permanentStreetTwo;

    @OneToOne(fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumn
    private User user;

    @Version
    @Column(name = "version")
    private Integer version = 0;   
}

我的问题是为什么在插入期间没有抛出异常,而在更新期间却是?我在这里错过了什么?

4

2 回答 2

5

我认为这个错误是因为关联中的orphanRemoval = true属性@OneToOne

当您更新对Usernull引用时ContactInfo,休眠可能会假设ContactInfo引用已设置为,然后由于 orphanRemoval 语义null,它会尝试删除“存在(但从未存在) ”的对象。

如果我关于这个错误的逻辑是正确的,那么休眠中存在一个错误。也许这在较新版本的休眠中已修复。

于 2013-06-27T04:08:38.893 回答
4

这是休眠的错误,已在版本中修复4.1.7
您的版本的临时解决方法,使用session.merge而不是session.saveOrUpdate.

问题已在此任务下修复

于 2013-06-27T05:37:26.940 回答