mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-10 05:04:52 +00:00
Fix @ElementCollection + @OrderBy with Inheritance
This commit is contained in:
parent
b9612247f5
commit
8db9709408
@ -211,11 +211,6 @@ public Object instantiate() {
|
|||||||
return getEntityMapping().getRepresentationStrategy().getInstantiator().instantiate( sessionFactory );
|
return getEntityMapping().getRepresentationStrategy().getInstantiator().instantiate( sessionFactory );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public SingularAttributeMapping getParentInjectionAttributeMapping() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EntityMappingType findContainingEntityMapping() {
|
public EntityMappingType findContainingEntityMapping() {
|
||||||
return entityMapping;
|
return entityMapping;
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
import org.hibernate.mapping.Collection;
|
import org.hibernate.mapping.Collection;
|
||||||
import org.hibernate.mapping.Value;
|
import org.hibernate.mapping.Value;
|
||||||
import org.hibernate.metamodel.mapping.CollectionPart;
|
import org.hibernate.metamodel.mapping.CollectionPart;
|
||||||
|
import org.hibernate.metamodel.mapping.ColumnConsumer;
|
||||||
import org.hibernate.metamodel.mapping.EntityAssociationMapping;
|
import org.hibernate.metamodel.mapping.EntityAssociationMapping;
|
||||||
import org.hibernate.metamodel.mapping.EntityMappingType;
|
import org.hibernate.metamodel.mapping.EntityMappingType;
|
||||||
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
|
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
|
||||||
@ -168,6 +169,12 @@ public <T> DomainResult<T> createDomainResult(
|
|||||||
throw new NotYetImplementedFor6Exception( getClass() );
|
throw new NotYetImplementedFor6Exception( getClass() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitColumns(ColumnConsumer consumer) {
|
||||||
|
entityMappingType.visitColumns( consumer );
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void applySqlSelections(
|
public void applySqlSelections(
|
||||||
NavigablePath navigablePath,
|
NavigablePath navigablePath,
|
||||||
|
@ -7,18 +7,24 @@
|
|||||||
package org.hibernate.metamodel.mapping.ordering.ast;
|
package org.hibernate.metamodel.mapping.ordering.ast;
|
||||||
|
|
||||||
import org.hibernate.SortOrder;
|
import org.hibernate.SortOrder;
|
||||||
|
import org.hibernate.metamodel.mapping.MappingType;
|
||||||
|
import org.hibernate.metamodel.mapping.ModelPartContainer;
|
||||||
|
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
|
||||||
import org.hibernate.metamodel.mapping.ordering.TranslationContext;
|
import org.hibernate.metamodel.mapping.ordering.TranslationContext;
|
||||||
|
import org.hibernate.persister.entity.AbstractEntityPersister;
|
||||||
import org.hibernate.query.NavigablePath;
|
import org.hibernate.query.NavigablePath;
|
||||||
import org.hibernate.sql.ast.spi.SqlAstCreationState;
|
import org.hibernate.sql.ast.spi.SqlAstCreationState;
|
||||||
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
|
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
|
||||||
import org.hibernate.sql.ast.tree.from.TableGroup;
|
import org.hibernate.sql.ast.tree.from.TableGroup;
|
||||||
import org.hibernate.sql.ast.tree.from.TableReference;
|
import org.hibernate.sql.ast.tree.from.TableReference;
|
||||||
|
import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
|
||||||
import org.hibernate.sql.ast.tree.select.QuerySpec;
|
import org.hibernate.sql.ast.tree.select.QuerySpec;
|
||||||
import org.hibernate.sql.ast.tree.select.SortSpecification;
|
import org.hibernate.sql.ast.tree.select.SortSpecification;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a column-reference used in an order-by fragment
|
* Represents a column-reference used in an order-by fragment
|
||||||
*
|
*
|
||||||
|
* @author Steve Ebersole
|
||||||
* @apiNote This is Hibernate-specific feature. For {@link javax.persistence.OrderBy} (JPA)
|
* @apiNote This is Hibernate-specific feature. For {@link javax.persistence.OrderBy} (JPA)
|
||||||
* all path references are expected to be domain paths (attributes).
|
* all path references are expected to be domain paths (attributes).
|
||||||
*
|
*
|
||||||
@ -58,16 +64,18 @@ public void apply(
|
|||||||
String collation,
|
String collation,
|
||||||
SortOrder sortOrder,
|
SortOrder sortOrder,
|
||||||
SqlAstCreationState creationState) {
|
SqlAstCreationState creationState) {
|
||||||
final TableReference primaryTableReference = tableGroup.getPrimaryTableReference();
|
TableReference tableReference;
|
||||||
|
|
||||||
|
tableReference = getTableReference( tableGroup );
|
||||||
|
|
||||||
final SqlExpressionResolver sqlExpressionResolver = creationState.getSqlExpressionResolver();
|
final SqlExpressionResolver sqlExpressionResolver = creationState.getSqlExpressionResolver();
|
||||||
|
|
||||||
ast.addSortSpecification(
|
ast.addSortSpecification(
|
||||||
new SortSpecification(
|
new SortSpecification(
|
||||||
sqlExpressionResolver.resolveSqlExpression(
|
sqlExpressionResolver.resolveSqlExpression(
|
||||||
SqlExpressionResolver.createColumnReferenceKey( primaryTableReference, columnExpression ),
|
SqlExpressionResolver.createColumnReferenceKey( tableReference, columnExpression ),
|
||||||
sqlAstProcessingState -> new org.hibernate.sql.ast.tree.expression.ColumnReference(
|
sqlAstProcessingState -> new org.hibernate.sql.ast.tree.expression.ColumnReference(
|
||||||
tableGroup.getPrimaryTableReference(),
|
tableReference,
|
||||||
columnExpression,
|
columnExpression,
|
||||||
isColumnExpressionFormula,
|
isColumnExpressionFormula,
|
||||||
// because these ordering fragments are only ever part of the order-by clause, there
|
// because these ordering fragments are only ever part of the order-by clause, there
|
||||||
@ -81,4 +89,29 @@ public void apply(
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TableReference getTableReference(TableGroup tableGroup) {
|
||||||
|
ModelPartContainer modelPart = tableGroup.getModelPart();
|
||||||
|
if ( modelPart instanceof PluralAttributeMapping ) {
|
||||||
|
MappingType partMappingType = ( (PluralAttributeMapping) modelPart ).getElementDescriptor()
|
||||||
|
.getPartMappingType();
|
||||||
|
if ( partMappingType instanceof AbstractEntityPersister ) {
|
||||||
|
AbstractEntityPersister abstractEntityPersister = (AbstractEntityPersister) partMappingType;
|
||||||
|
int i = abstractEntityPersister.determineTableNumberForColumn( columnExpression );
|
||||||
|
String tableName = abstractEntityPersister.getTableName( i );
|
||||||
|
for ( TableReferenceJoin tableReferenceJoin : tableGroup.getTableReferenceJoins() ) {
|
||||||
|
final TableReference joinedTableReference = tableReferenceJoin.getJoinedTableReference();
|
||||||
|
if ( joinedTableReference.getTableExpression()
|
||||||
|
.equals( tableName ) ) {
|
||||||
|
return joinedTableReference;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return tableGroup.getPrimaryTableReference();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -52,8 +52,21 @@ public DomainPath resolvePathPart(
|
|||||||
final ModelPart subPart = pluralAttributeMapping.findSubPart( name, null );
|
final ModelPart subPart = pluralAttributeMapping.findSubPart( name, null );
|
||||||
|
|
||||||
if ( subPart != null ) {
|
if ( subPart != null ) {
|
||||||
assert subPart instanceof CollectionPart;
|
if ( subPart instanceof CollectionPart ) {
|
||||||
return new CollectionPartPath( this, (CollectionPart) subPart );
|
return new CollectionPartPath( this, (CollectionPart) subPart );
|
||||||
|
}
|
||||||
|
else if ( !( subPart instanceof EmbeddableValuedModelPart ) ) {
|
||||||
|
final CollectionPartPath elementPath = new CollectionPartPath(
|
||||||
|
this,
|
||||||
|
pluralAttributeMapping.getElementDescriptor()
|
||||||
|
);
|
||||||
|
|
||||||
|
return new DomainPathContinuation(
|
||||||
|
elementPath.getNavigablePath().append( name ),
|
||||||
|
this,
|
||||||
|
pluralAttributeMapping.getElementDescriptor()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// the above checks for explicit element or index descriptor references
|
// the above checks for explicit element or index descriptor references
|
||||||
|
@ -133,6 +133,7 @@
|
|||||||
import org.hibernate.metamodel.mapping.AttributeMapping;
|
import org.hibernate.metamodel.mapping.AttributeMapping;
|
||||||
import org.hibernate.metamodel.mapping.AttributeMetadata;
|
import org.hibernate.metamodel.mapping.AttributeMetadata;
|
||||||
import org.hibernate.metamodel.mapping.AttributeMetadataAccess;
|
import org.hibernate.metamodel.mapping.AttributeMetadataAccess;
|
||||||
|
import org.hibernate.metamodel.mapping.ColumnConsumer;
|
||||||
import org.hibernate.metamodel.mapping.EntityDiscriminatorMapping;
|
import org.hibernate.metamodel.mapping.EntityDiscriminatorMapping;
|
||||||
import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
|
import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
|
||||||
import org.hibernate.metamodel.mapping.EntityMappingType;
|
import org.hibernate.metamodel.mapping.EntityMappingType;
|
||||||
@ -6441,6 +6442,13 @@ public void visitSuperTypeAttributeMappings(Consumer<AttributeMapping> action) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitColumns(ColumnConsumer consumer) {
|
||||||
|
getAttributeMappings().forEach(
|
||||||
|
attributeMapping -> attributeMapping.visitColumns( consumer )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitSubTypeAttributeMappings(Consumer<AttributeMapping> action) {
|
public void visitSubTypeAttributeMappings(Consumer<AttributeMapping> action) {
|
||||||
if ( subclassMappingTypes != null ) {
|
if ( subclassMappingTypes != null ) {
|
||||||
|
@ -1241,7 +1241,7 @@ public TableGroup createRootTableGroup(
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EntityDiscriminatorMapping getDiscriminatorMapping(TableGroup tableGroup) {
|
public EntityDiscriminatorMapping getDiscriminatorMapping(TableGroup tableGroup) {
|
||||||
if(hasSubclasses()) {
|
if ( hasSubclasses() ) {
|
||||||
if ( explicitDiscriminatorColumnName == null ) {
|
if ( explicitDiscriminatorColumnName == null ) {
|
||||||
CaseSearchedExpressionInfo info = getCaseSearchedExpression( tableGroup );
|
CaseSearchedExpressionInfo info = getCaseSearchedExpression( tableGroup );
|
||||||
return new JoinedSubclassDiscriminatorMappingImpl(
|
return new JoinedSubclassDiscriminatorMappingImpl(
|
||||||
@ -1291,34 +1291,38 @@ private CaseSearchedExpressionInfo getCaseSearchedExpression(TableGroup entityTa
|
|||||||
final BasicType discriminatorType = (BasicType) getDiscriminatorType();
|
final BasicType discriminatorType = (BasicType) getDiscriminatorType();
|
||||||
final CaseSearchedExpression caseSearchedExpression = new CaseSearchedExpression( discriminatorType );
|
final CaseSearchedExpression caseSearchedExpression = new CaseSearchedExpression( discriminatorType );
|
||||||
|
|
||||||
discriminatorValuesByTableName.forEach(
|
Boolean addPrimaryTableCaseAsLastCaseExpression = false;
|
||||||
(tableName, discriminatorValue) -> {
|
for ( String tableName : discriminatorValuesByTableName.keySet() ) {
|
||||||
if ( ! primaryTableReference.getTableExpression().equals( tableName ) ) {
|
if ( !primaryTableReference.getTableExpression().equals( tableName ) ) {
|
||||||
TableReference tableReference = entityTableGroup.getTableReference( tableName );
|
TableReference tableReference = entityTableGroup.getTableReference( tableName );
|
||||||
if ( tableReference == null ) {
|
if ( tableReference == null ) {
|
||||||
// we have not yet created a TableReference for this sub-class table, but we need to because
|
// we have not yet created a TableReference for this sub-class table, but we need to because
|
||||||
// it has a discriminator value associated with it
|
// it has a discriminator value associated with it
|
||||||
tableReference = entityTableGroup.resolveTableReference( tableName );
|
tableReference = entityTableGroup.resolveTableReference( tableName );
|
||||||
}
|
|
||||||
|
|
||||||
final ColumnReference identifierColumnReference = getIdentifierColumnReference( tableReference );
|
|
||||||
info.columnReferences.add( identifierColumnReference );
|
|
||||||
addWhen(
|
|
||||||
caseSearchedExpression,
|
|
||||||
tableReference,
|
|
||||||
identifierColumnReference,
|
|
||||||
discriminatorType
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
addWhen(
|
final ColumnReference identifierColumnReference = getIdentifierColumnReference( tableReference );
|
||||||
caseSearchedExpression,
|
info.columnReferences.add( identifierColumnReference );
|
||||||
primaryTableReference,
|
addWhen(
|
||||||
getIdentifierColumnReference( primaryTableReference ),
|
caseSearchedExpression,
|
||||||
discriminatorType
|
tableReference,
|
||||||
);
|
identifierColumnReference,
|
||||||
|
discriminatorType
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
addPrimaryTableCaseAsLastCaseExpression = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( addPrimaryTableCaseAsLastCaseExpression ) {
|
||||||
|
addWhen(
|
||||||
|
caseSearchedExpression,
|
||||||
|
primaryTableReference,
|
||||||
|
getIdentifierColumnReference( primaryTableReference ),
|
||||||
|
discriminatorType
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
info.caseSearchedExpression = caseSearchedExpression;
|
info.caseSearchedExpression = caseSearchedExpression;
|
||||||
return info;
|
return info;
|
||||||
|
@ -93,29 +93,25 @@ public Object getCompositeInstance() {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void resolveKey(RowProcessingState rowProcessingState) {
|
public void resolveKey(RowProcessingState rowProcessingState) {
|
||||||
// todo (6.0) : register "parent resolution listener" if the composite is defined for `@Parent`
|
|
||||||
// something like:
|
|
||||||
|
|
||||||
final PropertyAccess parentInjectionPropertyAccess = embeddedModelPartDescriptor.getParentInjectionAttributePropertyAccess();
|
final PropertyAccess parentInjectionPropertyAccess = embeddedModelPartDescriptor.getParentInjectionAttributePropertyAccess();
|
||||||
|
|
||||||
if ( parentInjectionPropertyAccess != null ) {
|
final FetchParentAccess fetchParentAccess = getFetchParentAccess();
|
||||||
if ( getFetchParentAccess() != null ) {
|
if ( parentInjectionPropertyAccess != null && fetchParentAccess != null ) {
|
||||||
getFetchParentAccess().findFirstEntityDescriptorAccess().registerResolutionListener(
|
fetchParentAccess.findFirstEntityDescriptorAccess().registerResolutionListener(
|
||||||
// todo (6.0) : this is the legacy behavior
|
// todo (6.0) : this is the legacy behavior
|
||||||
// - the first entity is injected as the parent, even if the composite
|
// - the first entity is injected as the parent, even if the composite
|
||||||
// is defined on another composite
|
// is defined on another composite
|
||||||
owner -> {
|
owner -> {
|
||||||
if ( compositeInstance == null ) {
|
if ( compositeInstance == null ) {
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
parentInjectionPropertyAccess.getSetter().set(
|
|
||||||
compositeInstance,
|
|
||||||
owner,
|
|
||||||
rowProcessingState.getSession().getFactory()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
);
|
parentInjectionPropertyAccess.getSetter().set(
|
||||||
}
|
compositeInstance,
|
||||||
|
owner,
|
||||||
|
rowProcessingState.getSession().getFactory()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,7 +133,7 @@ public void initializeInstance(RowProcessingState rowProcessingState) {
|
|||||||
|
|
||||||
final PropertyAccess parentInjectionPropertyAccess = embeddedModelPartDescriptor.getParentInjectionAttributePropertyAccess();
|
final PropertyAccess parentInjectionPropertyAccess = embeddedModelPartDescriptor.getParentInjectionAttributePropertyAccess();
|
||||||
|
|
||||||
if ( parentInjectionPropertyAccess != null && getFetchParentAccess() == null ) {
|
if ( parentInjectionPropertyAccess != null ) {
|
||||||
Initializer initializer = rowProcessingState.resolveInitializer( navigablePath.getParent() );
|
Initializer initializer = rowProcessingState.resolveInitializer( navigablePath.getParent() );
|
||||||
final Object owner;
|
final Object owner;
|
||||||
if ( initializer instanceof CollectionInitializer ) {
|
if ( initializer instanceof CollectionInitializer ) {
|
||||||
|
@ -12,7 +12,6 @@
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.hibernate.Filter;
|
import org.hibernate.Filter;
|
||||||
import org.hibernate.Transaction;
|
|
||||||
import org.hibernate.boot.spi.MetadataImplementor;
|
import org.hibernate.boot.spi.MetadataImplementor;
|
||||||
import org.hibernate.cfg.AvailableSettings;
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
import org.hibernate.mapping.Collection;
|
import org.hibernate.mapping.Collection;
|
||||||
@ -65,144 +64,114 @@ public void testSimpleElement(SessionFactoryScope scope) {
|
|||||||
scope.getMetadataImplementor().getCollectionBinding( Boy.class.getName() + '.' + "favoriteNumbers" )
|
scope.getMetadataImplementor().getCollectionBinding( Boy.class.getName() + '.' + "favoriteNumbers" )
|
||||||
.getCollectionTable().getName()
|
.getCollectionTable().getName()
|
||||||
);
|
);
|
||||||
scope.inSession(
|
|
||||||
|
scope.inTransaction(
|
||||||
session -> {
|
session -> {
|
||||||
Transaction transaction = session.getTransaction();
|
Boy boy = new Boy();
|
||||||
try {
|
boy.setFirstName( "John" );
|
||||||
transaction.begin();
|
boy.setLastName( "Doe" );
|
||||||
|
boy.getNickNames().add( "Johnny" );
|
||||||
Boy boy = new Boy();
|
boy.getNickNames().add( "Thing" );
|
||||||
boy.setFirstName( "John" );
|
boy.getScorePerNickName().put( "Johnny", 3 );
|
||||||
boy.setLastName( "Doe" );
|
boy.getScorePerNickName().put( "Thing", 5 );
|
||||||
boy.getNickNames().add( "Johnny" );
|
int[] favNbrs = new int[4];
|
||||||
boy.getNickNames().add( "Thing" );
|
for ( int index = 0; index < favNbrs.length - 1; index++ ) {
|
||||||
boy.getScorePerNickName().put( "Johnny", 3 );
|
favNbrs[index] = index * 3;
|
||||||
boy.getScorePerNickName().put( "Thing", 5 );
|
|
||||||
int[] favNbrs = new int[4];
|
|
||||||
for ( int index = 0; index < favNbrs.length - 1; index++ ) {
|
|
||||||
favNbrs[index] = index * 3;
|
|
||||||
}
|
|
||||||
boy.setFavoriteNumbers( favNbrs );
|
|
||||||
boy.getCharacters().add( Character.GENTLE );
|
|
||||||
boy.getCharacters().add( Character.CRAFTY );
|
|
||||||
|
|
||||||
HashMap<String, FavoriteFood> foods = new HashMap<>();
|
|
||||||
foods.put( "breakfast", FavoriteFood.PIZZA );
|
|
||||||
foods.put( "lunch", FavoriteFood.KUNGPAOCHICKEN );
|
|
||||||
foods.put( "dinner", FavoriteFood.SUSHI );
|
|
||||||
boy.setFavoriteFood( foods );
|
|
||||||
session.persist( boy );
|
|
||||||
transaction.commit();
|
|
||||||
|
|
||||||
session.clear();
|
|
||||||
transaction = session.beginTransaction();
|
|
||||||
boy = session.get( Boy.class, boy.getId() );
|
|
||||||
assertNotNull( boy.getNickNames() );
|
|
||||||
assertTrue( boy.getNickNames().contains( "Thing" ) );
|
|
||||||
assertNotNull( boy.getScorePerNickName() );
|
|
||||||
assertTrue( boy.getScorePerNickName().containsKey( "Thing" ) );
|
|
||||||
assertEquals( Integer.valueOf( 5 ), boy.getScorePerNickName().get( "Thing" ) );
|
|
||||||
assertNotNull( boy.getFavoriteNumbers() );
|
|
||||||
assertEquals( 3, boy.getFavoriteNumbers()[1] );
|
|
||||||
assertTrue( boy.getCharacters().contains( Character.CRAFTY ) );
|
|
||||||
assertTrue( boy.getFavoriteFood().get( "dinner" ).equals( FavoriteFood.SUSHI ) );
|
|
||||||
assertTrue( boy.getFavoriteFood().get( "lunch" ).equals( FavoriteFood.KUNGPAOCHICKEN ) );
|
|
||||||
assertTrue( boy.getFavoriteFood().get( "breakfast" ).equals( FavoriteFood.PIZZA ) );
|
|
||||||
List result = session.createQuery(
|
|
||||||
"select boy from Boy boy join boy.nickNames names where names = :name" )
|
|
||||||
.setParameter( "name", "Thing" ).list();
|
|
||||||
assertEquals( 1, result.size() );
|
|
||||||
session.delete( boy );
|
|
||||||
|
|
||||||
transaction.commit();
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
if ( transaction.isActive() ) {
|
|
||||||
transaction.rollback();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
boy.setFavoriteNumbers( favNbrs );
|
||||||
|
boy.getCharacters().add( Character.GENTLE );
|
||||||
|
boy.getCharacters().add( Character.CRAFTY );
|
||||||
|
|
||||||
|
HashMap<String, FavoriteFood> foods = new HashMap<>();
|
||||||
|
foods.put( "breakfast", FavoriteFood.PIZZA );
|
||||||
|
foods.put( "lunch", FavoriteFood.KUNGPAOCHICKEN );
|
||||||
|
foods.put( "dinner", FavoriteFood.SUSHI );
|
||||||
|
boy.setFavoriteFood( foods );
|
||||||
|
session.persist( boy );
|
||||||
|
|
||||||
|
session.getTransaction().commit();
|
||||||
|
|
||||||
|
session.clear();
|
||||||
|
|
||||||
|
session.beginTransaction();
|
||||||
|
boy = session.get( Boy.class, boy.getId() );
|
||||||
|
assertNotNull( boy.getNickNames() );
|
||||||
|
assertTrue( boy.getNickNames().contains( "Thing" ) );
|
||||||
|
assertNotNull( boy.getScorePerNickName() );
|
||||||
|
assertTrue( boy.getScorePerNickName().containsKey( "Thing" ) );
|
||||||
|
assertEquals( Integer.valueOf( 5 ), boy.getScorePerNickName().get( "Thing" ) );
|
||||||
|
assertNotNull( boy.getFavoriteNumbers() );
|
||||||
|
assertEquals( 3, boy.getFavoriteNumbers()[1] );
|
||||||
|
assertTrue( boy.getCharacters().contains( Character.CRAFTY ) );
|
||||||
|
assertTrue( boy.getFavoriteFood().get( "dinner" ).equals( FavoriteFood.SUSHI ) );
|
||||||
|
assertTrue( boy.getFavoriteFood().get( "lunch" ).equals( FavoriteFood.KUNGPAOCHICKEN ) );
|
||||||
|
assertTrue( boy.getFavoriteFood().get( "breakfast" ).equals( FavoriteFood.PIZZA ) );
|
||||||
|
List result = session.createQuery(
|
||||||
|
"select boy from Boy boy join boy.nickNames names where names = :name" )
|
||||||
|
.setParameter( "name", "Thing" ).list();
|
||||||
|
assertEquals( 1, result.size() );
|
||||||
|
session.delete( boy );
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCompositeElement(SessionFactoryScope scope) {
|
public void testCompositeElement(SessionFactoryScope scope) {
|
||||||
scope.inSession(
|
scope.inTransaction(
|
||||||
session -> {
|
session -> {
|
||||||
Transaction transaction = session.getTransaction();
|
Boy boy = new Boy();
|
||||||
try {
|
boy.setFirstName( "John" );
|
||||||
transaction.begin();
|
boy.setLastName( "Doe" );
|
||||||
|
Toy toy = new Toy();
|
||||||
|
toy.setName( "Balloon" );
|
||||||
|
toy.setSerial( "serial001" );
|
||||||
|
toy.setBrand( new Brand() );
|
||||||
|
toy.getBrand().setName( "Bandai" );
|
||||||
|
boy.getFavoriteToys().add( toy );
|
||||||
|
session.persist( boy );
|
||||||
|
session.getTransaction().commit();
|
||||||
|
|
||||||
Boy boy = new Boy();
|
session.clear();
|
||||||
boy.setFirstName( "John" );
|
|
||||||
boy.setLastName( "Doe" );
|
|
||||||
Toy toy = new Toy();
|
|
||||||
toy.setName( "Balloon" );
|
|
||||||
toy.setSerial( "serial001" );
|
|
||||||
toy.setBrand( new Brand() );
|
|
||||||
toy.getBrand().setName( "Bandai" );
|
|
||||||
boy.getFavoriteToys().add( toy );
|
|
||||||
session.persist( boy );
|
|
||||||
transaction.commit();
|
|
||||||
|
|
||||||
session.clear();
|
session.beginTransaction();
|
||||||
|
boy = session.get( Boy.class, boy.getId() );
|
||||||
transaction = session.beginTransaction();
|
assertNotNull( boy );
|
||||||
boy = session.get( Boy.class, boy.getId() );
|
assertNotNull( boy.getFavoriteToys() );
|
||||||
assertNotNull( boy );
|
assertTrue( boy.getFavoriteToys().contains( toy ) );
|
||||||
assertNotNull( boy.getFavoriteToys() );
|
Toy next = boy.getFavoriteToys().iterator().next();
|
||||||
assertTrue( boy.getFavoriteToys().contains( toy ) );
|
assertEquals( boy, next.getOwner(), "@Parent is failing" );
|
||||||
Toy next = boy.getFavoriteToys().iterator().next();
|
session.delete( boy );
|
||||||
assertEquals( boy, next.getOwner(), "@Parent is failing" );
|
|
||||||
session.delete( boy );
|
|
||||||
transaction.commit();
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
if ( transaction.isActive() ) {
|
|
||||||
transaction.rollback();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAttributedJoin(SessionFactoryScope scope) {
|
public void testAttributedJoin(SessionFactoryScope scope) {
|
||||||
scope.inSession(
|
scope.inTransaction(
|
||||||
session -> {
|
session -> {
|
||||||
Transaction transaction = session.getTransaction();
|
Country country = new Country();
|
||||||
try {
|
country.setName( "Australia" );
|
||||||
transaction.begin();
|
session.persist( country );
|
||||||
Country country = new Country();
|
|
||||||
country.setName( "Australia" );
|
|
||||||
session.persist( country );
|
|
||||||
|
|
||||||
Boy boy = new Boy();
|
Boy boy = new Boy();
|
||||||
boy.setFirstName( "John" );
|
boy.setFirstName( "John" );
|
||||||
boy.setLastName( "Doe" );
|
boy.setLastName( "Doe" );
|
||||||
CountryAttitude attitude = new CountryAttitude();
|
CountryAttitude attitude = new CountryAttitude();
|
||||||
// TODO: doesn't work
|
// TODO: doesn't work
|
||||||
attitude.setBoy( boy );
|
attitude.setBoy( boy );
|
||||||
attitude.setCountry( country );
|
attitude.setCountry( country );
|
||||||
attitude.setLikes( true );
|
attitude.setLikes( true );
|
||||||
boy.getCountryAttitudes().add( attitude );
|
boy.getCountryAttitudes().add( attitude );
|
||||||
session.persist( boy );
|
session.persist( boy );
|
||||||
transaction.commit();
|
session.getTransaction().commit();
|
||||||
|
|
||||||
session.clear();
|
session.clear();
|
||||||
|
|
||||||
transaction = session.beginTransaction();
|
session.beginTransaction();
|
||||||
boy = session.get( Boy.class, boy.getId() );
|
boy = session.get( Boy.class, boy.getId() );
|
||||||
assertTrue( boy.getCountryAttitudes().contains( attitude ) );
|
assertTrue( boy.getCountryAttitudes().contains( attitude ) );
|
||||||
session.delete( boy );
|
session.delete( boy );
|
||||||
session.delete( session.get( Country.class, country.getId() ) );
|
session.delete( session.get( Country.class, country.getId() ) );
|
||||||
transaction.commit();
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
if ( transaction.isActive() ) {
|
|
||||||
transaction.rollback();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -214,113 +183,85 @@ public void testLazyCollectionofElements(SessionFactoryScope scope) {
|
|||||||
scope.getMetadataImplementor().getCollectionBinding( Boy.class.getName() + '.' + "favoriteNumbers" )
|
scope.getMetadataImplementor().getCollectionBinding( Boy.class.getName() + '.' + "favoriteNumbers" )
|
||||||
.getCollectionTable().getName()
|
.getCollectionTable().getName()
|
||||||
);
|
);
|
||||||
scope.inSession(
|
|
||||||
|
scope.inTransaction(
|
||||||
session -> {
|
session -> {
|
||||||
Transaction transaction = session.getTransaction();
|
Boy boy = new Boy();
|
||||||
try {
|
boy.setFirstName( "John" );
|
||||||
transaction.begin();
|
boy.setLastName( "Doe" );
|
||||||
Boy boy = new Boy();
|
boy.getNickNames().add( "Johnny" );
|
||||||
boy.setFirstName( "John" );
|
boy.getNickNames().add( "Thing" );
|
||||||
boy.setLastName( "Doe" );
|
boy.getScorePerNickName().put( "Johnny", 3 );
|
||||||
boy.getNickNames().add( "Johnny" );
|
boy.getScorePerNickName().put( "Thing", 5 );
|
||||||
boy.getNickNames().add( "Thing" );
|
int[] favNbrs = new int[4];
|
||||||
boy.getScorePerNickName().put( "Johnny", 3 );
|
for ( int index = 0; index < favNbrs.length - 1; index++ ) {
|
||||||
boy.getScorePerNickName().put( "Thing", 5 );
|
favNbrs[index] = index * 3;
|
||||||
int[] favNbrs = new int[4];
|
|
||||||
for ( int index = 0; index < favNbrs.length - 1; index++ ) {
|
|
||||||
favNbrs[index] = index * 3;
|
|
||||||
}
|
|
||||||
boy.setFavoriteNumbers( favNbrs );
|
|
||||||
boy.getCharacters().add( Character.GENTLE );
|
|
||||||
boy.getCharacters().add( Character.CRAFTY );
|
|
||||||
session.persist( boy );
|
|
||||||
transaction.commit();
|
|
||||||
|
|
||||||
session.clear();
|
|
||||||
|
|
||||||
transaction = session.beginTransaction();
|
|
||||||
boy = session.get( Boy.class, boy.getId() );
|
|
||||||
assertNotNull( boy.getNickNames() );
|
|
||||||
assertTrue( boy.getNickNames().contains( "Thing" ) );
|
|
||||||
assertNotNull( boy.getScorePerNickName() );
|
|
||||||
assertTrue( boy.getScorePerNickName().containsKey( "Thing" ) );
|
|
||||||
assertEquals( new Integer( 5 ), boy.getScorePerNickName().get( "Thing" ) );
|
|
||||||
assertNotNull( boy.getFavoriteNumbers() );
|
|
||||||
assertEquals( 3, boy.getFavoriteNumbers()[1] );
|
|
||||||
assertTrue( boy.getCharacters().contains( Character.CRAFTY ) );
|
|
||||||
List result = session.createQuery(
|
|
||||||
"select boy from Boy boy join boy.nickNames names where names = :name" )
|
|
||||||
.setParameter( "name", "Thing" ).list();
|
|
||||||
assertEquals( 1, result.size() );
|
|
||||||
session.delete( boy );
|
|
||||||
transaction.commit();
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
if ( transaction.isActive() ) {
|
|
||||||
transaction.rollback();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
boy.setFavoriteNumbers( favNbrs );
|
||||||
|
boy.getCharacters().add( Character.GENTLE );
|
||||||
|
boy.getCharacters().add( Character.CRAFTY );
|
||||||
|
session.persist( boy );
|
||||||
|
session.getTransaction().commit();
|
||||||
|
|
||||||
|
session.clear();
|
||||||
|
|
||||||
|
session.beginTransaction();
|
||||||
|
boy = session.get( Boy.class, boy.getId() );
|
||||||
|
assertNotNull( boy.getNickNames() );
|
||||||
|
assertTrue( boy.getNickNames().contains( "Thing" ) );
|
||||||
|
assertNotNull( boy.getScorePerNickName() );
|
||||||
|
assertTrue( boy.getScorePerNickName().containsKey( "Thing" ) );
|
||||||
|
assertEquals( new Integer( 5 ), boy.getScorePerNickName().get( "Thing" ) );
|
||||||
|
assertNotNull( boy.getFavoriteNumbers() );
|
||||||
|
assertEquals( 3, boy.getFavoriteNumbers()[1] );
|
||||||
|
assertTrue( boy.getCharacters().contains( Character.CRAFTY ) );
|
||||||
|
List result = session.createQuery(
|
||||||
|
"select boy from Boy boy join boy.nickNames names where names = :name" )
|
||||||
|
.setParameter( "name", "Thing" ).list();
|
||||||
|
assertEquals( 1, result.size() );
|
||||||
|
session.delete( boy );
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFetchEagerAndFilter(SessionFactoryScope scope) {
|
public void testFetchEagerAndFilter(SessionFactoryScope scope) {
|
||||||
scope.inSession(
|
scope.inTransaction(
|
||||||
session -> {
|
session -> {
|
||||||
Transaction tx = session.beginTransaction();
|
TestCourse test = new TestCourse();
|
||||||
try {
|
|
||||||
TestCourse test = new TestCourse();
|
|
||||||
|
|
||||||
LocalizedString title = new LocalizedString( "title in english" );
|
LocalizedString title = new LocalizedString( "title in english" );
|
||||||
title.getVariations().put( Locale.FRENCH.getLanguage(), "title en francais" );
|
title.getVariations().put( Locale.FRENCH.getLanguage(), "title en francais" );
|
||||||
test.setTitle( title );
|
test.setTitle( title );
|
||||||
session.save( test );
|
session.save( test );
|
||||||
|
|
||||||
session.flush();
|
session.flush();
|
||||||
session.clear();
|
session.clear();
|
||||||
|
|
||||||
Filter filter = session.enableFilter( "selectedLocale" );
|
Filter filter = session.enableFilter( "selectedLocale" );
|
||||||
filter.setParameter( "param", "fr" );
|
filter.setParameter( "param", "fr" );
|
||||||
|
|
||||||
Query q = session.createQuery( "from TestCourse t" );
|
Query q = session.createQuery( "from TestCourse t" );
|
||||||
List l = q.list();
|
List l = q.list();
|
||||||
assertEquals( 1, l.size() );
|
assertEquals( 1, l.size() );
|
||||||
|
|
||||||
TestCourse t = session.get( TestCourse.class, test.getTestCourseId() );
|
TestCourse t = session.get( TestCourse.class, test.getTestCourseId() );
|
||||||
assertEquals( 1, t.getTitle().getVariations().size() );
|
assertEquals( 1, t.getTitle().getVariations().size() );
|
||||||
|
|
||||||
tx.rollback();
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
if ( tx.isActive() ) {
|
|
||||||
tx.rollback();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMapKeyType(SessionFactoryScope scope) {
|
public void testMapKeyType(SessionFactoryScope scope) {
|
||||||
|
scope.inTransaction(
|
||||||
scope.inSession(
|
|
||||||
session -> {
|
session -> {
|
||||||
Matrix m = new Matrix();
|
Matrix m = new Matrix();
|
||||||
m.getMvalues().put( 1, 1.1f );
|
m.getMvalues().put( 1, 1.1f );
|
||||||
Transaction tx = session.beginTransaction();
|
session.persist( m );
|
||||||
try {
|
session.flush();
|
||||||
session.persist( m );
|
session.clear();
|
||||||
session.flush();
|
m = session.get( Matrix.class, m.getId() );
|
||||||
session.clear();
|
assertEquals( 1.1f, m.getMvalues().get( 1 ), 0.01f );
|
||||||
m = session.get( Matrix.class, m.getId() );
|
|
||||||
assertEquals( 1.1f, m.getMvalues().get( 1 ), 0.01f );
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
if ( tx.isActive() ) {
|
|
||||||
tx.rollback();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,147 @@
|
|||||||
|
/*
|
||||||
|
* 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.annotations.collectionelement;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Set;
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.ElementCollection;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Inheritance;
|
||||||
|
import javax.persistence.InheritanceType;
|
||||||
|
import javax.persistence.OrderBy;
|
||||||
|
|
||||||
|
import org.hibernate.testing.orm.junit.DomainModel;
|
||||||
|
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||||
|
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
|
||||||
|
@DomainModel(
|
||||||
|
annotatedClasses = {
|
||||||
|
org.hibernate.orm.test.annotations.collectionelement.OrderByColumnNameTest.Product.class,
|
||||||
|
org.hibernate.orm.test.annotations.collectionelement.OrderByColumnNameTest.Widgets.class,
|
||||||
|
org.hibernate.orm.test.annotations.collectionelement.OrderByColumnNameTest.Widget1.class,
|
||||||
|
org.hibernate.orm.test.annotations.collectionelement.OrderByColumnNameTest.Widget2.class,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@SessionFactory
|
||||||
|
public class OrderByColumnNameTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOrderByName(SessionFactoryScope scope) {
|
||||||
|
scope.inTransaction(
|
||||||
|
session -> {
|
||||||
|
Product p = new Product();
|
||||||
|
HashSet<Widgets> set = new HashSet<>();
|
||||||
|
|
||||||
|
Widgets widget = new Widgets();
|
||||||
|
widget.setName( "hammer" );
|
||||||
|
set.add( widget );
|
||||||
|
session.persist( widget );
|
||||||
|
|
||||||
|
widget = new Widgets();
|
||||||
|
widget.setName( "axel" );
|
||||||
|
set.add( widget );
|
||||||
|
session.persist( widget );
|
||||||
|
|
||||||
|
widget = new Widgets();
|
||||||
|
widget.setName( "screwdriver" );
|
||||||
|
set.add( widget );
|
||||||
|
session.persist( widget );
|
||||||
|
|
||||||
|
p.setWidgets( set );
|
||||||
|
session.persist( p );
|
||||||
|
session.getTransaction().commit();
|
||||||
|
|
||||||
|
session.beginTransaction();
|
||||||
|
session.clear();
|
||||||
|
p = session.get( Product.class, p.getId() );
|
||||||
|
assertTrue( p.getWidgets().size() == 3, "has three Widgets" );
|
||||||
|
Iterator iter = p.getWidgets().iterator();
|
||||||
|
assertEquals( "axel", ( (Widgets) iter.next() ).getName() );
|
||||||
|
assertEquals( "hammer", ( (Widgets) iter.next() ).getName() );
|
||||||
|
assertEquals( "screwdriver", ( (Widgets) iter.next() ).getName() );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public static class Product {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
@ElementCollection
|
||||||
|
@OrderBy("name_1 ASC")
|
||||||
|
private Set<Widgets> widgets;
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Widgets> getWidgets() {
|
||||||
|
return widgets;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWidgets(Set<Widgets> widgets) {
|
||||||
|
this.widgets = widgets;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Inheritance(strategy = InheritanceType.JOINED)
|
||||||
|
public static class Widgets {
|
||||||
|
private String name;
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
public Widgets() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Column(name = "name_1")
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public static class Widget1 extends org.hibernate.orm.test.annotations.collectionelement.Widgets {
|
||||||
|
private String name1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public static class Widget2 extends org.hibernate.orm.test.annotations.collectionelement.Widgets {
|
||||||
|
private String name2;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -9,7 +9,6 @@
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
import org.hibernate.Transaction;
|
|
||||||
import org.hibernate.dialect.TeradataDialect;
|
import org.hibernate.dialect.TeradataDialect;
|
||||||
|
|
||||||
import org.hibernate.testing.orm.junit.DomainModel;
|
import org.hibernate.testing.orm.junit.DomainModel;
|
||||||
@ -26,6 +25,8 @@
|
|||||||
annotatedClasses = {
|
annotatedClasses = {
|
||||||
Products.class,
|
Products.class,
|
||||||
Widgets.class,
|
Widgets.class,
|
||||||
|
Widgets.Widget1.class,
|
||||||
|
Widgets.Widget2.class,
|
||||||
BugSystem.class
|
BugSystem.class
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -34,49 +35,38 @@ public class OrderByTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOrderByName(SessionFactoryScope scope) {
|
public void testOrderByName(SessionFactoryScope scope) {
|
||||||
scope.inSession(
|
scope.inTransaction(
|
||||||
session -> {
|
session -> {
|
||||||
Transaction tx = session.beginTransaction();
|
Products p = new Products();
|
||||||
try {
|
HashSet<Widgets> set = new HashSet<>();
|
||||||
|
|
||||||
Products p = new Products();
|
Widgets widget = new Widgets();
|
||||||
HashSet<Widgets> set = new HashSet<>();
|
widget.setName( "hammer" );
|
||||||
|
set.add( widget );
|
||||||
|
session.persist( widget );
|
||||||
|
|
||||||
Widgets widget = new Widgets();
|
widget = new Widgets();
|
||||||
widget.setName( "hammer" );
|
widget.setName( "axel" );
|
||||||
set.add( widget );
|
set.add( widget );
|
||||||
session.persist( widget );
|
session.persist( widget );
|
||||||
|
|
||||||
widget = new Widgets();
|
widget = new Widgets();
|
||||||
widget.setName( "axel" );
|
widget.setName( "screwdriver" );
|
||||||
set.add( widget );
|
set.add( widget );
|
||||||
session.persist( widget );
|
session.persist( widget );
|
||||||
|
|
||||||
widget = new Widgets();
|
p.setWidgets( set );
|
||||||
widget.setName( "screwdriver" );
|
session.persist( p );
|
||||||
set.add( widget );
|
session.getTransaction().commit();
|
||||||
session.persist( widget );
|
|
||||||
|
|
||||||
p.setWidgets( set );
|
session.beginTransaction();
|
||||||
session.persist( p );
|
session.clear();
|
||||||
tx.commit();
|
p = session.get( Products.class, p.getId() );
|
||||||
|
assertTrue( p.getWidgets().size() == 3, "has three Widgets" );
|
||||||
tx = session.beginTransaction();
|
Iterator iter = p.getWidgets().iterator();
|
||||||
session.clear();
|
assertEquals( "axel", ( (Widgets) iter.next() ).getName() );
|
||||||
p = session.get( Products.class, p.getId() );
|
assertEquals( "hammer", ( (Widgets) iter.next() ).getName() );
|
||||||
assertTrue( p.getWidgets().size() == 3, "has three Widgets" );
|
assertEquals( "screwdriver", ( (Widgets) iter.next() ).getName() );
|
||||||
Iterator iter = p.getWidgets().iterator();
|
|
||||||
assertEquals( "axel", ( (Widgets) iter.next() ).getName() );
|
|
||||||
assertEquals( "hammer", ( (Widgets) iter.next() ).getName() );
|
|
||||||
assertEquals( "screwdriver", ( (Widgets) iter.next() ).getName() );
|
|
||||||
tx.commit();
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
if ( tx.isActive() ) {
|
|
||||||
tx.rollback();
|
|
||||||
}
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -88,60 +78,50 @@ public void testOrderByName(SessionFactoryScope scope) {
|
|||||||
reason = "HHH-8190, uses Teradata reserved word - summary"
|
reason = "HHH-8190, uses Teradata reserved word - summary"
|
||||||
)
|
)
|
||||||
public void testOrderByWithDottedNotation(SessionFactoryScope scope) {
|
public void testOrderByWithDottedNotation(SessionFactoryScope scope) {
|
||||||
scope.inSession(
|
scope.inTransaction(
|
||||||
session -> {
|
session -> {
|
||||||
Transaction tx = session.beginTransaction();
|
BugSystem bs = new BugSystem();
|
||||||
try {
|
HashSet<Bug> set = new HashSet<>();
|
||||||
BugSystem bs = new BugSystem();
|
|
||||||
HashSet<Bug> set = new HashSet<>();
|
|
||||||
|
|
||||||
Bug bug = new Bug();
|
Bug bug = new Bug();
|
||||||
bug.setDescription( "JPA-2 locking" );
|
bug.setDescription( "JPA-2 locking" );
|
||||||
bug.setSummary( "JPA-2 impl locking" );
|
bug.setSummary( "JPA-2 impl locking" );
|
||||||
Person p = new Person();
|
Person p = new Person();
|
||||||
p.setFirstName( "Scott" );
|
p.setFirstName( "Scott" );
|
||||||
p.setLastName( "Marlow" );
|
p.setLastName( "Marlow" );
|
||||||
bug.setReportedBy( p );
|
bug.setReportedBy( p );
|
||||||
set.add( bug );
|
set.add( bug );
|
||||||
|
|
||||||
bug = new Bug();
|
bug = new Bug();
|
||||||
bug.setDescription( "JPA-2 annotations" );
|
bug.setDescription( "JPA-2 annotations" );
|
||||||
bug.setSummary( "JPA-2 impl annotations" );
|
bug.setSummary( "JPA-2 impl annotations" );
|
||||||
p = new Person();
|
p = new Person();
|
||||||
p.setFirstName( "Emmanuel" );
|
p.setFirstName( "Emmanuel" );
|
||||||
p.setLastName( "Bernard" );
|
p.setLastName( "Bernard" );
|
||||||
bug.setReportedBy( p );
|
bug.setReportedBy( p );
|
||||||
set.add( bug );
|
set.add( bug );
|
||||||
|
|
||||||
bug = new Bug();
|
bug = new Bug();
|
||||||
bug.setDescription( "Implement JPA-2 criteria" );
|
bug.setDescription( "Implement JPA-2 criteria" );
|
||||||
bug.setSummary( "JPA-2 impl criteria" );
|
bug.setSummary( "JPA-2 impl criteria" );
|
||||||
p = new Person();
|
p = new Person();
|
||||||
p.setFirstName( "Steve" );
|
p.setFirstName( "Steve" );
|
||||||
p.setLastName( "Ebersole" );
|
p.setLastName( "Ebersole" );
|
||||||
bug.setReportedBy( p );
|
bug.setReportedBy( p );
|
||||||
set.add( bug );
|
set.add( bug );
|
||||||
|
|
||||||
bs.setBugs( set );
|
bs.setBugs( set );
|
||||||
session.persist( bs );
|
session.persist( bs );
|
||||||
tx.commit();
|
session.getTransaction().commit();
|
||||||
|
|
||||||
tx = session.beginTransaction();
|
session.beginTransaction();
|
||||||
session.clear();
|
session.clear();
|
||||||
bs = session.get( BugSystem.class, bs.getId() );
|
bs = session.get( BugSystem.class, bs.getId() );
|
||||||
assertTrue( bs.getBugs().size() == 3, "has three bugs" );
|
assertTrue( bs.getBugs().size() == 3, "has three bugs" );
|
||||||
Iterator iter = bs.getBugs().iterator();
|
Iterator iter = bs.getBugs().iterator();
|
||||||
assertEquals( "Emmanuel", ( (Bug) iter.next() ).getReportedBy().getFirstName() );
|
assertEquals( "Emmanuel", ( (Bug) iter.next() ).getReportedBy().getFirstName() );
|
||||||
assertEquals( "Steve", ( (Bug) iter.next() ).getReportedBy().getFirstName() );
|
assertEquals( "Steve", ( (Bug) iter.next() ).getReportedBy().getFirstName() );
|
||||||
assertEquals( "Scott", ( (Bug) iter.next() ).getReportedBy().getFirstName() );
|
assertEquals( "Scott", ( (Bug) iter.next() ).getReportedBy().getFirstName() );
|
||||||
tx.commit();
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
if ( tx.isActive() ) {
|
|
||||||
tx.rollback();
|
|
||||||
}
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -39,5 +39,4 @@ public Set<Widgets> getWidgets() {
|
|||||||
public void setWidgets(Set<Widgets> widgets) {
|
public void setWidgets(Set<Widgets> widgets) {
|
||||||
this.widgets = widgets;
|
this.widgets = widgets;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,15 @@
|
|||||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||||
*/
|
*/
|
||||||
package org.hibernate.orm.test.annotations.collectionelement;
|
package org.hibernate.orm.test.annotations.collectionelement;
|
||||||
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Inheritance;
|
||||||
|
import javax.persistence.InheritanceType;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
|
@Inheritance(strategy = InheritanceType.JOINED)
|
||||||
public class Widgets {
|
public class Widgets {
|
||||||
private String name;
|
private String name;
|
||||||
private int id;
|
private int id;
|
||||||
@ -18,6 +22,7 @@ public Widgets() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Column(name = "name_1")
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
@ -36,4 +41,14 @@ public void setId(int id) {
|
|||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public static class Widget1 extends Widgets{
|
||||||
|
private String name1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public static class Widget2 extends Widgets{
|
||||||
|
private String name2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,9 +33,8 @@ public class TestBasicOps {
|
|||||||
public void testLoadAndStore(SessionFactoryScope scope) {
|
public void testLoadAndStore(SessionFactoryScope scope) {
|
||||||
Query q = new Query( new Location( "first", Location.Type.COUNTY ) );
|
Query q = new Query( new Location( "first", Location.Type.COUNTY ) );
|
||||||
scope.inTransaction(
|
scope.inTransaction(
|
||||||
session -> {
|
session ->
|
||||||
session.save( q );
|
session.save( q )
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
scope.inTransaction(
|
scope.inTransaction(
|
||||||
@ -52,47 +51,37 @@ public void testLoadAndStore(SessionFactoryScope scope) {
|
|||||||
@Test
|
@Test
|
||||||
@TestForIssue(jiraKey = "HHH-7072")
|
@TestForIssue(jiraKey = "HHH-7072")
|
||||||
public void testEmbeddableWithNullables(SessionFactoryScope scope) {
|
public void testEmbeddableWithNullables(SessionFactoryScope scope) {
|
||||||
scope.inSession(
|
scope.inTransaction(
|
||||||
session -> {
|
session -> {
|
||||||
|
Query q = new Query( new Location( null, Location.Type.COMMUNE ) );
|
||||||
|
session.save( q );
|
||||||
|
session.getTransaction().commit();
|
||||||
|
session.clear();
|
||||||
|
|
||||||
Transaction transaction = session.beginTransaction();
|
Transaction transaction = session.beginTransaction();
|
||||||
try {
|
q.getIncludedLocations().add( new Location( null, Location.Type.COUNTY ) );
|
||||||
Query q = new Query( new Location( null, Location.Type.COMMUNE ) );
|
session.update( q );
|
||||||
session.save( q );
|
transaction.commit();
|
||||||
transaction.commit();
|
session.clear();
|
||||||
session.clear();
|
|
||||||
|
|
||||||
transaction = session.beginTransaction();
|
transaction = session.beginTransaction();
|
||||||
q.getIncludedLocations().add( new Location( null, Location.Type.COUNTY ) );
|
q = session.get( Query.class, q.getId() );
|
||||||
session.update( q );
|
|
||||||
transaction.commit();
|
|
||||||
session.clear();
|
|
||||||
|
|
||||||
transaction = session.beginTransaction();
|
|
||||||
q = (Query) session.get( Query.class, q.getId() );
|
|
||||||
// assertEquals( 2, q.getIncludedLocations().size() );
|
// assertEquals( 2, q.getIncludedLocations().size() );
|
||||||
transaction.commit();
|
transaction.commit();
|
||||||
session.clear();
|
session.clear();
|
||||||
|
|
||||||
transaction = session.beginTransaction();
|
transaction = session.beginTransaction();
|
||||||
Iterator<Location> itr = q.getIncludedLocations().iterator();
|
Iterator<Location> itr = q.getIncludedLocations().iterator();
|
||||||
itr.next();
|
itr.next();
|
||||||
itr.remove();
|
itr.remove();
|
||||||
session.update( q );
|
session.update( q );
|
||||||
transaction.commit();
|
transaction.commit();
|
||||||
session.clear();
|
session.clear();
|
||||||
|
|
||||||
transaction = session.beginTransaction();
|
session.beginTransaction();
|
||||||
q = (Query) session.get( Query.class, q.getId() );
|
q = session.get( Query.class, q.getId() );
|
||||||
assertEquals( 1, q.getIncludedLocations().size() );
|
assertEquals( 1, q.getIncludedLocations().size() );
|
||||||
session.delete( q );
|
session.delete( q );
|
||||||
transaction.commit();
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
if ( transaction.isActive() ) {
|
|
||||||
transaction.rollback();
|
|
||||||
}
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user