|
| 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 | + |
| 11 | +import javax.persistence.CascadeType; |
| 12 | +import javax.persistence.Entity; |
| 13 | +import javax.persistence.FetchType; |
| 14 | +import javax.persistence.GeneratedValue; |
| 15 | +import javax.persistence.Id; |
| 16 | +import javax.persistence.JoinColumn; |
| 17 | +import javax.persistence.OneToOne; |
| 18 | + |
| 19 | +import org.hibernate.annotations.Fetch; |
| 20 | +import org.hibernate.annotations.FetchMode; |
| 21 | + |
| 22 | +import org.hibernate.testing.FailureExpected; |
| 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 | + |
| 34 | +public class OneToOneEagerDerivedIdFetchModeSelectTest extends BaseCoreFunctionalTestCase { |
| 35 | + private Foo foo; |
| 36 | + |
| 37 | + @Test |
| 38 | + @TestForIssue(jiraKey = "HHH-14390") |
| 39 | + @FailureExpected(jiraKey = "HHH-14390") |
| 40 | + public void testQuery() { |
| 41 | + |
| 42 | + doInHibernate( this::sessionFactory, session -> { |
| 43 | + Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo.id = :id" ) |
| 44 | + .setParameter( "id", foo.getId() ) |
| 45 | + .uniqueResult(); |
| 46 | + assertNotNull( newBar ); |
| 47 | + assertNotNull( newBar.getFoo() ); |
| 48 | + assertEquals( foo.getId(), newBar.getFoo().getId() ); |
| 49 | + assertEquals( "Some details", newBar.getDetails() ); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + @TestForIssue(jiraKey = "HHH-14390") |
| 55 | + @FailureExpected(jiraKey = "HHH-14390") |
| 56 | + public void testQueryById() { |
| 57 | + |
| 58 | + doInHibernate( this::sessionFactory, session -> { |
| 59 | + Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo = :foo" ) |
| 60 | + .setParameter( "foo", foo ) |
| 61 | + .uniqueResult(); |
| 62 | + assertNotNull( newBar ); |
| 63 | + assertNotNull( newBar.getFoo() ); |
| 64 | + assertEquals( foo.getId(), newBar.getFoo().getId() ); |
| 65 | + assertEquals( "Some details", newBar.getDetails() ); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + @FailureExpected( jiraKey = "HHH-14389") |
| 71 | + public void testFindById() { |
| 72 | + |
| 73 | + doInHibernate( this::sessionFactory, session -> { |
| 74 | + Bar newBar = session.find( Bar.class, foo ); |
| 75 | + assertNotNull( newBar ); |
| 76 | + assertNotNull( newBar.getFoo() ); |
| 77 | + assertSame( foo, newBar.getFoo() ); |
| 78 | + assertEquals( foo.getId(), newBar.getFoo().getId() ); |
| 79 | + assertEquals( "Some details", newBar.getDetails() ); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + @TestForIssue(jiraKey = "HHH-14390") |
| 85 | + @FailureExpected(jiraKey = "HHH-14390") |
| 86 | + public void testFindByPrimaryKey() { |
| 87 | + |
| 88 | + doInHibernate( this::sessionFactory, session -> { |
| 89 | + Bar newBar = session.find( Bar.class, foo.getId() ); |
| 90 | + assertNotNull( newBar ); |
| 91 | + assertNotNull( newBar.getFoo() ); |
| 92 | + assertEquals( foo.getId(), newBar.getFoo().getId() ); |
| 93 | + assertEquals( "Some details", newBar.getDetails() ); |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + @Before |
| 98 | + public void setupData() { |
| 99 | + this.foo = doInHibernate( this::sessionFactory, session -> { |
| 100 | + Foo foo = new Foo(); |
| 101 | + session.persist( foo ); |
| 102 | + |
| 103 | + Bar bar = new Bar(); |
| 104 | + bar.setFoo( foo ); |
| 105 | + bar.setDetails( "Some details" ); |
| 106 | + |
| 107 | + foo.setBar( bar ); |
| 108 | + |
| 109 | + session.persist( bar ); |
| 110 | + |
| 111 | + session.flush(); |
| 112 | + |
| 113 | + assertNotNull( foo.getId() ); |
| 114 | + assertEquals( foo.getId(), bar.getFoo().getId() ); |
| 115 | + |
| 116 | + return foo; |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + @After |
| 121 | + public void cleanupData() { |
| 122 | + this.foo = null; |
| 123 | + doInHibernate( this::sessionFactory, session -> { |
| 124 | + session.createQuery( "delete from Bar" ); |
| 125 | + session.createQuery( "delete from Foo" ); |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + protected Class<?>[] getAnnotatedClasses() { |
| 131 | + return new Class<?>[] { |
| 132 | + Foo.class, |
| 133 | + Bar.class, |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + @Entity(name = "Foo") |
| 138 | + public static class Foo implements Serializable { |
| 139 | + |
| 140 | + @Id |
| 141 | + @GeneratedValue |
| 142 | + private Long id; |
| 143 | + |
| 144 | + private String name; |
| 145 | + |
| 146 | + @OneToOne(mappedBy = "foo", cascade = CascadeType.ALL) |
| 147 | + private Bar bar; |
| 148 | + |
| 149 | + public Long getId() { |
| 150 | + return id; |
| 151 | + } |
| 152 | + |
| 153 | + public void setId(Long id) { |
| 154 | + this.id = id; |
| 155 | + } |
| 156 | + |
| 157 | + public Bar getBar() { |
| 158 | + return bar; |
| 159 | + } |
| 160 | + |
| 161 | + public void setBar(Bar bar) { |
| 162 | + this.bar = bar; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Entity(name = "Bar") |
| 167 | + public static class Bar implements Serializable { |
| 168 | + |
| 169 | + @Id |
| 170 | + @OneToOne(fetch = FetchType.EAGER) |
| 171 | + @Fetch(FetchMode.SELECT) |
| 172 | + @JoinColumn(name = "BAR_ID") |
| 173 | + private Foo foo; |
| 174 | + |
| 175 | + private String details; |
| 176 | + |
| 177 | + public Foo getFoo() { |
| 178 | + return foo; |
| 179 | + } |
| 180 | + |
| 181 | + public void setFoo(Foo foo) { |
| 182 | + this.foo = foo; |
| 183 | + } |
| 184 | + |
| 185 | + public String getDetails() { |
| 186 | + return details; |
| 187 | + } |
| 188 | + |
| 189 | + public void setDetails(String details) { |
| 190 | + this.details = details; |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments