如何在复合主要内部使用生成的序列:Hibernate

时间:2013-11-26 22:12:40

标签: hibernate jpa hibernate-mapping hibernate-annotations

我在复合主键中使用序列生成器,使用它的正确格式是什么? 当我持久保存两个对象时,我有一个org.hibernate.NonUniqueObjectException。

提前致谢。

实体

@Entity
@Table(name="EXT_TB_TITULAR")
public class ExtTbTitular implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private ExtTbTitularPK id;

@Column(name="CTITU_NOMB")
private String ctituNomb;

public ExtTbTitular() {
}

public ExtTbTitularPK getId() {
    return this.id;
}

public void setId(ExtTbTitularPK id) {
    this.id = id;
}

public String getCtituNomb() {
    return this.ctituNomb;
}

public void setCtituNomb(String ctituNomb) {
    this.ctituNomb = ctituNomb;
}
}

复合类:

   @Embeddable
   public class ExtTbTitularPK implements Serializable {
    private static final long serialVersionUID = 1L;

    private long ntituCodigo;

    private long numeSecue;

    private long solicitudId;

    public ExtTbTitularPK() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "entity_titular")
    @SequenceGenerator(name = "entity_titular", sequenceName = "SEQ_EXT_TB_TITULAR",                                allocationSize = 1)
    @Column(name = "NTITU_CODIGO", unique = true, nullable = false)
    public long getNtituCodigo() {
        return this.ntituCodigo;
    }
    //ETC..............

方式

   ExtTbTitulartitular titular = new ExtTbTitular();
   ExtTbTitularPK id = new ExtTbTitularPK(); 
   titular.setId(id);
   titular.getId().setSolicitudId(bean.getSolicitud().getSolicitudId());
   titular.getId().setNumeSecue(bean.getNumeSecue);
   sessionFactory.getCurrentSession().persist(titular);

0 个答案:

没有答案