|
| 1 | +package org.hibernate.orm.test.association; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import org.hibernate.testing.TestForIssue; |
| 6 | +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import javax.persistence.Entity; |
| 10 | +import javax.persistence.Id; |
| 11 | +import javax.persistence.MappedSuperclass; |
| 12 | +import javax.persistence.OneToOne; |
| 13 | + |
| 14 | +@TestForIssue(jiraKey = "HHH-16378") |
| 15 | +public class GenericAssociationTest extends BaseCoreFunctionalTestCase { |
| 16 | + |
| 17 | + @Override |
| 18 | + protected Class<?>[] getAnnotatedClasses() { |
| 19 | + return new Class[] { |
| 20 | + Parent.class, |
| 21 | + AbstractChild.class, |
| 22 | + Child.class |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void testFindByParentId() { |
| 28 | + inTransaction( session -> { |
| 29 | + Parent parent = new Parent( 1L ); |
| 30 | + Child child = new Child( 2L ); |
| 31 | + child.setParent( parent ); |
| 32 | + session.persist( parent ); |
| 33 | + session.persist( child ); |
| 34 | + } ); |
| 35 | + |
| 36 | + inTransaction( session -> { |
| 37 | + assertThat( session.createQuery( "from Child where parent.id = :parentId", Child.class ) |
| 38 | + .setParameter( "parentId", 1L ) |
| 39 | + .list() ) |
| 40 | + .containsExactly( session.getReference( Child.class, 2L ) ); |
| 41 | + } ); |
| 42 | + } |
| 43 | + |
| 44 | + @Entity(name = "Parent") |
| 45 | + public static class Parent { |
| 46 | + @Id |
| 47 | + private Long id; |
| 48 | + |
| 49 | + public Parent() { |
| 50 | + } |
| 51 | + |
| 52 | + public Parent(Long id) { |
| 53 | + this.id = id; |
| 54 | + } |
| 55 | + |
| 56 | + public Long getId() { |
| 57 | + return this.id; |
| 58 | + } |
| 59 | + |
| 60 | + public void setId(Long id) { |
| 61 | + this.id = id; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @MappedSuperclass |
| 66 | + public abstract static class AbstractChild<T> { |
| 67 | + @OneToOne(optional = false) |
| 68 | + private T parent; |
| 69 | + |
| 70 | + public AbstractChild() { |
| 71 | + } |
| 72 | + |
| 73 | + public abstract Long getId(); |
| 74 | + |
| 75 | + public T getParent() { |
| 76 | + return this.parent; |
| 77 | + } |
| 78 | + |
| 79 | + public void setParent(T parent) { |
| 80 | + this.parent = parent; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Entity(name = "Child") |
| 85 | + public static class Child extends AbstractChild<Parent> { |
| 86 | + @Id |
| 87 | + protected Long id; |
| 88 | + |
| 89 | + public Child() { |
| 90 | + } |
| 91 | + |
| 92 | + public Child(Long id) { |
| 93 | + this.id = id; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public Long getId() { |
| 98 | + return this.id; |
| 99 | + } |
| 100 | + |
| 101 | + public void setId(Long id) { |
| 102 | + this.id = id; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments