|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.lazyonetoone; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import jakarta.persistence.Entity; |
| 12 | +import jakarta.persistence.FetchType; |
| 13 | +import jakarta.persistence.Id; |
| 14 | +import jakarta.persistence.JoinColumn; |
| 15 | +import jakarta.persistence.NamedAttributeNode; |
| 16 | +import jakarta.persistence.NamedEntityGraph; |
| 17 | +import jakarta.persistence.OneToMany; |
| 18 | +import jakarta.persistence.OneToOne; |
| 19 | +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; |
| 20 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 21 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 22 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 23 | +import org.junit.jupiter.api.AfterAll; |
| 24 | +import org.junit.jupiter.api.BeforeAll; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import static org.hibernate.Hibernate.isInitialized; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 30 | + |
| 31 | +@DomainModel( |
| 32 | + annotatedClasses = { |
| 33 | + LazyOneToOneWithEntityGraphTest.Company.class, |
| 34 | + LazyOneToOneWithEntityGraphTest.Employee.class, |
| 35 | + LazyOneToOneWithEntityGraphTest.Project.class |
| 36 | + } |
| 37 | +) |
| 38 | +@SessionFactory |
| 39 | +@BytecodeEnhanced(runNotEnhancedAsWell = true) |
| 40 | +public class LazyOneToOneWithEntityGraphTest { |
| 41 | + @BeforeAll |
| 42 | + void setUp(SessionFactoryScope scope) { |
| 43 | + scope.inTransaction(session -> { |
| 44 | + // Create company |
| 45 | + Company company = new Company(); |
| 46 | + company.id = 1L; |
| 47 | + company.name = "Hibernate"; |
| 48 | + session.persist(company); |
| 49 | + |
| 50 | + // Create project |
| 51 | + Project project = new Project(); |
| 52 | + project.id = 1L; |
| 53 | + session.persist(project); |
| 54 | + |
| 55 | + // Create employee |
| 56 | + Employee employee = new Employee(); |
| 57 | + employee.id = 1L; |
| 58 | + employee.company = company; |
| 59 | + employee.projects = List.of(project); |
| 60 | + session.persist(employee); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + @AfterAll |
| 65 | + void tearDown(SessionFactoryScope scope) { |
| 66 | + scope.inTransaction(session -> { |
| 67 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + @Test |
| 73 | + void reproducerTest(SessionFactoryScope scope) { |
| 74 | + scope.inTransaction(session -> { |
| 75 | + // Load employee using entity graph |
| 76 | + Employee employee = session.createQuery( |
| 77 | + "select e from Employee e where e.id = :id", Employee.class) |
| 78 | + .setParameter("id", 1L) |
| 79 | + .setHint("javax.persistence.fetchgraph", session.getEntityGraph("employee.projects")) |
| 80 | + .getSingleResult(); |
| 81 | + |
| 82 | + assertTrue(isInitialized(employee.projects)); |
| 83 | + assertEquals("Hibernate", employee.company.name); |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + @Entity(name = "Company") |
| 88 | + public static class Company { |
| 89 | + @Id |
| 90 | + private Long id; |
| 91 | + |
| 92 | + private String name; |
| 93 | + } |
| 94 | + |
| 95 | + @Entity(name = "Employee") |
| 96 | + @NamedEntityGraph( |
| 97 | + name = "employee.projects", |
| 98 | + attributeNodes = @NamedAttributeNode("projects") |
| 99 | + ) |
| 100 | + public static class Employee { |
| 101 | + @Id |
| 102 | + private Long id; |
| 103 | + |
| 104 | + @OneToOne |
| 105 | + @JoinColumn(name = "company_name", referencedColumnName = "name") |
| 106 | + private Company company; |
| 107 | + |
| 108 | + @OneToMany(fetch = FetchType.LAZY) |
| 109 | + private List<Project> projects; |
| 110 | + } |
| 111 | + |
| 112 | + @Entity(name = "Project") |
| 113 | + public static class Project { |
| 114 | + @Id |
| 115 | + private Long id; |
| 116 | + } |
| 117 | +} |
0 commit comments