I'm working on a Java project using Spring Boot 2.6.6 and Spring Data JPA, Hibernate 5.6.7.Final and I'm getting a strange behaviour.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="<http://maven.apache.org/POM/4.0.0">
xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance">
xsi:schemaLocation="<http://maven.apache.org/POM/4.0.0> <http://maven.apache.org/xsd/maven-4.0.0.xsd">>
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<relativePath/>
<version>2.6.6</version>
</parent>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<artifactId>spring-boot-starter</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
<dependency>
<artifactId>spring-boot-starter-actuator</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
<dependency>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Consider following entities (getters and setters are omitted for brevity):
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(cascade = CascadeType.ALL,mappedBy = "user")
private List<ContactInfo> contactInfos = new ArrayList<>();
@Entity
@Table(name = "contact_info")
public class ContactInfo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String phoneNumber;
private String address;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "uid")
private User user;
}
I have already saved the user in the database and want to add a ContactInfo to it. I use Spring Data JPA and test is written with it.
@Transactional
public User saveUser() { saved the user in the database first
User user = new User();
user.setName("kevin");
return userRepository.save(user);
}
@Transactional
public User updateContactInfo(Long id) { User user = userRepository.findById(id).get();
ContactInfo contactInfo = new ContactInfo();
contactInfo.setUser(user);
user.getContactInfos().add(contactInfo);
User saveUser = userRepository.save(user);
System.out.println(saveUser.getContactInfos().size()); return user;
}
After that hibernate perform Merge operation on User entity and Cascade merge on contactInfos collection. I expect that saveUser will have one element in contactInfos collection, but it contains two the same elements. In database created only one. Can only create thumbnails for attached images Below is a log of my execution:
I found that other people had raised the same question, but it still didn't seem to be solved. The following links are for reference.
Has anyone could tell me what the problem is and how to fix it? I would really appreciate it.Thanks. |