Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
218 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
...va/org/hibernate/orm/test/metamodel/mapping/inheritance/OneToManyToInheritedTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* | ||
* 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.orm.test.metamodel.mapping.inheritance; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.OneToMany; | ||
|
||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.ServiceRegistry; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
/** | ||
* @author Laurent Almeras | ||
*/ | ||
@DomainModel( | ||
annotatedClasses = { | ||
OneToManyToInheritedTypeTest.SuperType.class, | ||
OneToManyToInheritedTypeTest.TypeA.class, | ||
OneToManyToInheritedTypeTest.TypeB.class, | ||
OneToManyToInheritedTypeTest.LinkedEntity.class | ||
} | ||
) | ||
@ServiceRegistry | ||
@SessionFactory | ||
public class OneToManyToInheritedTypeTest { | ||
|
||
@BeforeEach | ||
public void setUp(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
SuperType superType = new SuperType( 1 ); | ||
TypeB typeB = new TypeB( 2, "typeB" ); | ||
TypeA typeA = new TypeA( 3, "typeA" ); | ||
LinkedEntity entity = new LinkedEntity( 3 ); | ||
entity.addSuperType( superType ); | ||
entity.addSuperType( typeB ); | ||
entity.addSuperType( typeA ); | ||
session.save( superType ); | ||
session.save( typeB ); | ||
session.save( typeA ); | ||
session.save( entity ); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void basicTest(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
LinkedEntity entity = session.find( LinkedEntity.class, 3 ); | ||
List<SuperType> superTypes = entity.getSuperTypes(); | ||
assertThat( superTypes.size(), is( 3 ) ); | ||
for ( SuperType superType : superTypes ) { | ||
if ( superType.getId() == 2 ) { | ||
assertThat( superType, instanceOf( TypeB.class ) ); | ||
TypeB typeB = (TypeB) superType; | ||
assertThat( typeB.getTypeBName(), is( "typeB" ) ); | ||
} | ||
if ( superType.getId() == 3 ) { | ||
assertThat( superType, instanceOf( TypeA.class ) ); | ||
TypeA typeB = (TypeA) superType; | ||
assertThat( typeB.getTypeAName(), is( "typeA" ) ); | ||
} | ||
} | ||
} | ||
); | ||
} | ||
|
||
@Entity(name = "SuperType") | ||
public static class SuperType { | ||
private Integer id; | ||
|
||
public SuperType() { | ||
} | ||
|
||
public SuperType(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
@Id | ||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
} | ||
|
||
@Entity(name = "TypeA") | ||
public static class TypeA extends SuperType { | ||
private String typeAName; | ||
|
||
public TypeA() { | ||
} | ||
|
||
public TypeA(Integer id, String typeAName) { | ||
super( id ); | ||
this.typeAName = typeAName; | ||
} | ||
|
||
public String getTypeAName() { | ||
return typeAName; | ||
} | ||
|
||
public void setTypeAName(String typeAName) { | ||
this.typeAName = typeAName; | ||
} | ||
} | ||
|
||
@Entity(name = "TypeB") | ||
public static class TypeB extends SuperType { | ||
private String typeBName; | ||
|
||
public TypeB() { | ||
} | ||
|
||
public TypeB(Integer id, String typeBName) { | ||
super( id ); | ||
this.typeBName = typeBName; | ||
} | ||
|
||
public String getTypeBName() { | ||
return typeBName; | ||
} | ||
|
||
public void setTypeBName(String typeBName) { | ||
this.typeBName = typeBName; | ||
} | ||
} | ||
|
||
@Entity(name = "LinkedEntity") | ||
public static class LinkedEntity { | ||
private Integer id; | ||
private List<SuperType> superTypes; | ||
|
||
public LinkedEntity() { | ||
} | ||
|
||
public LinkedEntity(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
@Id | ||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
@OneToMany | ||
public List<SuperType> getSuperTypes() { | ||
return superTypes; | ||
} | ||
|
||
public void setSuperTypes(List<SuperType> superTypes) { | ||
this.superTypes = superTypes; | ||
} | ||
|
||
public void addSuperType(SuperType superType) { | ||
if ( superTypes == null ) { | ||
superTypes = new ArrayList<>(); | ||
} | ||
this.superTypes.add( superType ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters