Skip to content

Commit 331383c

Browse files
committed
HHH-9302 - Add test for issue
1 parent cd2ac94 commit 331383c

File tree

5 files changed

+230
-0
lines changed

5 files changed

+230
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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.inheritancediscriminator.joinedsubclass;
8+
9+
import org.hibernate.Transaction;
10+
import org.hibernate.criterion.Restrictions;
11+
import org.hibernate.resource.transaction.spi.TransactionStatus;
12+
13+
import org.junit.Before;
14+
import org.junit.Test;
15+
16+
import org.hibernate.testing.TestForIssue;
17+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
18+
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertNull;
21+
import static org.junit.Assert.assertTrue;
22+
23+
/**
24+
* @author Andrea Boriero
25+
*/
26+
@TestForIssue(jiraKey = "HHH-9302")
27+
public class JoinedSubclassTest extends BaseCoreFunctionalTestCase {
28+
29+
private Long subSubEntityId;
30+
31+
@Override
32+
protected Class<?>[] getAnnotatedClasses() {
33+
return new Class[] {RootEntity.class, SubEntity.class, SubSubEntity.class, SubSubSubEntity.class};
34+
}
35+
36+
@Before
37+
public void setup() {
38+
session = openSession();
39+
Transaction transaction = session.beginTransaction();
40+
41+
final SubSubEntity subSubEntity = new SubSubEntity();
42+
final SubEntity subEntity = new SubSubEntity();
43+
try {
44+
session.save( subEntity );
45+
session.save( subSubEntity );
46+
transaction.commit();
47+
subSubEntityId = subSubEntity.getId();
48+
}
49+
finally {
50+
if ( transaction.getStatus() == TransactionStatus.ACTIVE ) {
51+
transaction.rollback();
52+
}
53+
}
54+
session.close();
55+
}
56+
57+
@Test
58+
public void shouldRetrieveSubEntity() {
59+
session = openSession();
60+
try {
61+
RootEntity loaded = session.get( SubEntity.class, subSubEntityId );
62+
assertNotNull( loaded );
63+
assertTrue( loaded instanceof SubSubEntity );
64+
}
65+
finally {
66+
session.close();
67+
}
68+
}
69+
70+
public void shouldNotRetrieveSubSubSubEntity() {
71+
session = openSession();
72+
try {
73+
SubSubSubEntity loaded = session.get( SubSubSubEntity.class, subSubEntityId );
74+
assertNull( loaded );
75+
}
76+
finally {
77+
session.close();
78+
}
79+
}
80+
81+
// Criteria
82+
83+
@Test
84+
public void shouldRetrieveSubSubEntityWithCriteria() {
85+
session = openSession();
86+
try {
87+
SubSubEntity loaded = (SubSubEntity) session.createCriteria( SubSubEntity.class )
88+
.add( Restrictions.idEq( subSubEntityId ) )
89+
.uniqueResult();
90+
assertNotNull( loaded );
91+
}
92+
finally {
93+
session.close();
94+
}
95+
}
96+
97+
@Test
98+
public void shouldNotRetrieveSubSubSubEntityWithCriteria() {
99+
session = openSession();
100+
try {
101+
SubSubSubEntity loaded = (SubSubSubEntity) session.createCriteria( SubSubSubEntity.class )
102+
.add( Restrictions.idEq( subSubEntityId ) )
103+
.uniqueResult();
104+
assertNull( loaded );
105+
}
106+
finally {
107+
session.close();
108+
}
109+
}
110+
111+
// HQL
112+
113+
@Test
114+
public void shouldRetrieveSubSubEntityWithHQL() {
115+
session = openSession();
116+
try {
117+
SubSubEntity loaded = (SubSubEntity) session.createQuery(
118+
"select se from SubSubEntity se where se.id = :id" )
119+
.setLong( "id", subSubEntityId )
120+
.uniqueResult();
121+
assertNotNull( loaded );
122+
}
123+
finally {
124+
session.close();
125+
}
126+
}
127+
128+
@Test
129+
public void shouldNotRetrieveSubSubSubEntityWithHQL() {
130+
session = openSession();
131+
try {
132+
SubSubSubEntity loaded = (SubSubSubEntity) session.createQuery(
133+
"select se from SubSubSubEntity se where se.id = :id" )
134+
.setLong( "id", subSubEntityId )
135+
.uniqueResult();
136+
assertNull( loaded );
137+
}
138+
finally {
139+
session.close();
140+
}
141+
}
142+
143+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.inheritancediscriminator.joinedsubclass;
8+
9+
import javax.persistence.DiscriminatorColumn;
10+
import javax.persistence.DiscriminatorValue;
11+
import javax.persistence.Entity;
12+
import javax.persistence.GeneratedValue;
13+
import javax.persistence.Id;
14+
import javax.persistence.Inheritance;
15+
import javax.persistence.InheritanceType;
16+
17+
/**
18+
* @author Andrea Boriero
19+
*/
20+
@Entity
21+
@Inheritance(strategy = InheritanceType.JOINED)
22+
@DiscriminatorColumn()
23+
@DiscriminatorValue("ROOT")
24+
public class RootEntity {
25+
@Id
26+
@GeneratedValue
27+
private Long id;
28+
29+
public Long getId() {
30+
return id;
31+
}
32+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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.inheritancediscriminator.joinedsubclass;
8+
9+
import javax.persistence.DiscriminatorValue;
10+
import javax.persistence.Entity;
11+
12+
/**
13+
* @author Andrea Boriero
14+
*/
15+
@Entity
16+
@DiscriminatorValue("SUB")
17+
public class SubEntity extends RootEntity {
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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.inheritancediscriminator.joinedsubclass;
8+
9+
import javax.persistence.DiscriminatorValue;
10+
import javax.persistence.Entity;
11+
12+
/**
13+
* @author Andrea Boriero
14+
*/
15+
@Entity
16+
@DiscriminatorValue("SUB-SUB")
17+
public class SubSubEntity extends SubEntity {
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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.inheritancediscriminator.joinedsubclass;
8+
9+
import javax.persistence.DiscriminatorValue;
10+
import javax.persistence.Entity;
11+
12+
/**
13+
* @author Andrea Boriero
14+
*/
15+
@Entity
16+
@DiscriminatorValue("SUB-SUB-SUB")
17+
public class SubSubSubEntity extends SubSubEntity {
18+
}

0 commit comments

Comments
 (0)