+-
javax.persistence.EntityExistsException。不同的对象有相同的标识符值

以前我没有得到任何错误,但突然我开始得到错误。

javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [baag.betl.dbimporter.esmatrans.db.EquSecurityReferenceData#baag.db.SECU@59c70ceb]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:116) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:787) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:765) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]

在oracle数据库表中,没有为任何列创建序列。另外,esn和techid的唯一键列也有组合,我不想在数据库表中创建新的序列列。我不想在数据库表中创建新的序列列。

我想我不能使用@GeneratedValue并将其设置为Auto,否则我将得到休眠序列错误。

我也在每处理1000条记录后进行清除和刷新。

if (secuData != null) {
                sessionFactory.getCurrentSession().persist(secuData);
}

i++;
    if (i % 1000 == 0) {
                sessionFactory.getCurrentSession().flush();
                sessionFactory.getCurrentSession().clear();
    }
1
投票

要有一个复合主键,你可以有使用的 @IdClass@Embeddable 键方法。

要继续开展 @IdClass 办法,你需要遵循一些规则。

复合主密钥类必须是公共的 它必须有一个无参数的构造函数 它必须定义equals()和hashCode()方法。 它必须是可序列化的

所以,在你的情况下,该类将看起来像。

@Entity
@Table( name = "SECU" )
@IdClass( SECU.class )
public class SECU implements Serializable
{

    @Id
    @Column(name = "columnName") // use the correct column name if it varies from the variable name provided
    protected String esn;

    @Id
    @Column(name = "columnName") // use the correct column name if it varies from the variable name provided
    protected BigDecimal techrcrdid;

    @Column(name = "columnName") // use the correct column name if it varies from the variable name provided
    protected BigDecimal preTradLrgInScaleThrshld;

    @Column(name = "columnName") // use the correct column name if it varies from the variable name provided
    @Temporal( TemporalType.TIMESTAMP)
    protected LocalDateTime CreDt;

    @Column(name = "columnName") // use the correct column name if it varies from the variable name provided
    protected String fullnm;

    @Override
    public boolean equals( Object o )
    {
        if( this == o ) return true;
        if( !( o instanceof SECU ) ) return false;
        SECU secu = ( SECU ) o;
        return Objects.equals( esn, secu.esn ) &&
                       Objects.equals( techrcrdid, secu.techrcrdid ) &&
                       Objects.equals( preTradLrgInScaleThrshld, secu.preTradLrgInScaleThrshld ) &&
                       Objects.equals( CreDt, secu.CreDt ) &&
                       Objects.equals( fullnm, secu.fullnm );
    }

    @Override
    public int hashCode()
    {
        return Objects.hash( esn, techrcrdid, preTradLrgInScaleThrshld, CreDt, fullnm );
    }

    // getters and setters 
}

另外,请仔细检查每个实体类中的getter和setter,你的问题中提供的一次似乎不正确。

1
投票

你的实体类似乎是在模拟一个复合主键,但我只看到了两个 @Id 而不 @IdClass 使用的注释,这是必要的。