HHH-9302 - Add test for issue
This commit is contained in:
parent
17b9a06d6d
commit
4776ff0913
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.inheritancediscriminator.joinedsubclass;
|
||||
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.resource.transaction.spi.TransactionStatus;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Andrea Boriero
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-9302")
|
||||
public class JoinedSubclassTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
private Long subSubEntityId;
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[] {RootEntity.class, SubEntity.class, SubSubEntity.class, SubSubSubEntity.class};
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
session = openSession();
|
||||
Transaction transaction = session.beginTransaction();
|
||||
|
||||
final SubSubEntity subSubEntity = new SubSubEntity();
|
||||
final SubEntity subEntity = new SubSubEntity();
|
||||
try {
|
||||
session.save( subEntity );
|
||||
session.save( subSubEntity );
|
||||
transaction.commit();
|
||||
subSubEntityId = subSubEntity.getId();
|
||||
}
|
||||
finally {
|
||||
if ( transaction.getStatus() == TransactionStatus.ACTIVE ) {
|
||||
transaction.rollback();
|
||||
}
|
||||
}
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRetrieveSubEntity() {
|
||||
session = openSession();
|
||||
try {
|
||||
RootEntity loaded = session.get( SubEntity.class, subSubEntityId );
|
||||
assertNotNull( loaded );
|
||||
assertTrue( loaded instanceof SubSubEntity );
|
||||
}
|
||||
finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void shouldNotRetrieveSubSubSubEntity() {
|
||||
session = openSession();
|
||||
try {
|
||||
SubSubSubEntity loaded = session.get( SubSubSubEntity.class, subSubEntityId );
|
||||
assertNull( loaded );
|
||||
}
|
||||
finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Criteria
|
||||
|
||||
@Test
|
||||
public void shouldRetrieveSubSubEntityWithCriteria() {
|
||||
session = openSession();
|
||||
try {
|
||||
SubSubEntity loaded = (SubSubEntity) session.createCriteria( SubSubEntity.class )
|
||||
.add( Restrictions.idEq( subSubEntityId ) )
|
||||
.uniqueResult();
|
||||
assertNotNull( loaded );
|
||||
}
|
||||
finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotRetrieveSubSubSubEntityWithCriteria() {
|
||||
session = openSession();
|
||||
try {
|
||||
SubSubSubEntity loaded = (SubSubSubEntity) session.createCriteria( SubSubSubEntity.class )
|
||||
.add( Restrictions.idEq( subSubEntityId ) )
|
||||
.uniqueResult();
|
||||
assertNull( loaded );
|
||||
}
|
||||
finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
// HQL
|
||||
|
||||
@Test
|
||||
public void shouldRetrieveSubSubEntityWithHQL() {
|
||||
session = openSession();
|
||||
try {
|
||||
SubSubEntity loaded = (SubSubEntity) session.createQuery(
|
||||
"select se from SubSubEntity se where se.id = :id" )
|
||||
.setLong( "id", subSubEntityId )
|
||||
.uniqueResult();
|
||||
assertNotNull( loaded );
|
||||
}
|
||||
finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotRetrieveSubSubSubEntityWithHQL() {
|
||||
session = openSession();
|
||||
try {
|
||||
SubSubSubEntity loaded = (SubSubSubEntity) session.createQuery(
|
||||
"select se from SubSubSubEntity se where se.id = :id" )
|
||||
.setLong( "id", subSubEntityId )
|
||||
.uniqueResult();
|
||||
assertNull( loaded );
|
||||
}
|
||||
finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.inheritancediscriminator.joinedsubclass;
|
||||
|
||||
import javax.persistence.DiscriminatorColumn;
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
|
||||
/**
|
||||
* @author Andrea Boriero
|
||||
*/
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.JOINED)
|
||||
@DiscriminatorColumn()
|
||||
@DiscriminatorValue("ROOT")
|
||||
public class RootEntity {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.inheritancediscriminator.joinedsubclass;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
/**
|
||||
* @author Andrea Boriero
|
||||
*/
|
||||
@Entity
|
||||
@DiscriminatorValue("SUB")
|
||||
public class SubEntity extends RootEntity {
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.inheritancediscriminator.joinedsubclass;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
/**
|
||||
* @author Andrea Boriero
|
||||
*/
|
||||
@Entity
|
||||
@DiscriminatorValue("SUB-SUB")
|
||||
public class SubSubEntity extends SubEntity {
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.inheritancediscriminator.joinedsubclass;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
/**
|
||||
* @author Andrea Boriero
|
||||
*/
|
||||
@Entity
|
||||
@DiscriminatorValue("SUB-SUB-SUB")
|
||||
public class SubSubSubEntity extends SubSubEntity {
|
||||
}
|
Loading…
Reference in New Issue