diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/notfound/OptionalEagerMappedByNotFoundTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/notfound/OptionalEagerMappedByNotFoundTest.java index c0f5f4cd5c..08a1b32c28 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/notfound/OptionalEagerMappedByNotFoundTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/notfound/OptionalEagerMappedByNotFoundTest.java @@ -148,6 +148,7 @@ public class OptionalEagerMappedByNotFoundTest { private void checkResult(Person person) { assertNotNull( person ); + assertNotNull( person.getId() ); assertNull( person.getEmployment() ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/RefreshWithPropertyAccessAndCollectionMapManipulationInSetterMethodTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/RefreshWithPropertyAccessAndCollectionMapManipulationInSetterMethodTest.java new file mode 100644 index 0000000000..42558a9436 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/RefreshWithPropertyAccessAndCollectionMapManipulationInSetterMethodTest.java @@ -0,0 +1,150 @@ +package org.hibernate.orm.test.onetomany; + +import java.util.HashMap; +import java.util.Map; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Access; +import jakarta.persistence.AccessType; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.MapKeyColumn; +import jakarta.persistence.MapKeyEnumerated; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; + +@DomainModel( + annotatedClasses = { + RefreshWithPropertyAccessAndCollectionMapManipulationInSetterMethodTest.Car.class, + RefreshWithPropertyAccessAndCollectionMapManipulationInSetterMethodTest.CarName.class + } +) +@SessionFactory +@TestForIssue(jiraKey = "HHH-16272") +public class RefreshWithPropertyAccessAndCollectionMapManipulationInSetterMethodTest { + + @Test + public void testPersistAndRefresh(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Car car = new Car( "Audi" ); + session.persist( car ); + + session.flush(); + + session.refresh( car ); + } + ); + } + + @Entity(name = "Car") + @Table(name = "car") + public static class Car { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Access(AccessType.PROPERTY) + private Long id; + + private String name; + + @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) + @JoinColumn(name = "car_id", referencedColumnName = "id") + @MapKeyColumn(name = "country") + @MapKeyEnumerated(EnumType.ORDINAL) + Map brandNames = new HashMap<>(); + + public Car() { + for ( Country country : Country.values() ) { + brandNames.put( country, new CarName( null, country ) ); + } + } + + public Car(String name) { + this(); + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + for ( Country country : Country.values() ) { + CarName carName = brandNames.get( country ); + carName.carId = id; + } + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Map getBrandNames() { + return brandNames; + } + + public void setBrandNames(Map brandNames) { + this.brandNames = brandNames; + } + } + + @Entity(name = "CarName") + @Table(name = "carname") + public static class CarName { + + @Id + @Column(name = "car_id") + private Long carId; + + @Id + Country country; + + public CarName() { + } + + public CarName(Long carId, Country country) { + this.carId = carId; + this.country = country; + } + + public Long getCarId() { + return carId; + } + + public void setCarId(Long carId) { + this.carId = carId; + } + + public Country getCountry() { + return country; + } + + public void setCountry(Country country) { + this.country = country; + } + } + + public enum Country { + FRANCE, + ENGLAND, + CZECHIA, + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/RefreshWithPropertyAccessAndListManipulationInSetterMethodTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/RefreshWithPropertyAccessAndListManipulationInSetterMethodTest.java new file mode 100644 index 0000000000..4971b72fff --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/RefreshWithPropertyAccessAndListManipulationInSetterMethodTest.java @@ -0,0 +1,143 @@ +package org.hibernate.orm.test.onetomany; + +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Access; +import jakarta.persistence.AccessType; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; + +@DomainModel( + annotatedClasses = { + RefreshWithPropertyAccessAndListManipulationInSetterMethodTest.Car.class, + RefreshWithPropertyAccessAndListManipulationInSetterMethodTest.CarName.class + } +) +@SessionFactory +@TestForIssue(jiraKey = "HHH-16272") +public class RefreshWithPropertyAccessAndListManipulationInSetterMethodTest { + + @Test + public void testPersistAndRefresh(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Car car = new Car( "Audi" ); + session.persist( car ); + + session.flush(); + + session.refresh( car ); + } + ); + } + + @Entity(name = "Car") + @Table(name = "car") + public static class Car { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Access(AccessType.PROPERTY) + private Long id; + + private String name; + + @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) + List brandNames = new ArrayList<>(); + + public Car() { + for ( Country country : Country.values() ) { + brandNames.add( new CarName( null, country ) ); + } + } + + public Car(String name) { + this(); + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + for ( int i = 0; i < 3; i++ ) { + CarName carName = brandNames.get( i ); + carName.carId = id; + } + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getBrandNames() { + return brandNames; + } + + public void setBrandNames(List brandNames) { + this.brandNames = brandNames; + } + } + + @Entity(name = "CarName") + @Table(name = "carname") + public static class CarName { + + @Id + @Column(name = "car_id") + private Long carId; + + @Id + Country country; + + public CarName() { + } + + public CarName(Long carId, Country country) { + this.carId = carId; + this.country = country; + } + + public Long getCarId() { + return carId; + } + + public void setCarId(Long carId) { + this.carId = carId; + } + + public Country getCountry() { + return country; + } + + public void setCountry(Country country) { + this.country = country; + } + } + + public enum Country { + FRANCE, + ENGLAND, + CZECHIA, + } +}