JPA: Die Identifizierung einer Instanz von [Entität] wurde von [ID1] zu [ID2] verändert.
Posted: 28 Feb 2025, 09:53
Ich versuche, ein Template -Backend -Projekt mit Spring Boot 6 mit Anmeldeoptionen und Benutzerrollen zu erstellen.
Code: Select all
package com.example.demo.model;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Entity(name = "user")
@Table(name = "user")
@Data
@EqualsAndHashCode(callSuper = true)
public class User extends AbstractEntity {
@Column(name = "mail", nullable = false, unique = true)
private String mail;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName;
// ...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_role_id", nullable = false)
private UserRole userRole;
}
< /code>
package com.example.demo.model;
import com.example.demo.enums.UserRoleName;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Set;
@Entity(name = "user_role")
@Table(name = "user_role")
@Data
@EqualsAndHashCode(callSuper = true)
public class UserRole extends AbstractEntity {
@Column(name = "name", nullable = false, unique = true)
@Enumerated(EnumType.STRING)
private UserRoleName name;
@Column(name = "access_level", nullable = false)
private Integer accessLevel;
@OneToMany(mappedBy = "userRole", fetch = FetchType.LAZY, orphanRemoval = true)
private Set users;
public UserRole() {
super();
}
}
< /code>
They both extend my AbstractEntity:
package com.example.demo.model;
import jakarta.persistence.*;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import java.util.Date;
import java.util.UUID;
@MappedSuperclass
@Data
public abstract class AbstractEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.UUID)
protected UUID id;
@Column()
@CreationTimestamp
protected Date created;
@Column()
@UpdateTimestamp
protected Date updated;
protected AbstractEntity() {
}
}
< /code>
When persisting a User, I want the relation to UserRole to change, if it was altered. Although, I run into a persistence problem:
org.hibernate.HibernateException: identifier of an instance of com.example.demo.model.UserRole was altered from 2ee569d5-91dc-4eb9-82f7-7ce7d76b158a to 150179b8-a235-452f-aaeb-a536174a0f94
< /code>
In the controller, I use a ModelMapper:
private User convertToEntity(UserDto userDto) {
User user;
if (userDto.getId() != null) {
user = userService.getById(UUID.fromString(userDto.getId()))
.orElseThrow(() -> new NotFoundException("User not found from ID"));
} else {
// ...
}
modelMapper.map(userDto, user);
return user;
}
< /code>
To give a quick overview of the process from REST controller to DB, I included a diagram:
[img]https://i.sstatic.net/iVmLJ4cj.png[/img]
I'm not strong in JPA, and I'm pretty sure the answer is right in front of me, but I'm not even sure where to start looking.
My dependencies in the pom.xml-file are as follows:
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-data-jpa
3.1.2
org.springframework.boot
spring-boot-starter-webflux
3.1.2
org.springframework.boot
spring-boot-starter-security
3.2.4
org.springframework.security
spring-security-crypto
6.2.3
org.glassfish.jaxb
jaxb-runtime
2.3.2
org.hibernate
hibernate-jpamodelgen
6.2.7.Final
org.modelmapper
modelmapper
3.2.0
org.projectlombok
lombok
1.18.30
provided
com.google.code.gson
gson
2.11.0
com.h2database
h2
runtime
com.mysql
mysql-connector-j
8.1.0
io.jsonwebtoken
jjwt
0.12.3
javax.xml.bind
jaxb-api
2.2.2