JpaSystemException:尝试从null一对一属性中分配id

时间:2014-10-24 03:36:17

标签: spring hibernate playframework-2.0 one-to-one

我在Spring和Hibernate中使用Play框架,当我尝试将包含一对一关系的实体保存到另一个实体时出现以下错误

[play] Caused by: org.springframework.orm.jpa.JpaSystemException: attempted to assign id from null one-to-one property [org.famian.web.models.domains.StudyCriteria.ensatIdent]; nested exception is org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [org.famian.web.models.domains.StudyCriteria.ensatIdent]
 [play]     at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:244) ~[spring-orm-4.0.7.RELEASE.jar:4.0.7.RELEASE]
 [play]     at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:155) ~[spring-orm-4.0.7.RELEASE.jar:4.0.7.RELEASE]
 [play]     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417) ~[spring-orm-4.0.7.RELEASE.jar:4.0.7.RELEASE]
 [play]     at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.0.7.RELEASE.jar:4.0.7.RELEASE]
 [play]     at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.0.7.RELEASE.jar:4.0.7.RELEASE]
 [play] Caused by: org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [org.famian.web.models.domains.StudyCriteria.ensatIdent]
 [play]     at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:98) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
 [play]     at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
 [play]     at org.hibernate.jpa.event.internal.core.JpaMergeEventListener.saveWithGeneratedId(JpaMergeEventListener.java:73) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
 [play]     at org.hibernate.event.internal.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:271) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
 [play]     at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:251) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]    

2个实体共享一个公共主键,下面是我的类

@Entity
public class EnsatIdent implements Serializable {
    @EmbeddedId
    private EnsatIdentPK id;

    @OneToOne(mappedBy = "ensatIdent", cascade = CascadeType.ALL)
    private StudyCriteria studyCriteria;
}

@Entity
public class StudyCriteria {
    @EmbeddedId
    private EnsatIdentPK id;

    @MapsId
    @JoinColumns({
        @JoinColumn(name = "center_id"),
        @JoinColumn(name = "ensat_id")
    })
    @OneToOne
    private EnsatIdent ensatIdent;
}

@Embeddable
public class EnsatIdentPK implements Serializable {
    @Column(name = "center_id", columnDefinition = "varchar(5)")
    private String centerId;

    @Column(name = "ensat_id", columnDefinition = "int(11)")
    private Integer ensatId;
}

我的服务类是一个名为IdenService的类,它扩展了Spring PagingAndSortingRepository类。当我保存时,我打电话给

IdenService service.save(ensatIdent)

如果我在

上执行System.out.println()
  

ensatIdent.getStudyCriteria()

显示

  

StudyCriteria(id = EnsatIdentPK(centerId = GYMU,ensatId = 3))

如果实体仅包含Ident信息,则保存正常,但如果我在对象中添加studyCriteria,则会发生错误。我不明白发生了什么,请帮助。

1 个答案:

答案 0 :(得分:2)

解决了,问题是因为StudyCriteria对象没有EnsatIden对象的引用。

添加

ensatIdent.getStudyCriteria().setEnsatIdent(ensatIdent);

解决了错误