gpt4 book ai didi

java - JPA 合并在刷新期间抛出异常键 'PRIMARY' 的重复条目

转载 作者:行者123 更新时间:2023-11-29 10:52:29 25 4
gpt4 key购买 nike

我有一个使用 hibernate 5.2 和 JPA 来处理数据库的 spring 服务器。

我们有一个收集大量数据并根据这些数据保存事件的流程。它每天运行,这些事件有可能改变值。有时,当我尝试保存这些时,会出现以下异常:

15:28:32.819 [pool-6-thread-3] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Duplicate entry 'VAL-below_avg-2017-04-07' for key 'PRIMARY'
Query is: insert into event_date (description, period, time, date, symbol, type) values (?, ?, ?, ?, ?, ?, ?)
Query is:
insert into event_date (description, period, time, date, symbol, type) values (?, ?, ?, ?, ?, ?, ?)
[org.hibernate.exception.ConstraintViolationException: could not execute statement]
15:28:32.822 [pool-6-thread-3] ERROR org.hibernate.internal.ExceptionMapperStandardImpl - HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [PRIMARY]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:278)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:504)

我们在这里没有做任何花哨的事情。我们处理数据。我们创建新的对象。我们将它们传递到 DAO 中的以下方法中:

public void saveOrUpdate(List<EventDate> events) {
for (EventDate e : events) {
try{
entityManager.merge(e);
}catch(Exception e1){
logger.error("Something broke!");
}
}
}

我已经解决了大约十几个类似的问题,但在所有这些问题中,问题似乎都是由与另一个对象的某种方向关系引起的。就我而言,不存在这样的事情。我还看到有些人认为这是没有 hashcode 和 equals 的问题,所以我实现了这些但没有效果。

我的实体类如下所示(有很多实体继承了EventDate,但它们大多只是到处添加一些元属性,我不认为问题出在它们上:

@Entity
@Table(name = "event_date")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.STRING, length=255)
@DiscriminatorOptions(insert=false)
@JsonIgnoreProperties(ignoreUnknown=true)
public abstract class EventDate implements Serializable {
private static final long serialVersionUID = 1L;

@EmbeddedId
private SymDateTypeId id;

@Column(columnDefinition=COLUMN_DATETIME)
private String time = "UNKNOWN";

@Column(columnDefinition=COLUMN_TEXT)
private String description;

@Column(columnDefinition=COLUMN_VARCHAR20)
private String period;

//Getters/Setters/Hashcode/Equals omitted...
}

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

@Column(nullable=false)
private String symbol = "UNKNOWN";
@Column(nullable=false)
private String date = "UNKNOWN";
@Column(nullable=false)
private String type;

//Getters and setters omitted...

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((date == null) ? 0 : date.hashCode());
result = prime * result + ((symbol == null) ? 0 : symbol.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SymDateTypeId other = (SymDateTypeId) obj;
if (date == null) {
if (other.date != null)
return false;
} else if (!date.equals(other.date))
return false;
if (symbol == null) {
if (other.symbol != null)
return false;
} else if (!symbol.equals(other.symbol))
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
}

最佳答案

Duplicate entry 'VAL-below_avg-2017-04-07' for key 'PRIMARY'

肯定表明您的数据库中已经有相关行。尝试找出为什么你保存事件两次,一切都很好。您还可以将日志条目添加到您的源中。

关于java - JPA 合并在刷新期间抛出异常键 'PRIMARY' 的重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43482815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com