Hibernate没有在级联上生成子ID所有保存

时间:2016-02-10 15:49:32

标签: java hibernate

父类:

@Entity
@Table(name = "****")
@Audited
public class Ship
{
    @Id
    @Column(name = "id")
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid")
    private String              id;

    @OneToMany(mappedBy = "associatedShip", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<ShipLoadlineInformation>   loadlineInformation = new ArrayList<ShipLoadlineInformation>(0);

    .. with other fields constructors, getters and setters.
}

儿童班:

@Entity
@Table(name = "*********")
@Audited
public class ShipLoadlineInformation
{
    @Id
    @Column(name = "id")
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid")
    private String      id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "ASSOCIATED_SHIP")
    private Ship        associatedShip;

    .. with other fields, constructors, getter and setters
}

我将它保存在我的DAO中,如下所示:

    this.sessionFactory.getCurrentSession().save(ship);

当我尝试保存对象时,它会给我以下错误:

  

org.hibernate.NonUniqueObjectException:一个不同的对象   相同的标识符值已与会话关联:   [com.tms.model.transportMode.ShipLoadlineInformation#] at   org.hibernate.engine.internal.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:618)   〜[hibernate-core-4.3.11.Final.jar:4.3.11.Final] at   org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:301)   〜[hibernate-core-4.3.11.Final.jar:4.3.11.Final] at   org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:244)   〜[hibernate-core-4.3.11.Final.jar:4.3.11.Final] at   org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:109)   〜[hibernate-core-4.3.11.Final.jar:4.3.11.Final] at   org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)   〜[hibernate-core-4.3.11.Final.jar:4.3.11.Final] at   org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:684)   〜[hibernate-core-4.3.11.Final.jar:4.3.11.Final] at   org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:676)   〜[hibernate-core-4.3.11.Final.jar:4.3.11.Final] at   org.hibernate.engine.spi.CascadingActions $ 5.cascade(CascadingActions.java:235)   〜[hibernate-core-4.3.11.Final.jar:4.3.11.Final] at   org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350)   〜[hibernate-core-4.3.11.Final.jar:4.3.11.Final] at   org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293)   〜[hibernate-core-4.3.11.Final.jar:4.3.11.Final] at   org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)   〜[hibernate-core-4.3.11.Final.jar:4.3.11.Final] at   org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:379)   〜[hibernate-core-4.3.11.Final.jar:4.3.11.Final] at   org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:319)   〜[hibernate-core-4.3.11.Final.jar:4.3.11.Final] at   org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:296)   〜[hibernate-core-4.3.11.Final.jar:4.3.11.Final] at   org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)   〜[hibernate-core-4.3.11.Final.jar:4.3.11.Final] at   org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118)   〜[冬眠核-4.3.11.Final.jar:4.3.11.Final]

调试时,发现父类的id已生成。但是没有生成子类的id。

我认为这是原因。如果我错了,请纠正我。

感谢任何帮助。

感谢。

1 个答案:

答案 0 :(得分:1)

ShipLoadlineInformation集合中包含分离的Ship.loadlineInformation实例时,可能会发生此错误。保存save时,这种ship动作会以级联的方式生效。

解决问题的最简单方法是合并船舶(因此也可以将merge操作级联到loadlineInformation。)

所以,在你的DAO替换:

this.sessionFactory.getCurrentSession().save(ship)

使用:

ship = this.sessionFactory.getCurrentSession().merge(ship)