Skip to content

Commit

Permalink
HHH-19017: Adds Assertions to Unit Test
Browse files Browse the repository at this point in the history
  • Loading branch information
jimsimon-wk committed Jan 16, 2025
1 parent 52e644a commit 5b54685
Showing 1 changed file with 92 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.lazyonetoone;

import java.util.List;
Expand All @@ -14,80 +18,98 @@
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.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.hibernate.Hibernate.isInitialized;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@DomainModel(
annotatedClasses = {
LazyOneToOneWithEntityGraphTest.Company.class,
LazyOneToOneWithEntityGraphTest.Employee.class,
LazyOneToOneWithEntityGraphTest.Project.class
}
annotatedClasses = {
LazyOneToOneWithEntityGraphTest.Company.class,
LazyOneToOneWithEntityGraphTest.Employee.class,
LazyOneToOneWithEntityGraphTest.Project.class
}
)
@SessionFactory
@BytecodeEnhanced(runNotEnhancedAsWell = true)
public class LazyOneToOneWithEntityGraphTest {
@Test
void reproducerTest(SessionFactoryScope scope) {
scope.inTransaction(session -> {
// Create company
Company company = new Company();
company.id = 1L;
company.name = "Hibernate";
session.persist(company);

// Create project
Project project = new Project();
project.id = 1L;
session.persist(project);

// Create employee
Employee employee = new Employee();
employee.id = 1L;
employee.company = company;
employee.projects = List.of(project);
session.persist(employee);
});

scope.inTransaction(session -> {
// Load employee using entity graph
Employee employee = session.createQuery(
"select e from Employee e where e.id = :id", Employee.class)
.setParameter("id", 1L)
.setHint("javax.persistence.fetchgraph", session.getEntityGraph("employee.projects"))
.getSingleResult();
});

// No assertions here, because this test trigger an exception with Hibernate 6.6.4
}

@Entity(name = "Company")
public static class Company {
@Id
private Long id;

private String name;
}

@Entity(name = "Employee")
@NamedEntityGraph(
name = "employee.projects",
attributeNodes = @NamedAttributeNode("projects")
)
public static class Employee {
@Id
private Long id;

@OneToOne
@JoinColumn(name = "company_name", referencedColumnName = "name")
private Company company;

@OneToMany(fetch = FetchType.LAZY)
private List<Project> projects;
}

@Entity(name = "Project")
public static class Project {
@Id
private Long id;
}
}
@BeforeAll
void setUp(SessionFactoryScope scope) {
scope.inTransaction(session -> {
// Create company
Company company = new Company();
company.id = 1L;
company.name = "Hibernate";
session.persist(company);

// Create project
Project project = new Project();
project.id = 1L;
session.persist(project);

// Create employee
Employee employee = new Employee();
employee.id = 1L;
employee.company = company;
employee.projects = List.of(project);
session.persist(employee);
});
}

@AfterAll
void tearDown(SessionFactoryScope scope) {
scope.inTransaction(session -> {
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
});
}


@Test
void reproducerTest(SessionFactoryScope scope) {
scope.inTransaction(session -> {
// Load employee using entity graph
Employee employee = session.createQuery(
"select e from Employee e where e.id = :id", Employee.class)
.setParameter("id", 1L)
.setHint("javax.persistence.fetchgraph", session.getEntityGraph("employee.projects"))
.getSingleResult();

assertTrue(isInitialized(employee.projects));
assertEquals("Hibernate", employee.company.name);
});
}

@Entity(name = "Company")
public static class Company {
@Id
private Long id;

private String name;
}

@Entity(name = "Employee")
@NamedEntityGraph(
name = "employee.projects",
attributeNodes = @NamedAttributeNode("projects")
)
public static class Employee {
@Id
private Long id;

@OneToOne
@JoinColumn(name = "company_name", referencedColumnName = "name")
private Company company;

@OneToMany(fetch = FetchType.LAZY)
private List<Project> projects;
}

@Entity(name = "Project")
public static class Project {
@Id
private Long id;
}
}

0 comments on commit 5b54685

Please sign in to comment.