javax.persistence.EntityExistsException:具有相同标识符值的不同对象
javax.persistence.EntityExistsException: A different object with the same identifier value
以前我没有收到任何错误,但突然我开始收到错误:
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 数据库中的任何列创建序列 table。还有 esn 和 techid 的唯一键列的组合。我不想在我的数据库中创建新的序列列 table。
我想我不能使用 @GeneratedValue 并将其设置为 Auto 用于唯一列,否则我将出现休眠序列错误。
我也在每处理 1000 条记录后进行清除和刷新。
if (secuData != null) {
sessionFactory.getCurrentSession().persist(secuData);
}
i++;
if (i % 1000 == 0) {
sessionFactory.getCurrentSession().flush();
sessionFactory.getCurrentSession().clear();
}
您的实体 class 似乎正在为复合主键建模,但我只看到 2 @Id
并且没有使用必要的 @IdClass
注释。
要拥有复合主键,您可以使用 @IdClass
或 @Embeddable
键方法。
要继续使用 @IdClass
方法,您需要遵循一些规则,
- 复合主键class必须是public
- 它必须有一个无参数的构造函数
- 它必须定义 equals() 和 hashCode() 方法
- 必须是可序列化的
所以在你的情况下,class 看起来像,
@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 class,你的问题中提供的 once 似乎不正确。
以前我没有收到任何错误,但突然我开始收到错误:
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 数据库中的任何列创建序列 table。还有 esn 和 techid 的唯一键列的组合。我不想在我的数据库中创建新的序列列 table。
我想我不能使用 @GeneratedValue 并将其设置为 Auto 用于唯一列,否则我将出现休眠序列错误。
我也在每处理 1000 条记录后进行清除和刷新。
if (secuData != null) {
sessionFactory.getCurrentSession().persist(secuData);
}
i++;
if (i % 1000 == 0) {
sessionFactory.getCurrentSession().flush();
sessionFactory.getCurrentSession().clear();
}
您的实体 class 似乎正在为复合主键建模,但我只看到 2 @Id
并且没有使用必要的 @IdClass
注释。
要拥有复合主键,您可以使用 @IdClass
或 @Embeddable
键方法。
要继续使用 @IdClass
方法,您需要遵循一些规则,
- 复合主键class必须是public
- 它必须有一个无参数的构造函数
- 它必须定义 equals() 和 hashCode() 方法
- 必须是可序列化的
所以在你的情况下,class 看起来像,
@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 class,你的问题中提供的 once 似乎不正确。