spring - Hibernate collection with @ElementCollection contains duplicated elements in database
问题描述
Have a class.
@Entity
@Table(name="sessions")
@Component
public class Session {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ElementCollection
private Set<String> addedWords;
}
My flow is: get session one time. Add one word to addedWords
and save session, repeat it. The problem that when I check my database, there are a lot of duplications, at the same time I don't have any duplications in Java class. So the Set<String> addedWords
in class and this words in database not the same. Why I have this strange behavior and how to make things work well without any duplication? I use PostgreSQL. Saving method:
public synchronized void addWord(Session session, String word) {
session.getAddedWords().add(word);
sessionRepository.save(session);
}
Here spring config
spring:
jpa:
show-sql: true
hibernate:
ddl-auto: update
解决方案
推荐阅读
- java - Cassandra:Java 类在使用 cassandra 时抛出 InvalidDataAccessApiUsageException
- oracle - 查询最接近查询的上一个日期
- wordpress - Cloudflare 阻止来自 Wordpress 部署的混合内容
- python - 从 csv 文件创建新字典并将其用于多个功能
- azure-devops - 如何使用 Terraform 通过 Azure DevOps 创建 AWS 基础设施
- python - Python - 在遵循命令的同时写入文件
- python - 为什么列表中的项目似乎随机消失?
- python - 如何禁用 django 注册密码 help_text
- c# - 如何在函数中使用 Azure 门户调用 Azure Compute Rest API?
- python - 如何在 Python 2.7 中声明一个空字典的空字典?