|
| 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.test.annotations.derivedidentities.bidirectional; |
| 8 | + |
| 9 | +import java.io.Serializable; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.Objects; |
| 12 | +import java.util.Set; |
| 13 | +import javax.persistence.CascadeType; |
| 14 | +import javax.persistence.Entity; |
| 15 | +import javax.persistence.FetchType; |
| 16 | +import javax.persistence.Id; |
| 17 | +import javax.persistence.JoinColumn; |
| 18 | +import javax.persistence.ManyToOne; |
| 19 | +import javax.persistence.OneToMany; |
| 20 | + |
| 21 | +import org.hibernate.Hibernate; |
| 22 | + |
| 23 | +import org.hibernate.testing.TestForIssue; |
| 24 | +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 25 | +import org.junit.After; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; |
| 30 | +import static org.junit.Assert.assertEquals; |
| 31 | +import static org.junit.Assert.assertNotNull; |
| 32 | +import static org.junit.Assert.assertSame; |
| 33 | +import static org.junit.Assert.assertTrue; |
| 34 | + |
| 35 | +public class ManyToOneEagerDerivedIdFetchModeJoinTest extends BaseCoreFunctionalTestCase { |
| 36 | +private Foo foo; |
| 37 | + |
| 38 | +@Test |
| 39 | +@TestForIssue(jiraKey = "HHH-14466") |
| 40 | +public void testQuery() { |
| 41 | +doInHibernate( this::sessionFactory, session -> { |
| 42 | +Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo.id = :id" ) |
| 43 | +.setParameter( "id", foo.getId() ) |
| 44 | +.uniqueResult(); |
| 45 | +assertNotNull( newBar ); |
| 46 | +assertNotNull( newBar.getFoo() ); |
| 47 | +assertTrue( Hibernate.isInitialized( newBar.getFoo() ) ); |
| 48 | +assertEquals( foo.getId(), newBar.getFoo().getId() ); |
| 49 | +assertTrue( Hibernate.isInitialized( newBar.getFoo().getBars() ) ); |
| 50 | +assertEquals( 1, newBar.getFoo().getBars().size() ); |
| 51 | +assertSame( newBar, newBar.getFoo().getBars().iterator().next() ); |
| 52 | +assertEquals( "Some details", newBar.getDetails() ); |
| 53 | +}); |
| 54 | +} |
| 55 | + |
| 56 | +@Test |
| 57 | +@TestForIssue(jiraKey = "HHH-14466") |
| 58 | +public void testQueryById() { |
| 59 | + |
| 60 | +doInHibernate( this::sessionFactory, session -> { |
| 61 | +Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo = :foo" ) |
| 62 | +.setParameter( "foo", foo ) |
| 63 | +.uniqueResult(); |
| 64 | +assertNotNull( newBar ); |
| 65 | +assertNotNull( newBar.getFoo() ); |
| 66 | +assertTrue( Hibernate.isInitialized( newBar.getFoo() ) ); |
| 67 | +assertEquals( foo.getId(), newBar.getFoo().getId() ); |
| 68 | +assertTrue( Hibernate.isInitialized( newBar.getFoo().getBars() ) ); |
| 69 | +assertEquals( 1, newBar.getFoo().getBars().size() ); |
| 70 | +assertSame( newBar, newBar.getFoo().getBars().iterator().next() ); |
| 71 | +assertEquals( "Some details", newBar.getDetails() ); |
| 72 | +}); |
| 73 | +} |
| 74 | + |
| 75 | +@Test |
| 76 | +@TestForIssue(jiraKey = "HHH-14466") |
| 77 | +public void testFindByPrimaryKey() { |
| 78 | + |
| 79 | +doInHibernate( this::sessionFactory, session -> { |
| 80 | +Bar newBar = session.find( Bar.class, foo.getId() ); |
| 81 | +assertNotNull( newBar ); |
| 82 | +assertNotNull( newBar.getFoo() ); |
| 83 | +assertTrue( Hibernate.isInitialized( newBar.getFoo() ) ); |
| 84 | +assertEquals( foo.getId(), newBar.getFoo().getId() ); |
| 85 | +assertTrue( Hibernate.isInitialized( newBar.getFoo().getBars() ) ); |
| 86 | +assertEquals( 1, newBar.getFoo().getBars().size() ); |
| 87 | +assertSame( newBar, newBar.getFoo().getBars().iterator().next() ); |
| 88 | +assertEquals( "Some details", newBar.getDetails() ); |
| 89 | +}); |
| 90 | +} |
| 91 | + |
| 92 | +@Test |
| 93 | +@TestForIssue(jiraKey = "HHH-14466") |
| 94 | +public void testFindByInversePrimaryKey() { |
| 95 | + |
| 96 | +doInHibernate( this::sessionFactory, session -> { |
| 97 | +Foo newFoo = session.find( Foo.class, foo.getId() ); |
| 98 | +assertNotNull( newFoo ); |
| 99 | +assertNotNull( newFoo.getBars() ); |
| 100 | +assertTrue( Hibernate.isInitialized( newFoo.getBars() ) ); |
| 101 | +assertEquals( 1, newFoo.getBars().size() ); |
| 102 | +assertSame( newFoo, newFoo.getBars().iterator().next().getFoo() ); |
| 103 | +assertEquals( "Some details", newFoo.getBars().iterator().next().getDetails() ); |
| 104 | +}); |
| 105 | + |
| 106 | +} |
| 107 | + |
| 108 | +@Before |
| 109 | +public void setupData() { |
| 110 | +this.foo = doInHibernate( this::sessionFactory, session -> { |
| 111 | +Foo foo = new Foo(); |
| 112 | +foo.id = 1L; |
| 113 | +session.persist( foo ); |
| 114 | + |
| 115 | +Bar bar = new Bar(); |
| 116 | +bar.setFoo( foo ); |
| 117 | +bar.setDetails( "Some details" ); |
| 118 | + |
| 119 | +foo.getBars().add( bar ); |
| 120 | + |
| 121 | +session.persist( bar ); |
| 122 | + |
| 123 | +session.flush(); |
| 124 | + |
| 125 | +assertNotNull( foo.getId() ); |
| 126 | +assertEquals( foo.getId(), bar.getFoo().getId() ); |
| 127 | + |
| 128 | +return foo; |
| 129 | +}); |
| 130 | +} |
| 131 | + |
| 132 | +@After |
| 133 | +public void cleanupData() { |
| 134 | +doInHibernate( this::sessionFactory, session -> { |
| 135 | +session.delete( session.find( Foo.class, foo.id ) ); |
| 136 | +}); |
| 137 | +this.foo = null; |
| 138 | +} |
| 139 | + |
| 140 | +@Override |
| 141 | +protected Class<?>[] getAnnotatedClasses() { |
| 142 | +return new Class<?>[] { |
| 143 | +Foo.class, |
| 144 | +Bar.class, |
| 145 | +}; |
| 146 | +} |
| 147 | + |
| 148 | +@Entity(name = "Foo") |
| 149 | +public static class Foo { |
| 150 | + |
| 151 | +@Id |
| 152 | +private Long id; |
| 153 | + |
| 154 | +private String name; |
| 155 | + |
| 156 | +@OneToMany(mappedBy = "foo", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true) |
| 157 | +private Set<Bar> bars = new HashSet<>(); |
| 158 | + |
| 159 | +public Long getId() { |
| 160 | +return id; |
| 161 | +} |
| 162 | + |
| 163 | +public void setId(Long id) { |
| 164 | +this.id = id; |
| 165 | +} |
| 166 | + |
| 167 | +public Set<Bar> getBars() { |
| 168 | +return bars; |
| 169 | +} |
| 170 | + |
| 171 | +public void setBars(Set<Bar> bars) { |
| 172 | +this.bars = bars; |
| 173 | +} |
| 174 | + |
| 175 | +@Override |
| 176 | +public boolean equals(Object o) { |
| 177 | +if ( this == o ) { |
| 178 | +return true; |
| 179 | +} |
| 180 | +if ( o == null || getClass() != o.getClass() ) { |
| 181 | +return false; |
| 182 | +} |
| 183 | +Foo foo = (Foo) o; |
| 184 | +return id.equals( foo.id ); |
| 185 | +} |
| 186 | + |
| 187 | +@Override |
| 188 | +public int hashCode() { |
| 189 | +return Objects.hash( id ); |
| 190 | +} |
| 191 | +} |
| 192 | + |
| 193 | +@Entity(name = "Bar") |
| 194 | +public static class Bar implements Serializable { |
| 195 | + |
| 196 | +@Id |
| 197 | +@ManyToOne(fetch = FetchType.EAGER) |
| 198 | +@JoinColumn(name = "BAR_ID") |
| 199 | +private Foo foo; |
| 200 | + |
| 201 | +private String details; |
| 202 | + |
| 203 | +public Foo getFoo() { |
| 204 | +return foo; |
| 205 | +} |
| 206 | + |
| 207 | +public void setFoo(Foo foo) { |
| 208 | +this.foo = foo; |
| 209 | +} |
| 210 | + |
| 211 | +public String getDetails() { |
| 212 | +return details; |
| 213 | +} |
| 214 | + |
| 215 | +public void setDetails(String details) { |
| 216 | +this.details = details; |
| 217 | +} |
| 218 | + |
| 219 | +@Override |
| 220 | +public boolean equals(Object o) { |
| 221 | +if ( this == o ) { |
| 222 | +return true; |
| 223 | +} |
| 224 | +if ( o == null || getClass() != o.getClass() ) { |
| 225 | +return false; |
| 226 | +} |
| 227 | +Bar bar = (Bar) o; |
| 228 | +return foo.equals( bar.foo ); |
| 229 | +} |
| 230 | + |
| 231 | +@Override |
| 232 | +public int hashCode() { |
| 233 | +return Objects.hash( foo ); |
| 234 | +} |
| 235 | +} |
| 236 | +} |
0 commit comments