From 194293330b7b53b6703d153bb8a683a240dfefce Mon Sep 17 00:00:00 2001 From: Jay Paulsen Date: Wed, 1 May 2019 17:08:16 -0500 Subject: [PATCH] HHH-13380 Test for the issue --- ...OneToManyWithEqualsImplementationTest.java | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/LazyOneToManyWithEqualsImplementationTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/LazyOneToManyWithEqualsImplementationTest.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/LazyOneToManyWithEqualsImplementationTest.java new file mode 100644 index 0000000000..68dc27fca5 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazy/LazyOneToManyWithEqualsImplementationTest.java @@ -0,0 +1,128 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.bytecode.enhancement.lazy; + +import org.hibernate.annotations.LazyToOne; +import org.hibernate.annotations.LazyToOneOption; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.persistence.Basic; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.junit.Assert.assertEquals; + +@TestForIssue(jiraKey = "HHH-13380") +@RunWith( BytecodeEnhancerRunner.class ) +public class LazyOneToManyWithEqualsImplementationTest + extends BaseEntityManagerFunctionalTestCase { + + private Long personId; + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Person.class, Course.class }; + } + + @Before + public void setUp() { + doInJPA( this::entityManagerFactory, entityManager -> { + Person p = new Person(); + entityManager.persist(p); + personId = p.getId(); + + Course c1 = new Course( "First Course", p ); + p.getCourses().add( c1 ); + entityManager.persist( c1 ); + + Course c2 = new Course("Second Course", p ); + p.getCourses().add( c2 ); + entityManager.persist( c2 ); + }); + } + + + @Test + public void testRetrievalOfOneToMany() { + doInJPA(this::entityManagerFactory, entityManager -> { + Person p = entityManager.find( Person.class, personId ); + + Set courses = p.getCourses(); + assertEquals( courses.size(), 2 ); + }); + } + + @Entity(name = "Person") + public static class Person { + + @Id + @GeneratedValue() + private Long id; + public Long getId() { + return id; + } + + @OneToMany(mappedBy="person", fetch = FetchType.LAZY) + private Set courses = new HashSet<>(); + public Set getCourses() { return courses; } + + } + + @Entity(name = "Course") + public static class Course { + + @Id + @GeneratedValue() + private Long id; + public Long getId() { return id; } + + @Basic + private String title; + public String getTitle() { return title; } + + @ManyToOne(fetch = FetchType.LAZY) + @LazyToOne(LazyToOneOption.NO_PROXY) + private Person person; + public Person getPerson() { return person; } + + protected Course() {} + public Course(String title, Person person) { + this.title = title; + this.person = person; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) return true; + if ( o == null || getClass() != o.getClass() ) return false; + Course course = (Course) o; + return title.equals( course.title ) && + person.equals( course.person ); + } + + @Override + public int hashCode() { + return Objects.hash( title, person ); + } + } + +}