diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/Book.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/Book.java new file mode 100644 index 000000000000..0537925a7b55 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/Book.java @@ -0,0 +1,12 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.bytecode.enhancement.association; + +import jakarta.persistence.Entity; + + +@Entity +class Book extends Document{ +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/Document.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/Document.java new file mode 100644 index 000000000000..e59d0fa05b39 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/Document.java @@ -0,0 +1,35 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.bytecode.enhancement.association; + +import jakarta.persistence.ManyToOne; +import jakarta.persistence.MappedSuperclass; + + +@MappedSuperclass +abstract class Document { + + @ManyToOne + protected Person author; + + @ManyToOne + private Person translator; + + public Person getAuthor() { + return this.author; + } + + public void setAuthor(Person author) { + this.author = author; + } + + public Person getTranslator() { + return this.translator; + } + + public void setTranslator(Person translator) { + this.translator = translator; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyAssociationWithSuperclassTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyAssociationWithSuperclassTest.java new file mode 100644 index 000000000000..27aeb879bc66 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyAssociationWithSuperclassTest.java @@ -0,0 +1,54 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.bytecode.enhancement.association; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.FailureExpected; +import org.hibernate.testing.orm.junit.JiraKey; +import org.junit.jupiter.api.Test; + +import java.util.Set; + + +/** + * @author Thomas Junk + */ +@JiraKey("HHH-18827") +@BytecodeEnhanced +@EnhancementOptions(biDirectionalAssociationManagement = true) +public class OneToManyAssociationWithSuperclassTest { + + @Test + public void testEnhancementOneToManyMappedByProtected(){ + Person author = new Person(); + + Book book = new Book(); + assertNull(book.getAuthor()); + + author.setBooksWritten(Set.of(book)); + assertNotNull(book.getAuthor()); + assertEquals(author, book.getAuthor()); + } + + @Test + @FailureExpected( + jiraKey = "HHH-18827", + reason = "Byte-code enhancement fails to find private target field in MappedSuperclass") + public void testEnhancementOneToManyMappedByPrivate(){ + Person translator = new Person(); + + Book book = new Book(); + assertNull(book.getTranslator()); + + translator.setBooksTranslated(Set.of(book)); + assertNotNull(book.getTranslator()); + assertEquals(translator, book.getTranslator()); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/Person.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/Person.java new file mode 100644 index 000000000000..79ebc06bbe90 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/Person.java @@ -0,0 +1,36 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.bytecode.enhancement.association; + +import jakarta.persistence.Entity; +import jakarta.persistence.OneToMany; +import java.util.Set; + + +@Entity +class Person { + + @OneToMany(mappedBy = "author") + private Set booksWritten; + + @OneToMany(mappedBy = "translator") + private Set booksTranslated; + + public Set getBooksWritten() { + return this.booksWritten; + } + + public void setBooksWritten(Set booksWritten){ + this.booksWritten = booksWritten; + } + + public Set getBooksTranslated() { + return this.booksTranslated; + } + + public void setBooksTranslated(Set booksTranslated){ + this.booksTranslated = booksTranslated; + } +}