Implement support for @Embeddable with ToMany and ToOne
This commit is contained in:
parent
3be2369110
commit
1363844ca0
|
@ -17,15 +17,19 @@ import java.util.function.Consumer;
|
|||
import java.util.function.Function;
|
||||
|
||||
import org.hibernate.NotYetImplementedFor6Exception;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.internal.util.collections.ArrayHelper;
|
||||
import org.hibernate.mapping.Component;
|
||||
import org.hibernate.mapping.Property;
|
||||
import org.hibernate.mapping.ToOne;
|
||||
import org.hibernate.metamodel.mapping.internal.MappingModelCreationHelper;
|
||||
import org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess;
|
||||
import org.hibernate.metamodel.mapping.internal.SingularAssociationAttributeMapping;
|
||||
import org.hibernate.metamodel.spi.EmbeddableRepresentationStrategy;
|
||||
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
|
||||
import org.hibernate.persister.entity.EntityPersister;
|
||||
import org.hibernate.query.NavigablePath;
|
||||
import org.hibernate.sql.ast.Clause;
|
||||
import org.hibernate.sql.ast.tree.from.TableGroup;
|
||||
|
@ -34,7 +38,9 @@ import org.hibernate.sql.results.graph.DomainResult;
|
|||
import org.hibernate.sql.results.graph.DomainResultCreationState;
|
||||
import org.hibernate.sql.results.graph.Fetchable;
|
||||
import org.hibernate.type.BasicType;
|
||||
import org.hibernate.type.CollectionType;
|
||||
import org.hibernate.type.CompositeType;
|
||||
import org.hibernate.type.EntityType;
|
||||
import org.hibernate.type.Type;
|
||||
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
|
||||
import org.hibernate.type.spi.TypeConfiguration;
|
||||
|
@ -114,7 +120,8 @@ public class EmbeddableMappingType implements ManagedMappingType {
|
|||
while ( propertyIterator.hasNext() ) {
|
||||
final Property bootPropertyDescriptor = propertyIterator.next();
|
||||
|
||||
if ( subtypes[ attributeIndex ] instanceof BasicType ) {
|
||||
final Type subtype = subtypes[attributeIndex];
|
||||
if ( subtype instanceof BasicType ) {
|
||||
attributeMappings.put(
|
||||
bootPropertyDescriptor.getName(),
|
||||
MappingModelCreationHelper.buildBasicAttributeMapping(
|
||||
|
@ -122,7 +129,7 @@ public class EmbeddableMappingType implements ManagedMappingType {
|
|||
attributeIndex,
|
||||
bootPropertyDescriptor,
|
||||
this,
|
||||
(BasicType) subtypes[ attributeIndex ],
|
||||
(BasicType) subtype,
|
||||
containingTableExpression,
|
||||
mappedColumnExpressions.get( columnPosition++ ),
|
||||
representationStrategy.resolvePropertyAccess( bootPropertyDescriptor ),
|
||||
|
@ -131,8 +138,8 @@ public class EmbeddableMappingType implements ManagedMappingType {
|
|||
)
|
||||
);
|
||||
}
|
||||
else if ( subtypes[ attributeIndex ] instanceof CompositeType ) {
|
||||
final CompositeType subCompositeType = (CompositeType) subtypes[ attributeIndex ];
|
||||
else if ( subtype instanceof CompositeType ) {
|
||||
final CompositeType subCompositeType = (CompositeType) subtype;
|
||||
final int columnSpan = subCompositeType.getColumnSpan( creationProcess.getCreationContext().getSessionFactory() );
|
||||
|
||||
attributeMappings.put(
|
||||
|
@ -153,6 +160,53 @@ public class EmbeddableMappingType implements ManagedMappingType {
|
|||
|
||||
columnPosition += columnSpan;
|
||||
}
|
||||
else {
|
||||
final EntityPersister entityPersister = creationProcess
|
||||
.getEntityPersister( bootDescriptor.getOwner().getEntityName() );
|
||||
if ( subtype instanceof CollectionType ) {
|
||||
attributeMappings.put(
|
||||
bootPropertyDescriptor.getName(),
|
||||
MappingModelCreationHelper.buildPluralAttributeMapping(
|
||||
bootPropertyDescriptor.getName(),
|
||||
attributeIndex,
|
||||
bootPropertyDescriptor,
|
||||
entityPersister,
|
||||
representationStrategy.resolvePropertyAccess( bootPropertyDescriptor ),
|
||||
compositeType.getCascadeStyle( attributeIndex),
|
||||
compositeType.getFetchMode( attributeIndex ),
|
||||
creationProcess
|
||||
)
|
||||
);
|
||||
}
|
||||
else if ( subtype instanceof EntityType ) {
|
||||
final Dialect dialect = creationProcess.getCreationContext()
|
||||
.getSessionFactory()
|
||||
.getJdbcServices()
|
||||
.getDialect();
|
||||
|
||||
final SingularAssociationAttributeMapping singularAssociationAttributeMapping = MappingModelCreationHelper.buildSingularAssociationAttributeMapping(
|
||||
bootPropertyDescriptor.getName(),
|
||||
attributeIndex,
|
||||
bootPropertyDescriptor,
|
||||
entityPersister,
|
||||
(EntityType) subtype,
|
||||
getRepresentationStrategy().resolvePropertyAccess( bootPropertyDescriptor ),
|
||||
compositeType.getCascadeStyle( attributeIndex ),
|
||||
creationProcess
|
||||
);
|
||||
MappingModelCreationHelper.interpretKeyDescriptor(
|
||||
singularAssociationAttributeMapping,
|
||||
bootPropertyDescriptor,
|
||||
(ToOne) bootPropertyDescriptor.getValue(),
|
||||
entityPersister,
|
||||
dialect,
|
||||
creationProcess
|
||||
);
|
||||
attributeMappings.put( bootPropertyDescriptor.getName(), singularAssociationAttributeMapping );
|
||||
// todo (6.0) : not sure it is always correct
|
||||
columnPosition++;
|
||||
}
|
||||
}
|
||||
|
||||
attributeIndex++;
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ import org.hibernate.engine.FetchStrategy;
|
|||
import org.hibernate.engine.FetchTiming;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.internal.util.collections.CollectionHelper;
|
||||
import org.hibernate.sql.ast.tree.from.TableGroupJoinProducer;
|
||||
import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
|
||||
import org.hibernate.sql.results.graph.DomainResult;
|
||||
import org.hibernate.sql.results.graph.DomainResultCreationState;
|
||||
|
@ -246,13 +247,14 @@ public class EmbeddedAttributeMapping
|
|||
lhs
|
||||
);
|
||||
|
||||
lhs.addTableGroupJoin( new TableGroupJoin( navigablePath, SqlAstJoinType.INNER, compositeTableGroup, null ) );
|
||||
|
||||
return new TableGroupJoin(
|
||||
TableGroupJoin tableGroupJoin = new TableGroupJoin(
|
||||
navigablePath,
|
||||
sqlAstJoinType,
|
||||
compositeTableGroup
|
||||
);
|
||||
lhs.addTableGroupJoin( tableGroupJoin );
|
||||
|
||||
return tableGroupJoin;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -13,6 +13,7 @@ import java.util.SortedMap;
|
|||
import java.util.SortedSet;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.hibernate.FetchMode;
|
||||
import org.hibernate.LockMode;
|
||||
import org.hibernate.MappingException;
|
||||
import org.hibernate.NotYetImplementedFor6Exception;
|
||||
|
@ -679,6 +680,7 @@ public class MappingModelCreationHelper {
|
|||
ManagedMappingType declaringType,
|
||||
PropertyAccess propertyAccess,
|
||||
CascadeStyle cascadeStyle,
|
||||
FetchMode fetchMode,
|
||||
MappingModelCreationProcess creationProcess) {
|
||||
|
||||
final Collection bootValueMapping = (Collection) bootProperty.getValue();
|
||||
|
@ -873,7 +875,7 @@ public class MappingModelCreationHelper {
|
|||
};
|
||||
|
||||
final FetchStyle style = FetchStrategyHelper.determineFetchStyleByMetadata(
|
||||
( (OuterJoinLoadable) declaringType ).getFetchMode( stateArrayPosition ),
|
||||
fetchMode,
|
||||
collectionDescriptor.getCollectionType(),
|
||||
sessionFactory
|
||||
);
|
||||
|
|
|
@ -140,8 +140,6 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
|
|||
creationState.getSqlAstCreationState().getCreationContext()
|
||||
);
|
||||
|
||||
lhsTableGroup.addTableGroupJoin( tableGroupJoin );
|
||||
|
||||
sqlAstCreationState.getFromClauseAccess().registerTableGroup(
|
||||
fetchablePath,
|
||||
tableGroupJoin.getJoinedGroup()
|
||||
|
@ -327,20 +325,24 @@ public class SingularAssociationAttributeMapping extends AbstractSingularAttribu
|
|||
.getFromClauseAccess()
|
||||
.findTableGroup( parentParentNavigablePath )
|
||||
.getModelPart();
|
||||
// final SingularAssociationAttributeMapping part = (SingularAssociationAttributeMapping) modelPart
|
||||
// .findSubPart( panentNaviblePath.getLocalName(), null );
|
||||
final EntityAssociationMapping part = (EntityAssociationMapping) modelPart.findSubPart( panentNaviblePath.getLocalName(), null );
|
||||
|
||||
final ModelPart subPart = modelPart.findSubPart( panentNaviblePath.getLocalName(), null );
|
||||
if ( subPart instanceof EntityAssociationMapping ) {
|
||||
final EntityAssociationMapping part = (EntityAssociationMapping) subPart;
|
||||
|
||||
if ( panentNaviblePath.getLocalName().equals( referencedPropertyName )
|
||||
&& part.getFetchableName().equals( referencedPropertyName ) ) {
|
||||
return true;
|
||||
}
|
||||
else if ( part.getKeyTargetMatchPart() != null
|
||||
// && part.getKeyTargetMatchPart().equals( this ) ) {
|
||||
&& part.getKeyTargetMatchPart().getPartName().equals( getAttributeName() ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -5824,7 +5824,6 @@ public abstract class AbstractEntityPersister
|
|||
final Dialect dialect = creationProcess.getCreationContext()
|
||||
.getSessionFactory()
|
||||
.getJdbcServices()
|
||||
.getJdbcEnvironment()
|
||||
.getDialect();
|
||||
|
||||
MappingModelCreationHelper.interpretKeyDescriptor(
|
||||
|
@ -5836,6 +5835,8 @@ public abstract class AbstractEntityPersister
|
|||
creationProcess
|
||||
);
|
||||
} );
|
||||
|
||||
singularAssociationsToFinilize.clear();
|
||||
}
|
||||
|
||||
protected static SqmMultiTableMutationStrategy interpretSqmMultiTableStrategy(
|
||||
|
@ -6063,6 +6064,7 @@ public abstract class AbstractEntityPersister
|
|||
this,
|
||||
propertyAccess,
|
||||
tupleAttrDefinition.getCascadeStyle(),
|
||||
getFetchMode( stateArrayPosition ),
|
||||
creationProcess
|
||||
);
|
||||
}
|
||||
|
|
|
@ -370,10 +370,8 @@ public abstract class AbstractSqlAstWalker
|
|||
protected void processTableGroupJoin(TableGroupJoin tableGroupJoin) {
|
||||
final TableGroup joinedGroup = tableGroupJoin.getJoinedGroup();
|
||||
|
||||
//noinspection StatementWithEmptyBody
|
||||
if ( joinedGroup instanceof VirtualTableGroup ) {
|
||||
// nothing to do
|
||||
// todo (6.0) : join predicates?
|
||||
processTableGroupJoins( tableGroupJoin.getJoinedGroup());
|
||||
}
|
||||
else {
|
||||
appendSql( EMPTY_STRING );
|
||||
|
|
|
@ -10,6 +10,7 @@ import java.util.IdentityHashMap;
|
|||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
|
||||
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
|
||||
import org.hibernate.metamodel.mapping.SingularAttributeMapping;
|
||||
import org.hibernate.metamodel.mapping.StateArrayContributorMapping;
|
||||
|
@ -48,11 +49,12 @@ public abstract class AbstractEmbeddableInitializer extends AbstractFetchParentA
|
|||
this.embeddedModelPartDescriptor = resultDescriptor.getReferencedMappingContainer();
|
||||
this.fetchParentAccess = fetchParentAccess;
|
||||
|
||||
final int numOfAttrs = embeddedModelPartDescriptor.getEmbeddableTypeDescriptor().getNumberOfAttributeMappings();
|
||||
final EmbeddableMappingType embeddableTypeDescriptor = embeddedModelPartDescriptor.getEmbeddableTypeDescriptor();
|
||||
final int numOfAttrs = embeddableTypeDescriptor.getNumberOfAttributeMappings();
|
||||
this.resolvedValues = new Object[ numOfAttrs ];
|
||||
this.assemblerMap = new IdentityHashMap<>( numOfAttrs );
|
||||
|
||||
this.embeddedModelPartDescriptor.getEmbeddableTypeDescriptor().visitStateArrayContributors(
|
||||
embeddableTypeDescriptor.visitStateArrayContributors(
|
||||
stateArrayContributor -> {
|
||||
final Fetch fetch = resultDescriptor.findFetch( stateArrayContributor.getFetchableName() );
|
||||
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
* 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.annotations.override;
|
||||
package org.hibernate.orm.test.annotations.override;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
|
@ -4,7 +4,8 @@
|
|||
* 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.annotations.override;
|
||||
package org.hibernate.orm.test.annotations.override;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
/**
|
|
@ -4,7 +4,7 @@
|
|||
* 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.annotations.override;
|
||||
package org.hibernate.orm.test.annotations.override;
|
||||
|
||||
import org.hibernate.dialect.H2Dialect;
|
||||
import org.hibernate.mapping.Table;
|
|
@ -0,0 +1,150 @@
|
|||
/*
|
||||
* 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.override;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.boot.spi.MetadataImplementor;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
|
||||
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.hibernate.test.util.SchemaUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Emmanuel Bernard
|
||||
*/
|
||||
@DomainModel(
|
||||
annotatedClasses = {
|
||||
Employee.class,
|
||||
Location.class,
|
||||
Move.class,
|
||||
Trip.class,
|
||||
PhoneNumber.class,
|
||||
Addr.class,
|
||||
SocialSite.class,
|
||||
SocialTouchPoints.class
|
||||
}
|
||||
)
|
||||
@ServiceRegistry(settings = {
|
||||
@ServiceRegistry.Setting(name = AvailableSettings.IMPLICIT_NAMING_STRATEGY, value = "legacy-jpa")
|
||||
})
|
||||
@SessionFactory
|
||||
public class AssociationOverrideTest {
|
||||
|
||||
@Test
|
||||
public void testOverriding(SessionFactoryScope scope) {
|
||||
Location paris = new Location();
|
||||
paris.setName( "Paris" );
|
||||
Location atlanta = new Location();
|
||||
atlanta.setName( "Atlanta" );
|
||||
Trip trip = new Trip();
|
||||
trip.setFrom( paris );
|
||||
//trip.setTo( atlanta );
|
||||
scope.inSession(
|
||||
session -> {
|
||||
Transaction tx = session.beginTransaction();
|
||||
session.persist( paris );
|
||||
session.persist( atlanta );
|
||||
try {
|
||||
session.persist( trip );
|
||||
session.flush();
|
||||
fail( "Should be non nullable" );
|
||||
}
|
||||
catch (PersistenceException e) {
|
||||
//success
|
||||
}
|
||||
finally {
|
||||
tx.rollback();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSchemaCreation(SessionFactoryScope scope) {
|
||||
final MetadataImplementor metadata = scope.getMetadataImplementor();
|
||||
|
||||
assertTrue( SchemaUtil.isTablePresent( "Employee", metadata ) );
|
||||
assertTrue(
|
||||
"Overridden @JoinColumn fails",
|
||||
SchemaUtil.isColumnPresent( "Employee", "fld_address_fk", metadata )
|
||||
);
|
||||
|
||||
assertTrue( "Overridden @JoinTable name fails", SchemaUtil.isTablePresent( "tbl_empl_sites", metadata ) );
|
||||
assertTrue(
|
||||
"Overridden @JoinTable with default @JoinColumn fails",
|
||||
SchemaUtil.isColumnPresent( "tbl_empl_sites", "employee_id", metadata )
|
||||
);
|
||||
assertTrue(
|
||||
"Overridden @JoinTable.inverseJoinColumn fails",
|
||||
SchemaUtil.isColumnPresent( "tbl_empl_sites", "to_website_fk", metadata )
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDottedNotation(SessionFactoryScope scope) {
|
||||
scope.inSession(
|
||||
session -> {
|
||||
Transaction tx = session.beginTransaction();
|
||||
try {
|
||||
ContactInfo ci = new ContactInfo();
|
||||
Addr address = new Addr();
|
||||
address.setCity( "Boston" );
|
||||
address.setCountry( "USA" );
|
||||
address.setState( "MA" );
|
||||
address.setStreet( "27 School Street" );
|
||||
address.setZipcode( "02108" );
|
||||
ci.setAddr( address );
|
||||
List<PhoneNumber> phoneNumbers = new ArrayList();
|
||||
PhoneNumber num = new PhoneNumber();
|
||||
num.setNumber( 5577188 );
|
||||
Employee e = new Employee();
|
||||
Collection employeeList = new ArrayList();
|
||||
employeeList.add( e );
|
||||
e.setContactInfo( ci );
|
||||
num.setEmployees( employeeList );
|
||||
phoneNumbers.add( num );
|
||||
ci.setPhoneNumbers( phoneNumbers );
|
||||
SocialTouchPoints socialPoints = new SocialTouchPoints();
|
||||
List<SocialSite> sites = new ArrayList<>();
|
||||
SocialSite site = new SocialSite();
|
||||
site.setEmployee( employeeList );
|
||||
site.setWebsite( "www.jboss.org" );
|
||||
sites.add( site );
|
||||
socialPoints.setWebsite( sites );
|
||||
ci.setSocial( socialPoints );
|
||||
session.persist( e );
|
||||
tx.commit();
|
||||
|
||||
tx.begin();
|
||||
session.clear();
|
||||
session.get( Employee.class, e.getId() );
|
||||
tx.commit();
|
||||
}
|
||||
catch (Exception e) {
|
||||
if ( tx != null && tx.isActive() ) {
|
||||
tx.rollback();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -4,7 +4,7 @@
|
|||
* 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.annotations.override;
|
||||
package org.hibernate.orm.test.annotations.override;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.hibernate.test.util.SchemaUtil;
|
|
@ -4,7 +4,7 @@
|
|||
* 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.annotations.override;
|
||||
package org.hibernate.orm.test.annotations.override;
|
||||
|
||||
import javax.persistence.AssociationOverride;
|
||||
import javax.persistence.AttributeOverride;
|
|
@ -4,7 +4,8 @@
|
|||
* 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.annotations.override;
|
||||
package org.hibernate.orm.test.annotations.override;
|
||||
|
||||
import java.util.List;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Embeddable;
|
|
@ -4,7 +4,8 @@
|
|||
* 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.annotations.override;
|
||||
package org.hibernate.orm.test.annotations.override;
|
||||
|
||||
import javax.persistence.AssociationOverride;
|
||||
import javax.persistence.AssociationOverrides;
|
||||
import javax.persistence.Embedded;
|
|
@ -4,7 +4,7 @@
|
|||
* 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.annotations.override;
|
||||
package org.hibernate.orm.test.annotations.override;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashSet;
|
|
@ -4,7 +4,7 @@
|
|||
* 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.annotations.override;
|
||||
package org.hibernate.orm.test.annotations.override;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
@ -19,6 +19,7 @@ import org.hibernate.boot.spi.MetadataImplementor;
|
|||
import org.hibernate.testing.FailureExpected;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
|
@ -4,9 +4,8 @@
|
|||
* 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.override;
|
||||
|
||||
//$Id$
|
||||
package org.hibernate.test.annotations.override;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
|
@ -4,9 +4,8 @@
|
|||
* 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.override;
|
||||
|
||||
//$Id$
|
||||
package org.hibernate.test.annotations.override;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
|
@ -4,7 +4,8 @@
|
|||
* 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.annotations.override;
|
||||
package org.hibernate.orm.test.annotations.override;
|
||||
|
||||
import java.util.Collection;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
|
@ -4,7 +4,8 @@
|
|||
* 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.annotations.override;
|
||||
package org.hibernate.orm.test.annotations.override;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import javax.persistence.Embeddable;
|
||||
|
|
@ -4,7 +4,8 @@
|
|||
* 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.annotations.override;
|
||||
package org.hibernate.orm.test.annotations.override;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import javax.persistence.AttributeOverride;
|
|
@ -4,7 +4,8 @@
|
|||
* 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.annotations.override;
|
||||
package org.hibernate.orm.test.annotations.override;
|
||||
|
||||
import java.util.Collection;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
|
@ -4,7 +4,8 @@
|
|||
* 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.annotations.override;
|
||||
package org.hibernate.orm.test.annotations.override;
|
||||
|
||||
import java.util.List;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Embeddable;
|
|
@ -4,9 +4,8 @@
|
|||
* 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.override;
|
||||
|
||||
//$Id$
|
||||
package org.hibernate.test.annotations.override;
|
||||
import javax.persistence.AssociationOverride;
|
||||
import javax.persistence.AssociationOverrides;
|
||||
import javax.persistence.Entity;
|
|
@ -1,127 +0,0 @@
|
|||
/*
|
||||
* 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.annotations.override;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.boot.MetadataBuilder;
|
||||
import org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.hibernate.test.util.SchemaUtil;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Emmanuel Bernard
|
||||
*/
|
||||
public class AssociationOverrideTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
@Test
|
||||
public void testOverriding() throws Exception {
|
||||
Location paris = new Location();
|
||||
paris.setName( "Paris" );
|
||||
Location atlanta = new Location();
|
||||
atlanta.setName( "Atlanta" );
|
||||
Trip trip = new Trip();
|
||||
trip.setFrom( paris );
|
||||
//trip.setTo( atlanta );
|
||||
Session s = openSession();
|
||||
Transaction tx = s.beginTransaction();
|
||||
s.persist( paris );
|
||||
s.persist( atlanta );
|
||||
try {
|
||||
s.persist( trip );
|
||||
s.flush();
|
||||
fail( "Should be non nullable" );
|
||||
}
|
||||
catch (PersistenceException e) {
|
||||
//success
|
||||
}
|
||||
finally {
|
||||
tx.rollback();
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDottedNotation() throws Exception {
|
||||
assertTrue( SchemaUtil.isTablePresent( "Employee", metadata() ) );
|
||||
assertTrue( "Overridden @JoinColumn fails",
|
||||
SchemaUtil.isColumnPresent( "Employee", "fld_address_fk", metadata() ) );
|
||||
|
||||
assertTrue( "Overridden @JoinTable name fails", SchemaUtil.isTablePresent( "tbl_empl_sites", metadata() ) );
|
||||
assertTrue( "Overridden @JoinTable with default @JoinColumn fails",
|
||||
SchemaUtil.isColumnPresent( "tbl_empl_sites", "employee_id", metadata() ) );
|
||||
assertTrue( "Overridden @JoinTable.inverseJoinColumn fails",
|
||||
SchemaUtil.isColumnPresent( "tbl_empl_sites", "to_website_fk", metadata() ) );
|
||||
|
||||
Session s = openSession();
|
||||
Transaction tx = s.beginTransaction();
|
||||
ContactInfo ci = new ContactInfo();
|
||||
Addr address = new Addr();
|
||||
address.setCity("Boston");
|
||||
address.setCountry("USA");
|
||||
address.setState("MA");
|
||||
address.setStreet("27 School Street");
|
||||
address.setZipcode("02108");
|
||||
ci.setAddr(address);
|
||||
List<PhoneNumber> phoneNumbers = new ArrayList();
|
||||
PhoneNumber num = new PhoneNumber();
|
||||
num.setNumber(5577188);
|
||||
Employee e = new Employee();
|
||||
Collection employeeList = new ArrayList();
|
||||
employeeList.add(e);
|
||||
e.setContactInfo(ci);
|
||||
num.setEmployees(employeeList);
|
||||
phoneNumbers.add(num);
|
||||
ci.setPhoneNumbers(phoneNumbers);
|
||||
SocialTouchPoints socialPoints = new SocialTouchPoints();
|
||||
List<SocialSite> sites = new ArrayList<SocialSite>();
|
||||
SocialSite site = new SocialSite();
|
||||
site.setEmployee(employeeList);
|
||||
site.setWebsite("www.jboss.org");
|
||||
sites.add(site);
|
||||
socialPoints.setWebsite(sites);
|
||||
ci.setSocial(socialPoints);
|
||||
s.persist(e);
|
||||
tx.commit();
|
||||
|
||||
tx = s.beginTransaction();
|
||||
s.clear();
|
||||
e = (Employee) s.get(Employee.class,e.getId());
|
||||
tx.commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configureMetadataBuilder(MetadataBuilder metadataBuilder) {
|
||||
super.configureMetadataBuilder( metadataBuilder );
|
||||
metadataBuilder.applyImplicitNamingStrategy( ImplicitNamingStrategyLegacyJpaImpl.INSTANCE );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[]{
|
||||
Employee.class,
|
||||
Location.class,
|
||||
Move.class,
|
||||
Trip.class,
|
||||
PhoneNumber.class,
|
||||
Addr.class,
|
||||
SocialSite.class,
|
||||
SocialTouchPoints.class
|
||||
};
|
||||
}
|
||||
}
|
|
@ -401,5 +401,10 @@ public class SessionFactoryExtension
|
|||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MetadataImplementor getMetadataImplementor() {
|
||||
return modelScope.getDomainModel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ package org.hibernate.testing.orm.junit;
|
|||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.hibernate.boot.spi.MetadataImplementor;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
|
||||
|
@ -27,4 +28,6 @@ public interface SessionFactoryScope {
|
|||
|
||||
<T> T fromTransaction(Function<SessionImplementor, T> action);
|
||||
<T> T fromTransaction(SessionImplementor session, Function<SessionImplementor, T> action);
|
||||
|
||||
MetadataImplementor getMetadataImplementor();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue