Java JPA Repository.save具有唯一密钥约束
我正在使用JPA向我的POSTGRES db中插入一个内容。我最近在其中一个表上添加了一个唯一的键约束。该表如下所示:Java JPA Repository.save具有唯一密钥约束,java,postgresql,hibernate,spring-boot,spring-data-jpa,Java,Postgresql,Hibernate,Spring Boot,Spring Data Jpa,我正在使用JPA向我的POSTGRES db中插入一个内容。我最近在其中一个表上添加了一个唯一的键约束。该表如下所示: ______________________________________________ |Id| Action| Actor| Assignee| Date| Team| ---------------------------------------------- Id是主键,Action、Actor、Assignment和Date的组合是唯一键。当我从第三方
______________________________________________
|Id| Action| Actor| Assignee| Date| Team|
----------------------------------------------
Id是主键,Action、Actor、Assignment和Date的组合是唯一键。当我从第三方API获取JSON对象时,我使用以下方法将数据保存到数据库中
streamItemArray.parallelStream().forEach(streamItem -> {
try{
streamItemRepository.save(streamItem);
}catch(ConstraintViolationException e){
System.out.println("Issue with unique key");
System.out.println(e.getMessage());
}
catch (Exception e){
System.out.println("others");
System.out.println(e.getMessage());
}
});
代码最初运行了几次,现在根本没有运行
2017-07-31 15:14:04.811 WARN 7867 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 23505
2017-07-31 15:14:04.812 ERROR 7867 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: duplicate key value violates unique constraint "Unique_key"
Detail: Key (action, actor, assignee, date)=(Dismissed, Karen William, Karen William, 2017-07-21 01:19:05 IST) already exists.
2017-07-31 15:14:04.812 INFO 7867 --- [nio-8080-exec-4] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements
others
could not execute statement; SQL [n/a]; constraint [Unique_key]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
我的问题是:
提前感谢您的帮助。使用Set而不是列表来筛选重复项。您是否在enity中添加了唯一约束?是的,我添加了唯一约束-
@Table(name=“workflow”,uniqueConstraints={@UniqueConstraint(columnNames={“action”、“actor”、“Assignment”、“date”})
@AbdullahKhan-请详细说明您的评论。我不明白你的意思?将传入的json映射到一个对象类,比如X。根据类X中的业务逻辑,根据对象相等性正确重写equals和hashcode。执行与上面执行的相同的逻辑。