Koen De Jaeger (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
) *commented* on HHH-16745 (
https://hibernate.atlassian.net/browse/HHH-16745?atlOrigin=eyJpIjoiZjZhZG...
)
Re: TransientObjectException when loading versioned entity from second-level cache with
@OneToOne LAZY mapping (
https://hibernate.atlassian.net/browse/HHH-16745?atlOrigin=eyJpIjoiZjZhZG...
)
I tried but can’t reproduce it in a unit test
*unit test can't reproduce*
package org.hibernate.bugs;
import jakarta.persistence.*;
import org.hibernate.annotations.Generated;
import org.hibernate.generator.EventType;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import java.io.Serializable;
import java.util.Date;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
/**
* This template demonstrates how to develop a test case for Hibernate ORM, using the Java
Persistence API.
*/
@DomainModel(annotatedClasses = {
JPAUnitTestCase.User.class,
JPAUnitTestCase.PAI.class,
})
@SessionFactory
public class JPAUnitTestCase {
private EntityManagerFactory entityManagerFactory;
@Before
public void init() {
entityManagerFactory =
Persistence.createEntityManagerFactory("templatePU");
}
@After
public void destroy() {
entityManagerFactory.close();
}
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(session -> {
User user1 = new User("user 1");
session.persist(user1);
user1.setSavedBy(user1);
session.merge(user1);
var user2 = new User("user 2");
user2.setSavedBy(user1);
session.persist(user2);
var user3 = new User("user 3");
user3.setSavedBy(user2);
session.persist(user3);
PAI pai1 = new PAI();
pai1.setCreatedBy(user1);
pai1.setLastModifiedBy(user2);
session.persist(pai1);
PAI pai2 = new PAI();
pai2.setCreatedBy(user2);
pai2.setLastModifiedBy(user3);
session.persist(pai2);
});
}
@AfterAll
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(session -> {
session.createMutationQuery("delete from PAI").executeUpdate();
session.createMutationQuery("delete from User").executeUpdate();
});
}
@org.junit.jupiter.api.Test
public void testSelectNormalUser(SessionFactoryScope scope) {
scope.inTransaction(session -> {
var users = session.createQuery("select p from User p",
User.class).getResultList();
assertThat(users.size()).isEqualTo(3);
});
}
@org.junit.jupiter.api.Test
public void testSelectNormalUserasd(SessionFactoryScope scope) {
scope.inTransaction(session -> {
var users = session.createQuery("select p from PAI p",
User.class).getResultList();
assertThat(users.size()).isEqualTo(2);
});
}
@Table(name = "users")
@Entity(name = "User")
public static class User implements Serializable {
@Id
@SequenceGenerator(name = "user_seq", sequenceName =
"user_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"user_seq")
private Long id;
@Column(updatable = false, insertable = false, columnDefinition =
"varchar")
private String uuid;
@Column(unique = true)
private String username;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "saved_by")
private User savedBy;
@Version
private Date modified;
public User() {
}
public User(String data) {
this.username = data;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public User getSavedBy() {
return savedBy;
}
public void setSavedBy(User savedBy) {
this.savedBy = savedBy;
}
public Date getModified() {
return modified;
}
public void setModified(Date modified) {
this.modified = modified;
}
}
@Table(name = "pais")
@Entity(name = "PAI")
public static class PAI implements Serializable {
@Id
@SequenceGenerator(name = "procedure_allocation_instance_seq",
sequenceName = "procedure_allocation_instance_seq", allocationSize = 1)
@GeneratedValue(generator = "procedure_allocation_instance_seq", strategy
= GenerationType.SEQUENCE)
private Long id;
@ManyToOne
private User createdBy;
@ManyToOne
private User lastModifiedBy;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public User getCreatedBy() {
return createdBy;
}
public void setCreatedBy(User createdBy) {
this.createdBy = createdBy;
}
public User getLastModifiedBy() {
return lastModifiedBy;
}
public void setLastModifiedBy(User lastModifiedBy) {
this.lastModifiedBy = lastModifiedBy;
}
}
}
(
https://hibernate.atlassian.net/browse/HHH-16745#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16745#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100227- sha1:8ffa416 )