Marcus
Marcus

Reputation: 3869

javax.persistence.EntityExistsException: A different object with the same identifier value

Previously i was not getting any error but suddenly i am started getting error :

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]

There is no sequence created for any column in oracle database table. Also there is combination of unique key column for esn and techid. I dont want to create new sequence column in my database table.

I think i cannot use @GeneratedValue and set it to Auto for unique columns otherwise i will get hibernate sequence error.

I am also doing clear and flush after every 1000 records processed.

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

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

Upvotes: 1

Views: 3831

Answers (2)

Klaus
Klaus

Reputation: 1731

To have a composite primary key, you can either have the use of @IdClass or @Embeddable key approach.

To continue with the @IdClass approach you need to follow some rules,

  • The composite primary key class must be public
  • It must have a no-arg constructor
  • It must define equals() and hashCode() methods
  • It must be Serializable

So in your case, the class would look like,

@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 
}

Also double check your getters and setters in each entity class, the once provided in your question seems incorrect.

Upvotes: 2

Shailendra
Shailendra

Reputation: 9102

Your entity class appears to be modelling a composite primary key, but I only see 2 @Id and no @IdClass annotation used which is necessary.

Upvotes: 1

Related Questions