Skip to content

Commit b40d125

Browse files
committed
HHH-14390 : Added test cases
1 parent 30fcb05 commit b40d125

File tree

4 files changed

+729
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)