|
| 1 | +package org.hibernate.orm.test.proxy; |
| 2 | + |
| 3 | +import org.hibernate.Hibernate; |
| 4 | +import org.hibernate.LazyInitializationException; |
| 5 | +import org.hibernate.query.Query; |
| 6 | + |
| 7 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 8 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 9 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 10 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 11 | +import org.junit.jupiter.api.BeforeAll; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import jakarta.persistence.Entity; |
| 15 | +import jakarta.persistence.FetchType; |
| 16 | +import jakarta.persistence.Id; |
| 17 | +import jakarta.persistence.Inheritance; |
| 18 | +import jakarta.persistence.InheritanceType; |
| 19 | +import jakarta.persistence.ManyToOne; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 23 | + |
| 24 | +@DomainModel( |
| 25 | + annotatedClasses = { |
| 26 | + DetachedProxyAsQueryParameterTest.Department.class, |
| 27 | + DetachedProxyAsQueryParameterTest.BasicDepartment.class, |
| 28 | + DetachedProxyAsQueryParameterTest.SpecialDepartment.class, |
| 29 | + DetachedProxyAsQueryParameterTest.Employee.class |
| 30 | + } |
| 31 | +) |
| 32 | +@SessionFactory |
| 33 | +@JiraKey("HHH-17704") |
| 34 | +public class DetachedProxyAsQueryParameterTest { |
| 35 | + |
| 36 | + private static final Integer EMPLOYEE_ID = 1; |
| 37 | + |
| 38 | + @BeforeAll |
| 39 | + public void setUp(SessionFactoryScope scope) { |
| 40 | + scope.inTransaction( |
| 41 | + session -> { |
| 42 | + SpecialDepartment department = new SpecialDepartment( 2, "dep1" ); |
| 43 | + Employee employee = new Employee( EMPLOYEE_ID, "Fab", department ); |
| 44 | + session.persist( department ); |
| 45 | + session.persist( employee ); |
| 46 | + } |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testQuery(SessionFactoryScope scope) { |
| 52 | + Employee employee = scope.fromTransaction( |
| 53 | + session -> |
| 54 | + session.find( Employee.class, EMPLOYEE_ID ) |
| 55 | + ); |
| 56 | + assertFalse( Hibernate.isInitialized( employee.getDepartment() ) ); |
| 57 | + scope.inSession( |
| 58 | + session -> { |
| 59 | + Query<Employee> query = session.createQuery( |
| 60 | + "select e from Employee e where e.department = :department", |
| 61 | + Employee.class |
| 62 | + ); |
| 63 | + query.setParameter( "department", employee.getDepartment() ); |
| 64 | + query.list(); |
| 65 | + } |
| 66 | + ); |
| 67 | + scope.inSession( |
| 68 | + session -> { |
| 69 | + session.createQuery( |
| 70 | + "select d from Department d where d = :department" ) |
| 71 | + .setParameter( "department", employee.getDepartment() ).list(); |
| 72 | + } |
| 73 | + ); |
| 74 | + |
| 75 | + |
| 76 | + assertThrows( |
| 77 | + LazyInitializationException.class, () -> { |
| 78 | + scope.inSession( |
| 79 | + session -> { |
| 80 | + // In order to validate the parameter and check that it is an instance of SpecialDepartment |
| 81 | + // the Department proxy have to be initialized but being a detached instance |
| 82 | + // a LazyInitializationException is thrown |
| 83 | + session.createQuery( |
| 84 | + "select d from SpecialDepartment d where d = :department" |
| 85 | + ).setParameter( "department", employee.getDepartment() ).list(); |
| 86 | + } |
| 87 | + ); |
| 88 | + } |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + @Entity(name = "Department") |
| 93 | + @Inheritance(strategy = InheritanceType.SINGLE_TABLE) |
| 94 | + public static class Department { |
| 95 | + @Id |
| 96 | + private Integer id; |
| 97 | + |
| 98 | + public Department() { |
| 99 | + } |
| 100 | + |
| 101 | + public Department(Integer id) { |
| 102 | + this.id = id; |
| 103 | + } |
| 104 | + |
| 105 | + public Integer getId() { |
| 106 | + return id; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Entity(name = "BasicDepartment") |
| 111 | + public static class BasicDepartment extends Department { |
| 112 | + |
| 113 | + |
| 114 | + private String name; |
| 115 | + |
| 116 | + public BasicDepartment() { |
| 117 | + } |
| 118 | + |
| 119 | + public BasicDepartment(Integer id, String name) { |
| 120 | + super( id ); |
| 121 | + this.name = name; |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + public String getName() { |
| 126 | + return name; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Entity(name = "SpecialDepartment") |
| 131 | + public static class SpecialDepartment extends BasicDepartment { |
| 132 | + public SpecialDepartment() { |
| 133 | + } |
| 134 | + |
| 135 | + public SpecialDepartment(Integer id, String name) { |
| 136 | + super( id, name ); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Entity(name = "Employee") |
| 141 | + public static class Employee { |
| 142 | + |
| 143 | + @Id |
| 144 | + private Integer id; |
| 145 | + |
| 146 | + private String name; |
| 147 | + |
| 148 | + @ManyToOne(fetch = FetchType.LAZY) |
| 149 | + private Department department; |
| 150 | + |
| 151 | + public Employee() { |
| 152 | + } |
| 153 | + |
| 154 | + public Employee(Integer id, String name, Department department) { |
| 155 | + this.id = id; |
| 156 | + this.name = name; |
| 157 | + this.department = department; |
| 158 | + } |
| 159 | + |
| 160 | + public Integer getId() { |
| 161 | + return id; |
| 162 | + } |
| 163 | + |
| 164 | + public String getName() { |
| 165 | + return name; |
| 166 | + } |
| 167 | + |
| 168 | + public Department getDepartment() { |
| 169 | + return department; |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments