Fix load NonAggregateIdentifier by id

This commit is contained in:
Andrea Boriero 2021-10-29 16:42:41 +02:00 committed by Christian Beikov
parent b971d76bf6
commit 9d08be53a6
54 changed files with 994 additions and 890 deletions

View File

@ -131,16 +131,16 @@ public class DefaultLoadEventListener implements LoadEventListener {
if ( cidMapping.getAttributeCount() == 1 ) {
final AttributeMapping singleIdAttribute = cidMapping.getAttributes().get( 0 );
if ( singleIdAttribute.getMappedType() instanceof EntityMappingType ) {
final EntityMappingType dependentIdTargetMapping = (EntityMappingType) singleIdAttribute.getMappedType();
final EntityIdentifierMapping dependentIdTargetIdMapping = dependentIdTargetMapping.getIdentifierMapping();
final JavaType dependentParentIdJtd = dependentIdTargetIdMapping.getMappedType().getMappedJavaTypeDescriptor();
if ( dependentParentIdJtd.getJavaTypeClass().isInstance( event.getEntityId() ) ) {
final EntityMappingType parentIdTargetMapping = (EntityMappingType) singleIdAttribute.getMappedType();
final EntityIdentifierMapping parentIdTargetIdMapping = parentIdTargetMapping.getIdentifierMapping();
final JavaType parentIdJtd = parentIdTargetIdMapping.getMappedType().getMappedJavaTypeDescriptor();
if ( parentIdJtd.getJavaTypeClass().isInstance( event.getEntityId() ) ) {
// yep that's what we have...
loadByDerivedIdentitySimplePkValue(
event,
loadType,
persister,
(EntityPersister) dependentIdTargetMapping
(EntityPersister) parentIdTargetMapping
);
return;
}
@ -158,14 +158,12 @@ public class DefaultLoadEventListener implements LoadEventListener {
LoadEvent event,
LoadType options,
EntityPersister dependentPersister,
// EmbeddedComponentType dependentIdType,
EntityPersister parentPersister) {
final EventSource session = event.getSession();
final EntityKey parentEntityKey = session.generateEntityKey( event.getEntityId(), parentPersister );
final Object parent = doLoad( event, parentPersister, parentEntityKey, options );
final Object dependent = dependentPersister.instantiate( parent, session );
dependentPersister.setPropertyValues( dependent, new Object[] {parent} );
final EntityKey dependentEntityKey = session.generateEntityKey( dependent, dependentPersister );
event.setEntityId( dependent );

View File

@ -615,7 +615,7 @@ public class EmbeddableMappingType implements ManagedMappingType, SelectableMapp
public void visitFetchables(
Consumer<Fetchable> fetchableConsumer,
EntityMappingType treatTargetType) {
visitAttributeMappings( attributeMapping -> fetchableConsumer.accept( (Fetchable) attributeMapping ) );
visitAttributeMappings( attributeMapping -> fetchableConsumer.accept( attributeMapping ) );
}
@Override

View File

@ -16,8 +16,8 @@ import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.ManyToOne;
import org.hibernate.mapping.Property;
import org.hibernate.metamodel.internal.AbstractCompositeIdentifierMapping;
import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
import org.hibernate.metamodel.mapping.EntityMappingType;
@ -42,8 +42,10 @@ import org.hibernate.type.ComponentType;
public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentifierMapping {
private final List<SingularAttributeMapping> idAttributeMappings;
private final Component bootCidDescriptor;
private final Component bootIdClassDescriptor;
private final ComponentType bootIdComponentType;
private final ComponentType bootCidComponentType;
private final boolean[] isBootIdPropertyManyToOne;
public NonAggregatedIdentifierMappingImpl(
EmbeddableMappingType embeddableDescriptor,
@ -64,8 +66,13 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif
);
this.idAttributeMappings = idAttributeMappings;
this.bootCidDescriptor = bootCidDescriptor;
this.bootIdClassDescriptor = bootIdClassDescriptor;
final int propertySpan = bootIdClassDescriptor.getPropertySpan();
isBootIdPropertyManyToOne = new boolean[propertySpan];
for ( int i = 0; i < propertySpan; i++ ) {
isBootIdPropertyManyToOne[i] = bootIdClassDescriptor.getProperty( i ).getValue() instanceof ManyToOne;
}
bootIdComponentType = (ComponentType) bootIdClassDescriptor.getType();
bootCidComponentType = (ComponentType) bootCidDescriptor.getType();
}
@Override
@ -81,50 +88,71 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif
@Override
public Object getIdentifier(Object entity, SharedSessionContractImplementor session) {
if ( hasContainingClass() ) {
final Serializable disassemble = bootCidDescriptor.getType().disassemble( entity, session, null );
return bootIdClassDescriptor.getType().assemble( disassemble, session, null );
final Serializable disassemble = bootCidComponentType.disassemble( entity, session, null );
return bootIdComponentType.assemble( disassemble, session, null );
}
else {
return entity;
}
}
@Override
public Object disassemble(Object value, SharedSessionContractImplementor session) {
if ( !getEmbeddableTypeDescriptor().getMappedJavaTypeDescriptor()
.getJavaTypeClass()
.isAssignableFrom( value.getClass() ) ) {
final Object[] result = new Object[idAttributeMappings.size()];
for ( int i = 0; i < idAttributeMappings.size(); i++ ) {
final AttributeMapping attributeMapping = idAttributeMappings.get( i );
Object o = attributeMapping.getPropertyAccess().getGetter().get( value );
result[i] = attributeMapping.disassemble( o, session );
}
return result;
}
return getEmbeddableTypeDescriptor().disassemble( value, session );
}
@Override
public void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session) {
final SessionFactoryImplementor factory = session.getFactory();
final Object[] propertyValues = ( (ComponentType) bootIdClassDescriptor.getType() )
.getPropertyValues( id, session );
forEachAttribute(
(position, attribute) -> {
Object propertyValue = propertyValues[position];
final Property property = bootIdClassDescriptor.getProperty( position );
if ( attribute instanceof ToOneAttributeMapping && !( property.getValue() instanceof ManyToOne ) ) {
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attribute;
final EntityPersister entityPersister = toOneAttributeMapping.getEntityMappingType()
.getEntityPersister();
final EntityKey entityKey = session.generateEntityKey(
propertyValue,
entityPersister
);
final PersistenceContext persistenceContext = session.getPersistenceContext();
// it is conceivable there is a proxy, so check that first
propertyValue = persistenceContext.getProxy( entityKey );
if ( propertyValue == null ) {
// otherwise look for an initialized version
propertyValue = persistenceContext.getEntity( entityKey );
if ( !getEmbeddableTypeDescriptor().getMappedJavaTypeDescriptor().getJavaTypeClass().isAssignableFrom( id.getClass()) ) {
idAttributeMappings.get( 0 ).getPropertyAccess().getSetter().set( entity, id, session.getFactory() );
}
else {
final SessionFactoryImplementor factory = session.getFactory();
final Object[] propertyValues = bootIdComponentType.getPropertyValues( id, session );
forEachAttribute(
(position, attribute) -> {
Object propertyValue = propertyValues[position];
if ( attribute instanceof ToOneAttributeMapping && !isBootIdPropertyManyToOne[position] ) {
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attribute;
final EntityPersister entityPersister = toOneAttributeMapping.getEntityMappingType()
.getEntityPersister();
final EntityKey entityKey = session.generateEntityKey(
propertyValue,
entityPersister
);
final PersistenceContext persistenceContext = session.getPersistenceContext();
// it is conceivable there is a proxy, so check that first
propertyValue = persistenceContext.getProxy( entityKey );
if ( propertyValue == null ) {
// get the association out of the entity itself
propertyValue = factory.getMetamodel()
.findEntityDescriptor( entity.getClass() )
.getPropertyValue( entity, toOneAttributeMapping.getAttributeName() );
// otherwise look for an initialized version
propertyValue = persistenceContext.getEntity( entityKey );
if ( propertyValue == null ) {
// get the association out of the entity itself
propertyValue = factory.getMetamodel()
.findEntityDescriptor( entity.getClass() )
.getPropertyValue( entity, toOneAttributeMapping.getAttributeName() );
}
}
}
attribute.getPropertyAccess()
.getSetter()
.set( entity, propertyValue, factory );
}
attribute.getPropertyAccess()
.getSetter()
.set( entity, propertyValue, factory );
}
);
);
}
}
@Override
@ -186,6 +214,6 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif
@Override
public boolean hasContainingClass() {
return bootIdClassDescriptor != bootCidDescriptor;
return bootIdComponentType != bootCidComponentType;
}
}

View File

@ -4819,10 +4819,6 @@ public abstract class AbstractEntityPersister
if ( entity instanceof SelfDirtinessTracker ) {
( (SelfDirtinessTracker) entity ).$$_hibernate_clearDirtyAttributes();
}
if ( singleIdEntityLoader instanceof Preparable ) {
( (Preparable) singleIdEntityLoader ).prepare();
}
}
public String[] getPropertyNames() {

View File

@ -0,0 +1,107 @@
/*
* 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.derivedidentities.bidirectional;
import org.hibernate.testing.TestForIssue;
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.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
@DomainModel(
annotatedClasses = {
Product.class,
OrderLine.class,
Order.class
}
)
@SessionFactory
public class CompositeDerivedIdentityTest {
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from OrderLine" ).executeUpdate();
session.createQuery( "delete from Order" ).executeUpdate();
session.createQuery( "delete from Product" ).executeUpdate();
}
);
}
@Test
public void testCreateProject(SessionFactoryScope scope) {
Product product = new Product();
product.setName( "Product 1" );
scope.inTransaction(
session -> {
session.save( product );
}
);
Order order = new Order();
order.setName( "Order 1" );
order.addLineItem( product, 2 );
scope.inTransaction(
session -> {
session.save( order );
}
);
Long orderId = order.getId();
scope.inTransaction(
session -> {
Order o = session.get( Order.class, orderId );
assertEquals( 1, o.getLineItems().size() );
session.delete( o );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-10476")
public void testBidirectonalKeyManyToOneId(SessionFactoryScope scope) {
Product product = new Product();
product.setName( "Product 1" );
scope.inTransaction(
session ->
session.save( product )
);
Order order = new Order();
order.setName( "Order 1" );
order.addLineItem( product, 2 );
scope.inTransaction(
session ->
session.save( order )
);
scope.inTransaction(
session -> {
OrderLine orderLine = order.getLineItems().iterator().next();
orderLine.setAmount( 5 );
OrderLine orderLineGotten = session.get( OrderLine.class, orderLine );
assertSame( orderLineGotten, orderLine );
assertEquals( Integer.valueOf( 2 ), orderLineGotten.getAmount() );
assertTrue( session.getPersistenceContext().isEntryFor( orderLineGotten ) );
assertFalse( session.getPersistenceContext().isEntryFor( orderLineGotten.getOrder() ) );
assertFalse( session.getPersistenceContext().isEntryFor( orderLineGotten.getProduct() ) );
}
);
}
}

View File

@ -4,12 +4,23 @@
* 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.derivedidentities.bidirectional;
package org.hibernate.orm.test.annotations.derivedidentities.bidirectional;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import org.hibernate.Hibernate;
import org.hibernate.testing.TestForIssue;
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.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
@ -18,27 +29,25 @@ import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import org.hibernate.Hibernate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
public class ManyToOneEagerDerivedIdFetchModeJoinTest extends BaseCoreFunctionalTestCase {
@DomainModel(
annotatedClasses = {
ManyToOneEagerDerivedIdFetchModeJoinTest.Foo.class,
ManyToOneEagerDerivedIdFetchModeJoinTest.Bar.class,
}
)
@SessionFactory
public class ManyToOneEagerDerivedIdFetchModeJoinTest {
private Foo foo;
@Test
@TestForIssue(jiraKey = "HHH-14466")
public void testQuery() {
doInHibernate( this::sessionFactory, session -> {
public void testQuery(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo.id = :id" )
.setParameter( "id", foo.getId() )
.uniqueResult();
@ -50,14 +59,13 @@ public class ManyToOneEagerDerivedIdFetchModeJoinTest extends BaseCoreFunctional
assertEquals( 1, newBar.getFoo().getBars().size() );
assertSame( newBar, newBar.getFoo().getBars().iterator().next() );
assertEquals( "Some details", newBar.getDetails() );
});
} );
}
@Test
@TestForIssue(jiraKey = "HHH-14466")
public void testQueryById() {
doInHibernate( this::sessionFactory, session -> {
public void testQueryById(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo = :foo" )
.setParameter( "foo", foo )
.uniqueResult();
@ -69,14 +77,13 @@ public class ManyToOneEagerDerivedIdFetchModeJoinTest extends BaseCoreFunctional
assertEquals( 1, newBar.getFoo().getBars().size() );
assertSame( newBar, newBar.getFoo().getBars().iterator().next() );
assertEquals( "Some details", newBar.getDetails() );
});
} );
}
@Test
@TestForIssue(jiraKey = "HHH-14466")
public void testFindByPrimaryKey() {
doInHibernate( this::sessionFactory, session -> {
public void testFindByPrimaryKey(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Bar newBar = session.find( Bar.class, foo.getId() );
assertNotNull( newBar );
assertNotNull( newBar.getFoo() );
@ -86,14 +93,13 @@ public class ManyToOneEagerDerivedIdFetchModeJoinTest extends BaseCoreFunctional
assertEquals( 1, newBar.getFoo().getBars().size() );
assertSame( newBar, newBar.getFoo().getBars().iterator().next() );
assertEquals( "Some details", newBar.getDetails() );
});
} );
}
@Test
@TestForIssue(jiraKey = "HHH-14466")
public void testFindByInversePrimaryKey() {
doInHibernate( this::sessionFactory, session -> {
public void testFindByInversePrimaryKey(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Foo newFoo = session.find( Foo.class, foo.getId() );
assertNotNull( newFoo );
assertNotNull( newFoo.getBars() );
@ -101,13 +107,13 @@ public class ManyToOneEagerDerivedIdFetchModeJoinTest extends BaseCoreFunctional
assertEquals( 1, newFoo.getBars().size() );
assertSame( newFoo, newFoo.getBars().iterator().next().getFoo() );
assertEquals( "Some details", newFoo.getBars().iterator().next().getDetails() );
});
} );
}
@Before
public void setupData() {
this.foo = doInHibernate( this::sessionFactory, session -> {
@BeforeEach
public void setupData(SessionFactoryScope scope) {
this.foo = scope.fromTransaction( session -> {
Foo foo = new Foo();
foo.id = 1L;
session.persist( foo );
@ -126,25 +132,17 @@ public class ManyToOneEagerDerivedIdFetchModeJoinTest extends BaseCoreFunctional
assertEquals( foo.getId(), bar.getFoo().getId() );
return foo;
});
} );
}
@After
public void cleanupData() {
doInHibernate( this::sessionFactory, session -> {
@AfterEach
public void cleanupData(SessionFactoryScope scope) {
scope.inTransaction( session -> {
session.delete( session.find( Foo.class, foo.id ) );
});
} );
this.foo = null;
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Foo.class,
Bar.class,
};
}
@Entity(name = "Foo")
public static class Foo {

View File

@ -4,10 +4,21 @@
* 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.derivedidentities.bidirectional;
package org.hibernate.orm.test.annotations.derivedidentities.bidirectional;
import java.io.Serializable;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.testing.TestForIssue;
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.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
@ -16,27 +27,24 @@ import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class OneToOneEagerDerivedIdFetchModeSelectTest extends BaseCoreFunctionalTestCase {
@DomainModel(
annotatedClasses = {
OneToOneEagerDerivedIdFetchModeSelectTest.Foo.class,
OneToOneEagerDerivedIdFetchModeSelectTest.Bar.class,
}
)
@SessionFactory
public class OneToOneEagerDerivedIdFetchModeSelectTest {
private Foo foo;
@Test
@TestForIssue(jiraKey = "HHH-14390")
public void testQuery() {
doInHibernate( this::sessionFactory, session -> {
public void testQuery(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo.id = :id" )
.setParameter( "id", foo.getId() )
.uniqueResult();
@ -44,14 +52,13 @@ public class OneToOneEagerDerivedIdFetchModeSelectTest extends BaseCoreFunctiona
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
});
} );
}
@Test
@TestForIssue(jiraKey = "HHH-14390")
public void testQueryById() {
doInHibernate( this::sessionFactory, session -> {
public void testQueryById(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo = :foo" )
.setParameter( "foo", foo )
.uniqueResult();
@ -59,25 +66,24 @@ public class OneToOneEagerDerivedIdFetchModeSelectTest extends BaseCoreFunctiona
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
});
} );
}
@Test
@TestForIssue(jiraKey = "HHH-14390")
public void testFindByPrimaryKey() {
doInHibernate( this::sessionFactory, session -> {
public void testFindByPrimaryKey(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Bar newBar = session.find( Bar.class, foo.getId() );
assertNotNull( newBar );
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
});
} );
}
@Before
public void setupData() {
this.foo = doInHibernate( this::sessionFactory, session -> {
@BeforeEach
public void setupData(SessionFactoryScope scope) {
this.foo = scope.fromTransaction( session -> {
Foo foo = new Foo();
session.persist( foo );
@ -95,24 +101,16 @@ public class OneToOneEagerDerivedIdFetchModeSelectTest extends BaseCoreFunctiona
assertEquals( foo.getId(), bar.getFoo().getId() );
return foo;
});
} );
}
@After
public void cleanupData() {
@AfterEach
public void cleanupData(SessionFactoryScope scope) {
this.foo = null;
doInHibernate( this::sessionFactory, session -> {
scope.inTransaction( session -> {
session.createQuery( "delete from Bar" );
session.createQuery( "delete from Foo" );
});
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Foo.class,
Bar.class,
};
} );
}
@Entity(name = "Foo")

View File

@ -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.derivedidentities.bidirectional;
package org.hibernate.orm.test.annotations.derivedidentities.bidirectional;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

View File

@ -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.derivedidentities.bidirectional;
package org.hibernate.orm.test.annotations.derivedidentities.bidirectional;
import java.io.Serializable;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;

View File

@ -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.derivedidentities.bidirectional;
package org.hibernate.orm.test.annotations.derivedidentities.bidirectional;
public class OrderLinePK

View File

@ -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.derivedidentities.bidirectional;
package org.hibernate.orm.test.annotations.derivedidentities.bidirectional;
import java.io.Serializable;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;

View File

@ -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.derivedidentities.e2.b;
package org.hibernate.orm.test.annotations.derivedidentities.e2.b;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;

View File

@ -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.derivedidentities.e3.b;
package org.hibernate.orm.test.annotations.derivedidentities.e2.b;
import java.io.Serializable;
import jakarta.persistence.Embeddable;

View File

@ -0,0 +1,64 @@
/*
* 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.derivedidentities.e2.b;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.orm.test.util.SchemaUtil;
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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Emmanuel Bernard
*/
@DomainModel(
annotatedClasses = {
Employee.class,
Dependent.class
}
)
@SessionFactory
public class DerivedIdentityIdClassParentEmbeddedIdDepTest {
@Test
public void testManyToOne(SessionFactoryScope scope) {
final MetadataImplementor metadata = scope.getMetadataImplementor();
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "emp_firstName", metadata ) );
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "emp_lastName", metadata ) );
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "name", metadata ) );
assertTrue( !SchemaUtil.isColumnPresent( "Dependent", "firstName", metadata ) );
assertTrue( !SchemaUtil.isColumnPresent( "Dependent", "lastName", metadata ) );
Employee e = new Employee();
e.firstName = "Emmanuel";
e.lastName = "Bernard";
scope.inTransaction(
session -> {
session.persist( e );
Dependent d = new Dependent();
d.emp = e;
d.id = new DependentId();
d.id.name = "Doggy";
session.persist( d );
session.flush();
session.clear();
d = session.get( Dependent.class, d.id );
assertNotNull( d.emp );
assertEquals( e.firstName, d.emp.firstName );
}
);
}
}

View File

@ -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.derivedidentities.e2.b;
package org.hibernate.orm.test.annotations.derivedidentities.e2.b;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;

View File

@ -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.derivedidentities.e2.b;
package org.hibernate.orm.test.annotations.derivedidentities.e2.b;
import java.io.Serializable;
/**

View File

@ -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.derivedidentities.e3.a;
package org.hibernate.orm.test.annotations.derivedidentities.e3.a;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

View File

@ -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.derivedidentities.e3.a;
package org.hibernate.orm.test.annotations.derivedidentities.e3.a;
import java.io.Serializable;
/**

View File

@ -0,0 +1,71 @@
/*
* 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.derivedidentities.e3.a;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.orm.test.util.SchemaUtil;
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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Emmanuel Bernard
*/
@DomainModel(
annotatedClasses = {
Dependent.class,
Employee.class
}
)
@SessionFactory
public class DerivedIdentityEmbeddedIdParentIdClassTest {
@Test
public void testManyToOne(SessionFactoryScope scope) {
final MetadataImplementor metadata = scope.getMetadataImplementor();
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "FK1", metadata ) );
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "FK2", metadata ) );
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "dep_name", metadata ) );
assertTrue( !SchemaUtil.isColumnPresent( "Dependent", "firstName", metadata ) );
assertTrue( !SchemaUtil.isColumnPresent( "Dependent", "lastName", metadata ) );
Employee e = new Employee();
e.empId = new EmployeeId();
e.empId.firstName = "Emmanuel";
e.empId.lastName = "Bernard";
scope.inTransaction(
session -> {
session.persist( e );
Dependent d = new Dependent();
d.emp = e;
d.name = "Doggy";
DependentId dId = new DependentId();
dId.emp = new EmployeeId();
dId.emp.firstName = e.empId.firstName;
dId.emp.lastName = e.empId.lastName;
dId.name = d.name;
session.persist( d );
session.flush();
session.clear();
d = session.get( Dependent.class, dId );
assertNotNull( d.emp );
assertEquals( e.empId.firstName, d.emp.empId.firstName );
session.delete( d );
session.delete( d.emp );
}
);
}
}

View File

@ -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.derivedidentities.e3.a;
package org.hibernate.orm.test.annotations.derivedidentities.e3.a;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;

View File

@ -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.derivedidentities.e3.a;
package org.hibernate.orm.test.annotations.derivedidentities.e3.a;
import java.io.Serializable;
/**

View File

@ -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.derivedidentities.e3.b;
package org.hibernate.orm.test.annotations.derivedidentities.e3.b;
import jakarta.persistence.AttributeOverride;
import jakarta.persistence.Column;
import jakarta.persistence.EmbeddedId;

View File

@ -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.derivedidentities.e2.b;
package org.hibernate.orm.test.annotations.derivedidentities.e3.b;
import java.io.Serializable;
import jakarta.persistence.Embeddable;

View File

@ -0,0 +1,61 @@
/*
* 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.derivedidentities.e3.b;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.orm.test.util.SchemaUtil;
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.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* @author Emmanuel Bernard
*/
@DomainModel(
annotatedClasses = { Dependent.class, Employee.class }
)
@SessionFactory
public class DerivedIdentityEmbeddedIdParentEmbeddedIdDepTest {
@Test
public void testManyToOne(SessionFactoryScope scope) {
final MetadataImplementor metadata = scope.getMetadataImplementor();
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "FK1", metadata ) );
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "FK2", metadata ) );
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "dep_name", metadata ) );
assertTrue( !SchemaUtil.isColumnPresent( "Dependent", "firstName", metadata ) );
assertTrue( !SchemaUtil.isColumnPresent( "Dependent", "lastName", metadata ) );
Employee e = new Employee();
e.empId = new EmployeeId();
e.empId.firstName = "Emmanuel";
e.empId.lastName = "Bernard";
scope.inTransaction(
session -> {
session.persist( e );
Dependent d = new Dependent();
d.emp = e;
d.id = new DependentId();
d.id.name = "Doggy";
session.persist( d );
session.flush();
session.clear();
d = session.get( Dependent.class, d.id );
assertNotNull( d.emp );
assertEquals( e.empId.firstName, d.emp.empId.firstName );
}
);
}
}

View File

@ -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.derivedidentities.e3.b;
package org.hibernate.orm.test.annotations.derivedidentities.e3.b;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;

View File

@ -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.derivedidentities.e3.b;
package org.hibernate.orm.test.annotations.derivedidentities.e3.b;
import java.io.Serializable;
import jakarta.persistence.Embeddable;

View File

@ -0,0 +1,128 @@
/*
* 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.derivedidentities.e4.a;
import java.util.Date;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.orm.test.util.SchemaUtil;
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.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Emmanuel Bernard
*/
@DomainModel(
annotatedClasses = {
MedicalHistory.class,
Simple.class,
Person.class,
FinancialHistory.class
}
)
@SessionFactory
public class DerivedIdentitySimpleParentSimpleDepTest {
@Test
public void testOneToOneExplicitJoinColumn(SessionFactoryScope scope) {
final MetadataImplementor metadata = scope.getMetadataImplementor();
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK", metadata ) );
assertTrue( !SchemaUtil.isColumnPresent( "MedicalHistory", "id", metadata ) );
Person person = new Person( "aaa" );
scope.inTransaction(
session -> {
session.persist( person );
MedicalHistory medicalHistory = new MedicalHistory( person );
session.persist( medicalHistory );
}
);
scope.inTransaction(
session -> {
MedicalHistory medicalHistory = session.get( MedicalHistory.class, "aaa" );
assertEquals( person.ssn, medicalHistory.patient.ssn );
medicalHistory.lastupdate = new Date();
}
);
}
@Test
public void testManyToOneExplicitJoinColumn(SessionFactoryScope scope) {
final MetadataImplementor metadata = scope.getMetadataImplementor();
assertTrue( SchemaUtil.isColumnPresent( "FinancialHistory", "patient_ssn", metadata ) );
assertTrue( !SchemaUtil.isColumnPresent( "FinancialHistory", "id", metadata ) );
Person person = new Person( "aaa" );
scope.inTransaction(
session -> {
session.persist( person );
FinancialHistory financialHistory = new FinancialHistory( person );
session.persist( financialHistory );
}
);
scope.inTransaction(
session -> {
FinancialHistory financialHistory = session.get( FinancialHistory.class, "aaa" );
assertEquals( person.ssn, financialHistory.patient.ssn );
financialHistory.lastUpdate = new Date();
}
);
}
@Test
public void testSimplePkValueLoading(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Person e = new Person( "aaa" );
session.persist( e );
FinancialHistory d = new FinancialHistory( e );
session.persist( d );
}
);
scope.inTransaction(
session -> {
FinancialHistory history = session.get( FinancialHistory.class, "aaa" );
assertNotNull( history );
}
);
}
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
FinancialHistory history = session.get( FinancialHistory.class, "aaa" );
if ( history != null ) {
session.delete( history );
session.delete( history.patient );
}
}
);
scope.inTransaction(
session -> {
MedicalHistory history = session.get( MedicalHistory.class, "aaa" );
if ( history != null ) {
session.delete( history );
session.delete( history.patient );
}
}
);
}
}

View File

@ -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.derivedidentities.e4.a;
package org.hibernate.orm.test.annotations.derivedidentities.e4.a;
import java.io.Serializable;
import java.util.Date;
import jakarta.persistence.Entity;

View File

@ -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.derivedidentities.e4.a;
package org.hibernate.orm.test.annotations.derivedidentities.e4.a;
import java.io.Serializable;
import java.util.Date;
import jakarta.persistence.Entity;

View File

@ -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.derivedidentities.e4.a;
package org.hibernate.orm.test.annotations.derivedidentities.e4.a;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

View File

@ -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.derivedidentities.e4.a;
package org.hibernate.orm.test.annotations.derivedidentities.e4.a;
import java.io.Serializable;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

View File

@ -4,63 +4,84 @@
* 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.derivedidentities.e4.c;
package org.hibernate.orm.test.annotations.derivedidentities.e4.c;
import java.io.Serializable;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.testing.TestForIssue;
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 jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToOne;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.orm.test.util.SchemaUtil.getColumnNames;
/**
* Test that when an entity with derived identity is referenced from a third entity,
* associations on that third entity are bound in a second pass *after* the derived identity is bound.
*
* <p>
* This test used to fail on bootstrap with the following error:
*
* <p>
* org.hibernate.MappingException: Foreign key (FK2m2b1kaetxfvcsaih4raaocn8:ref_mto_derived [])) must have same number of columns as the referenced primary key (mto_derived [idsource_id])
*/
@TestForIssue(jiraKey = "HHH-14467")
public class DerivedIdentitySimpleParentSimpleDepSecondPassOrderingTest extends BaseNonConfigCoreFunctionalTestCase {
@DomainModel(
annotatedClasses = {
// These two must be mentioned first in order to trigger the bug.
// In a real application this would happen randomly, depending on how JARs are scanned.
DerivedIdentitySimpleParentSimpleDepSecondPassOrderingTest.EntityReferencingEntityWithOneToOneDerivedId.class,
DerivedIdentitySimpleParentSimpleDepSecondPassOrderingTest.EntityReferencingEntityWithManyToOneDerivedId.class,
DerivedIdentitySimpleParentSimpleDepSecondPassOrderingTest.EntityWithSimpleId.class,
DerivedIdentitySimpleParentSimpleDepSecondPassOrderingTest.EntityWithOneToOneDerivedId.class,
DerivedIdentitySimpleParentSimpleDepSecondPassOrderingTest.EntityWithManyToOneDerivedId.class
}
)
@SessionFactory
public class DerivedIdentitySimpleParentSimpleDepSecondPassOrderingTest {
@Test
public void testOneToOne() {
assertThat( getColumnNames( "oto_derived", metadata() ) )
public void testOneToOne(SessionFactoryScope scope) {
final MetadataImplementor metadata = scope.getMetadataImplementor();
assertThat( getColumnNames( "oto_derived", metadata ) )
.contains( "idsource_id" )
.doesNotContain( "id", "idSource", "idsource" );
inTransaction( s -> {
scope.inTransaction( s -> {
EntityWithSimpleId simple = new EntityWithSimpleId( 1 );
s.persist( simple );
EntityWithOneToOneDerivedId derived = new EntityWithOneToOneDerivedId( simple );
s.persist( derived );
} );
inTransaction( s -> {
scope.inTransaction( s -> {
EntityWithOneToOneDerivedId derived = s.get( EntityWithOneToOneDerivedId.class, 1 );
assertThat( derived.getIdsource().getId() ).isEqualTo( 1 );
derived.setData( "something" );
} );
inTransaction( s -> {
scope.inTransaction( s -> {
EntityWithOneToOneDerivedId derived = s.get( EntityWithOneToOneDerivedId.class, 1 );
assertThat( derived.getData() ).isNotNull();
} );
inTransaction( s -> {
scope.inTransaction( s -> {
EntityWithOneToOneDerivedId derived = s.get( EntityWithOneToOneDerivedId.class, 1 );
EntityReferencingEntityWithOneToOneDerivedId referencing =
new EntityReferencingEntityWithOneToOneDerivedId( 1, derived );
s.persist( referencing );
} );
inTransaction( s -> {
scope.inTransaction( s -> {
EntityReferencingEntityWithOneToOneDerivedId referencing =
s.get( EntityReferencingEntityWithOneToOneDerivedId.class, 1 );
assertThat( referencing.getRef().getIdsource().getId() ).isEqualTo( 1 );
@ -68,57 +89,44 @@ public class DerivedIdentitySimpleParentSimpleDepSecondPassOrderingTest extends
}
@Test
public void testManyToOne() {
assertThat( getColumnNames( "mto_derived", metadata() ) )
public void testManyToOne(SessionFactoryScope scope) {
final MetadataImplementor metadata = scope.getMetadataImplementor();
assertThat( getColumnNames( "mto_derived", metadata ) )
.contains( "idsource_id" )
.doesNotContain( "id", "idSource", "idsource" );
inTransaction( s -> {
scope.inTransaction( s -> {
EntityWithSimpleId simple = new EntityWithSimpleId( 2 );
s.persist( simple );
EntityWithManyToOneDerivedId derived = new EntityWithManyToOneDerivedId( simple );
s.persist( derived );
} );
inTransaction( s -> {
scope.inTransaction( s -> {
EntityWithManyToOneDerivedId derived = s.get( EntityWithManyToOneDerivedId.class, 2 );
assertThat( derived.getIdsource().getId() ).isEqualTo( 2 );
derived.setData( "something" );
} );
inTransaction( s -> {
scope.inTransaction( s -> {
EntityWithManyToOneDerivedId derived = s.get( EntityWithManyToOneDerivedId.class, 2 );
assertThat( derived.getData() ).isNotNull();
} );
inTransaction( s -> {
scope.inTransaction( s -> {
EntityWithManyToOneDerivedId derived = s.get( EntityWithManyToOneDerivedId.class, 2 );
EntityReferencingEntityWithManyToOneDerivedId referencing =
new EntityReferencingEntityWithManyToOneDerivedId( 2, derived );
s.persist( referencing );
} );
inTransaction( s -> {
scope.inTransaction( s -> {
EntityReferencingEntityWithManyToOneDerivedId referencing =
s.get( EntityReferencingEntityWithManyToOneDerivedId.class, 2 );
assertThat( referencing.getRef().getIdsource().getId() ).isEqualTo( 2 );
} );
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
// These two must be mentioned first in order to trigger the bug.
// In a real application this would happen randomly, depending on how JARs are scanned.
EntityReferencingEntityWithOneToOneDerivedId.class,
EntityReferencingEntityWithManyToOneDerivedId.class,
EntityWithSimpleId.class,
EntityWithOneToOneDerivedId.class,
EntityWithManyToOneDerivedId.class
};
}
@Entity(name = "simple")
public static class EntityWithSimpleId {
@Id

View File

@ -0,0 +1,96 @@
/*
* 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.derivedidentities.e5.a;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.orm.test.util.SchemaUtil;
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;
/**
* @author Emmanuel Bernard
*/
@DomainModel(
annotatedClasses = {
MedicalHistory.class,
Person.class
}
)
@SessionFactory
public class DerivedIdentityIdClassParentSameIdTypeIdClassDepTest {
private static final String FIRST_NAME = "Emmanuel";
private static final String LAST_NAME = "Bernard";
@Test
public void testOneToOneExplicitJoinColumn(SessionFactoryScope scope) {
final MetadataImplementor metadata = scope.getMetadataImplementor();
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", metadata ) );
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", metadata ) );
assertTrue( !SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", metadata ) );
scope.inTransaction(
session -> {
Person e = new Person( FIRST_NAME, LAST_NAME );
session.persist( e );
MedicalHistory d = new MedicalHistory( e );
session.persist( d );
session.flush();
session.refresh( d );
}
);
scope.inTransaction(
session -> {
PersonId pId = new PersonId( FIRST_NAME, LAST_NAME );
MedicalHistory d2 = session.get( MedicalHistory.class, pId );
Person p2 = session.get( Person.class, pId );
assertEquals( pId.firstName, d2.patient.firstName );
assertEquals( pId.firstName, p2.firstName );
session.delete( d2 );
session.delete( p2 );
}
);
}
@Test
public void testTckLikeBehavior(SessionFactoryScope scope) {
final MetadataImplementor metadata = scope.getMetadataImplementor();
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", metadata ) );
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", metadata ) );
assertTrue( !SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", metadata ) );
scope.inTransaction(
session -> {
Person e = new Person( FIRST_NAME, LAST_NAME );
session.persist( e );
MedicalHistory d = new MedicalHistory( e );
session.persist( d );
session.flush();
session.refresh( d );
session.getTransaction().commit();
// NOTE THAT WE LEAVE THE SESSION OPEN!
session.getTransaction().begin();
PersonId pId = new PersonId( FIRST_NAME, LAST_NAME );
MedicalHistory d2 = session.get( MedicalHistory.class, pId );
Person p2 = session.get( Person.class, pId );
assertEquals( pId.firstName, d2.patient.firstName );
assertEquals( pId.firstName, p2.firstName );
session.delete( d2 );
session.delete( p2 );
}
);
}
}

View File

@ -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.derivedidentities.e5.a;
package org.hibernate.orm.test.annotations.derivedidentities.e5.a;
import java.io.Serializable;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

View File

@ -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.derivedidentities.e5.a;
package org.hibernate.orm.test.annotations.derivedidentities.e5.a;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;

View File

@ -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.derivedidentities.e5.a;
package org.hibernate.orm.test.annotations.derivedidentities.e5.a;
import java.io.Serializable;
/**

View File

@ -0,0 +1,77 @@
/*
* 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.derivedidentities.e6.a;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.orm.test.util.SchemaUtil;
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.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Emmanuel Bernard
*/
@DomainModel(
annotatedClasses = {
MedicalHistory.class,
Person.class
}
)
@SessionFactory
public class DerivedIdentityEmbeddedIdParentSameIdTypeIdClassDepTest {
@Test
public void testOneToOneExplicitJoinColumn(SessionFactoryScope scope) {
final MetadataImplementor metadataImplementor = scope.getMetadataImplementor();
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", metadataImplementor ) );
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", metadataImplementor ) );
assertTrue( !SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", metadataImplementor ) );
scope.inTransaction(
session -> {
Person e = new Person();
e.id = new PersonId();
e.id.firstName = "Emmanuel";
e.id.lastName = "Bernard";
session.persist( e );
MedicalHistory d = new MedicalHistory();
d.patient = e;
session.persist( d );
session.flush();
session.clear();
PersonId pId = new PersonId();
pId.firstName = e.id.firstName;
pId.lastName = e.id.lastName;
d = session.get( MedicalHistory.class, pId );
assertEquals( pId.firstName, d.patient.id.firstName );
session.delete( d );
session.delete( d.patient );
}
);
}
@AfterEach
public void teardDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from MedicalHistory" ).executeUpdate();
session.createQuery( "delete from Person" ).executeUpdate();
}
);
}
}

View File

@ -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.derivedidentities.e6.a;
package org.hibernate.orm.test.annotations.derivedidentities.e6.a;
import java.io.Serializable;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@ -26,4 +26,8 @@ public class MedicalHistory implements Serializable {
})
@OneToOne
Person patient;
public void setPatient(Person patient) {
this.patient = patient;
}
}

View File

@ -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.derivedidentities.e6.a;
package org.hibernate.orm.test.annotations.derivedidentities.e6.a;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;

View File

@ -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.derivedidentities.e6.b;
package org.hibernate.orm.test.annotations.derivedidentities.e6.a;
import java.io.Serializable;
import jakarta.persistence.Embeddable;

View File

@ -0,0 +1,75 @@
/*
* 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.derivedidentities.e6.b;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.orm.test.util.SchemaUtil;
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.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Emmanuel Bernard
*/
@DomainModel(
annotatedClasses = {
MedicalHistory.class,
Person.class
}
)
@SessionFactory
public class DerivedIdentityEmbeddedIdParentSameIdTypeEmbeddedIdDepTest {
@Test
public void testOneToOneExplicitJoinColumn(SessionFactoryScope scope) {
final MetadataImplementor metadata = scope.getMetadataImplementor();
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", metadata ) );
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", metadata ) );
assertTrue( !SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", metadata ) );
scope.inTransaction(
session -> {
Person e = new Person();
e.id = new PersonId();
e.id.firstName = "Emmanuel";
e.id.lastName = "Bernard";
session.persist( e );
MedicalHistory d = new MedicalHistory();
// d.id = new PersonId();
// d.id.firstName = "Emmanuel"; //FIXME not needed when foreign is enabled
// d.id.lastName = "Bernard"; //FIXME not needed when foreign is enabled
d.patient = e;
session.persist( d );
session.flush();
session.clear();
d = session.get( MedicalHistory.class, d.id );
assertEquals( d.id.firstName, d.patient.id.firstName );
}
);
}
@AfterEach
public void teardDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from MedicalHistory" ).executeUpdate();
session.createQuery( "delete from Person" ).executeUpdate();
}
);
}
}

View File

@ -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.derivedidentities.e6.b;
package org.hibernate.orm.test.annotations.derivedidentities.e6.b;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;

View File

@ -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.derivedidentities.e6.b;
package org.hibernate.orm.test.annotations.derivedidentities.e6.b;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;

View File

@ -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.derivedidentities.e6.a;
package org.hibernate.orm.test.annotations.derivedidentities.e6.b;
import java.io.Serializable;
import jakarta.persistence.Embeddable;

View File

@ -4,9 +4,21 @@
* 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.derivedidentities.unidirectional;
package org.hibernate.orm.test.annotations.derivedidentities.unidirectional;
import java.io.Serializable;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.testing.TestForIssue;
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.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
@ -14,26 +26,23 @@ import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class OneToOneEagerDerivedIdFetchModeSelectTest extends BaseCoreFunctionalTestCase {
@DomainModel(
annotatedClasses = {
OneToOneEagerDerivedIdFetchModeSelectTest.Foo.class,
OneToOneEagerDerivedIdFetchModeSelectTest.Bar.class,
}
)
@SessionFactory
public class OneToOneEagerDerivedIdFetchModeSelectTest {
private Foo foo;
@Test
@TestForIssue( jiraKey = "HHH-14390")
public void testQuery() {
doInHibernate( this::sessionFactory, session -> {
@Test
@TestForIssue(jiraKey = "HHH-14390")
public void testQuery(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo.id = :id" )
.setParameter( "id", foo.getId() )
.uniqueResult();
@ -41,14 +50,13 @@ public class OneToOneEagerDerivedIdFetchModeSelectTest extends BaseCoreFunctiona
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
});
} );
}
@Test
@TestForIssue(jiraKey = "HHH-14390")
public void testQueryById() {
doInHibernate( this::sessionFactory, session -> {
public void testQueryById(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo = :foo" )
.setParameter( "foo", foo )
.uniqueResult();
@ -56,25 +64,24 @@ public class OneToOneEagerDerivedIdFetchModeSelectTest extends BaseCoreFunctiona
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
});
} );
}
@Test
@TestForIssue(jiraKey = "HHH-14390")
public void testFindByPrimaryKey() {
doInHibernate( this::sessionFactory, session -> {
public void testFindByPrimaryKey(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Bar newBar = session.find( Bar.class, foo.getId() );
assertNotNull( newBar );
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
});
} );
}
@Before
public void setupData() {
this.foo = doInHibernate( this::sessionFactory, session -> {
@BeforeEach
public void setupData(SessionFactoryScope scope) {
this.foo = scope.fromTransaction( session -> {
Foo foo = new Foo();
session.persist( foo );
@ -89,24 +96,16 @@ public class OneToOneEagerDerivedIdFetchModeSelectTest extends BaseCoreFunctiona
assertEquals( foo.getId(), bar.getFoo().getId() );
return foo;
});
} );
}
@After
public void cleanupData() {
@AfterEach
public void cleanupData(SessionFactoryScope scope) {
this.foo = null;
doInHibernate( this::sessionFactory, session -> {
scope.inTransaction( session -> {
session.createQuery( "delete from Bar" );
session.createQuery( "delete from Foo" );
});
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Foo.class,
Bar.class,
};
} );
}
@Entity(name = "Foo")

View File

@ -4,9 +4,21 @@
* 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.derivedidentities.unidirectional;
package org.hibernate.orm.test.annotations.derivedidentities.unidirectional;
import java.io.Serializable;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.testing.TestForIssue;
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.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
@ -14,27 +26,23 @@ import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class OneToOneLazyDerivedIdFetchModeSelectTest extends BaseCoreFunctionalTestCase {
@DomainModel(
annotatedClasses = {
OneToOneLazyDerivedIdFetchModeSelectTest.Foo.class,
OneToOneLazyDerivedIdFetchModeSelectTest.Bar.class,
}
)
@SessionFactory
public class OneToOneLazyDerivedIdFetchModeSelectTest {
private Foo foo;
@Test
@TestForIssue( jiraKey = "HHH-14390")
public void testQuery() {
doInHibernate( this::sessionFactory, session -> {
@TestForIssue(jiraKey = "HHH-14390")
public void testQuery(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo.id = :id" )
.setParameter( "id", foo.getId() )
.uniqueResult();
@ -42,14 +50,13 @@ public class OneToOneLazyDerivedIdFetchModeSelectTest extends BaseCoreFunctional
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
});
} );
}
@Test
@TestForIssue(jiraKey = "HHH-14390")
public void testQueryById() {
doInHibernate( this::sessionFactory, session -> {
public void testQueryById(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo = :foo" )
.setParameter( "foo", foo )
.uniqueResult();
@ -57,25 +64,24 @@ public class OneToOneLazyDerivedIdFetchModeSelectTest extends BaseCoreFunctional
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
});
} );
}
@Test
@TestForIssue(jiraKey = "HHH-14390")
public void testFindByPrimaryKey() {
doInHibernate( this::sessionFactory, session -> {
public void testFindByPrimaryKey(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Bar newBar = session.find( Bar.class, foo.getId() );
assertNotNull( newBar );
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
});
} );
}
@Before
public void setupData() {
this.foo = doInHibernate( this::sessionFactory, session -> {
@BeforeEach
public void setupData(SessionFactoryScope scope) {
this.foo = scope.fromTransaction( session -> {
Foo foo = new Foo();
session.persist( foo );
@ -90,24 +96,16 @@ public class OneToOneLazyDerivedIdFetchModeSelectTest extends BaseCoreFunctional
assertEquals( foo.getId(), bar.getFoo().getId() );
return foo;
});
} );
}
@After
public void cleanupData() {
@AfterEach
public void cleanupData(SessionFactoryScope scope) {
this.foo = null;
doInHibernate( this::sessionFactory, session -> {
scope.inTransaction( session -> {
session.createQuery( "delete from Bar" );
session.createQuery( "delete from Foo" );
});
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Foo.class,
Bar.class,
};
} );
}
@Entity(name = "Foo")

View File

@ -1,100 +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.derivedidentities.bidirectional;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.SessionImpl;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
public class CompositeDerivedIdentityTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Product.class,
OrderLine.class,
Order.class
};
}
@Test
public void testCreateProject() {
Product product = new Product();
product.setName("Product 1");
Session session = openSession();
session.beginTransaction();
session.save(product);
session.getTransaction().commit();
session.close();
Order order = new Order();
order.setName("Order 1");
order.addLineItem(product, 2);
session = openSession();
session.beginTransaction();
session.save(order);
session.getTransaction().commit();
session.close();
Long orderId = order.getId();
session = openSession();
session.beginTransaction();
order = (Order) session.get(Order.class, orderId);
assertEquals(1, order.getLineItems().size());
session.delete( order );
session.getTransaction().commit();
session.close();
}
@Test
@TestForIssue( jiraKey = "HHH-10476")
public void testBidirectonalKeyManyToOneId() {
Product product = new Product();
product.setName( "Product 1" );
Session session = openSession();
session.beginTransaction();
session.save( product );
session.getTransaction().commit();
session.close();
Order order = new Order();
order.setName( "Order 1" );
order.addLineItem( product, 2 );
session = openSession();
session.beginTransaction();
session.save( order );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
OrderLine orderLine = order.getLineItems().iterator().next();
orderLine.setAmount( 5 );
OrderLine orderLineGotten = session.get( OrderLine.class, orderLine );
assertSame( orderLineGotten, orderLine );
assertEquals( Integer.valueOf( 2 ), orderLineGotten.getAmount() );
SessionImplementor si = (SessionImplementor) session;
assertTrue( si.getPersistenceContext().isEntryFor( orderLineGotten ) );
assertFalse( si.getPersistenceContext().isEntryFor( orderLineGotten.getOrder() ) );
assertFalse( si.getPersistenceContext().isEntryFor( orderLineGotten.getProduct() ) );
session.getTransaction().commit();
session.close();
}
}

View File

@ -1,58 +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.derivedidentities.e2.b;
import org.hibernate.Session;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.orm.test.util.SchemaUtil;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* @author Emmanuel Bernard
*/
public class DerivedIdentityIdClassParentEmbeddedIdDepTest extends BaseNonConfigCoreFunctionalTestCase {
@Test
public void testManyToOne() throws Exception {
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "emp_firstName", metadata() ) );
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "emp_lastName", metadata() ) );
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "name", metadata() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "Dependent", "firstName", metadata() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "Dependent", "lastName", metadata() ) );
Employee e = new Employee();
e.firstName = "Emmanuel";
e.lastName = "Bernard";
Session s = openSession( );
s.getTransaction().begin();
s.persist( e );
Dependent d = new Dependent();
d.emp = e;
d.id = new DependentId();
d.id.name = "Doggy";
s.persist( d );
s.flush();
s.clear();
d = (Dependent) s.get( Dependent.class, d.id );
assertNotNull( d.emp );
assertEquals( e.firstName, d.emp.firstName );
s.getTransaction().rollback();
s.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Employee.class,
Dependent.class
};
}
}

View File

@ -1,66 +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.derivedidentities.e3.a;
import org.hibernate.Session;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.orm.test.util.SchemaUtil;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* @author Emmanuel Bernard
*/
public class DerivedIdentityEmbeddedIdParentIdClassTest extends BaseNonConfigCoreFunctionalTestCase {
@Test
public void testManyToOne() throws Exception {
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "FK1", metadata() ) );
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "FK2", metadata() ) );
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "dep_name", metadata() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "Dependent", "firstName", metadata() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "Dependent", "lastName", metadata() ) );
Employee e = new Employee();
e.empId = new EmployeeId();
e.empId.firstName = "Emmanuel";
e.empId.lastName = "Bernard";
Session s = openSession( );
s.getTransaction().begin();
s.persist( e );
Dependent d = new Dependent();
d.emp = e;
d.name = "Doggy";
DependentId dId = new DependentId();
dId.emp = new EmployeeId();
dId.emp.firstName = e.empId.firstName;
dId.emp.lastName = e.empId.lastName;
dId.name = d.name;
s.persist( d );
s.flush();
s.clear();
d = (Dependent) s.get( Dependent.class, dId );
assertNotNull( d.emp );
assertEquals( e.empId.firstName, d.emp.empId.firstName );
s.delete( d );
s.delete( d.emp );
s.getTransaction().commit();
s.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Dependent.class,
Employee.class
};
}
}

View File

@ -1,55 +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.derivedidentities.e3.b;
import org.hibernate.Session;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.orm.test.util.SchemaUtil;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* @author Emmanuel Bernard
*/
public class DerivedIdentityEmbeddedIdParentEmbeddedIdDepTest extends BaseNonConfigCoreFunctionalTestCase {
@Test
public void testManyToOne() throws Exception {
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "FK1", metadata() ) );
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "FK2", metadata() ) );
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "dep_name", metadata() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "Dependent", "firstName", metadata() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "Dependent", "lastName", metadata() ) );
Employee e = new Employee();
e.empId = new EmployeeId();
e.empId.firstName = "Emmanuel";
e.empId.lastName = "Bernard";
Session s = openSession( );
s.getTransaction().begin();
s.persist( e );
Dependent d = new Dependent();
d.emp = e;
d.id = new DependentId();
d.id.name = "Doggy";
s.persist( d );
s.flush();
s.clear();
d = (Dependent) s.get( Dependent.class, d.id );
assertNotNull( d.emp );
assertEquals( e.empId.firstName, d.emp.empId.firstName );
s.getTransaction().rollback();
s.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { Dependent.class, Employee.class };
}
}

View File

@ -1,119 +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.derivedidentities.e4.a;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.orm.test.util.SchemaUtil;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* @author Emmanuel Bernard
*/
public class DerivedIdentitySimpleParentSimpleDepTest extends BaseNonConfigCoreFunctionalTestCase {
@Test
public void testOneToOneExplicitJoinColumn() throws Exception {
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK", metadata() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "MedicalHistory", "id", metadata() ) );
Session s = openSession();
s.getTransaction().begin();
Person person = new Person( "aaa" );
s.persist( person );
MedicalHistory medicalHistory = new MedicalHistory( person );
s.persist( medicalHistory );
s.getTransaction().commit();
s.close();
s = openSession();
s.getTransaction().begin();
medicalHistory = (MedicalHistory) s.get( MedicalHistory.class, "aaa" );
assertEquals( person.ssn, medicalHistory.patient.ssn );
medicalHistory.lastupdate = new Date();
s.getTransaction().commit();
s.close();
s = openSession();
s.getTransaction().begin();
medicalHistory = (MedicalHistory) s.get( MedicalHistory.class, "aaa" );
assertNotNull( medicalHistory.lastupdate );
s.delete( medicalHistory );
s.delete( medicalHistory.patient );
s.getTransaction().commit();
s.close();
}
@Test
public void testManyToOneExplicitJoinColumn() throws Exception {
assertTrue( SchemaUtil.isColumnPresent( "FinancialHistory", "patient_ssn", metadata() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "FinancialHistory", "id", metadata() ) );
Session s = openSession();
s.getTransaction().begin();
Person person = new Person( "aaa" );
s.persist( person );
FinancialHistory financialHistory = new FinancialHistory( person );
s.persist( financialHistory );
s.getTransaction().commit();
s.close();
s = openSession();
s.getTransaction().begin();
financialHistory = (FinancialHistory) s.get( FinancialHistory.class, "aaa" );
assertEquals( person.ssn, financialHistory.patient.ssn );
financialHistory.lastUpdate = new Date();
s.getTransaction().commit();
s.close();
s = openSession();
s.getTransaction().begin();
financialHistory = (FinancialHistory) s.get( FinancialHistory.class, "aaa" );
assertNotNull( financialHistory.lastUpdate );
s.delete( financialHistory );
s.delete( financialHistory.patient );
s.getTransaction().commit();
s.close();
}
@Test
public void testSimplePkValueLoading() {
Session s = openSession();
s.getTransaction().begin();
Person e = new Person( "aaa" );
s.persist( e );
FinancialHistory d = new FinancialHistory( e );
s.persist( d );
s.getTransaction().commit();
s.close();
s = openSession();
s.getTransaction().begin();
FinancialHistory history = (FinancialHistory) s.get( FinancialHistory.class, "aaa" );
assertNotNull( history );
s.delete( history );
s.delete( history.patient );
s.getTransaction().commit();
s.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
MedicalHistory.class,
Simple.class,
Person.class,
FinancialHistory.class
};
}
}

View File

@ -1,92 +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.derivedidentities.e5.a;
import org.hibernate.Session;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.orm.test.util.SchemaUtil;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Emmanuel Bernard
*/
public class DerivedIdentityIdClassParentSameIdTypeIdClassDepTest extends BaseNonConfigCoreFunctionalTestCase {
private static final String FIRST_NAME = "Emmanuel";
private static final String LAST_NAME = "Bernard";
@Test
public void testOneToOneExplicitJoinColumn() throws Exception {
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", metadata() ) );
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", metadata() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", metadata() ) );
Session s = openSession();
s.getTransaction().begin();
Person e = new Person( FIRST_NAME, LAST_NAME );
s.persist( e );
MedicalHistory d = new MedicalHistory( e );
s.persist( d );
s.flush();
s.refresh( d );
s.getTransaction().commit();
s.close();
s = openSession();
s.getTransaction().begin();
PersonId pId = new PersonId( FIRST_NAME, LAST_NAME );
MedicalHistory d2 = (MedicalHistory) s.get( MedicalHistory.class, pId );
Person p2 = (Person) s.get( Person.class, pId );
assertEquals( pId.firstName, d2.patient.firstName );
assertEquals( pId.firstName, p2.firstName );
s.delete( d2 );
s.delete( p2 );
s.getTransaction().commit();
s.close();
}
@Test
public void testTckLikeBehavior() throws Exception {
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", metadata() ) );
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", metadata() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", metadata() ) );
Session s = openSession();
s.getTransaction().begin();
Person e = new Person( FIRST_NAME, LAST_NAME );
s.persist( e );
MedicalHistory d = new MedicalHistory( e );
s.persist( d );
s.flush();
s.refresh( d );
s.getTransaction().commit();
// NOTE THAT WE LEAVE THE SESSION OPEN!
s.getTransaction().begin();
PersonId pId = new PersonId( FIRST_NAME, LAST_NAME );
MedicalHistory d2 = (MedicalHistory) s.get( MedicalHistory.class, pId );
Person p2 = (Person) s.get( Person.class, pId );
assertEquals( pId.firstName, d2.patient.firstName );
assertEquals( pId.firstName, p2.firstName );
s.delete( d2 );
s.delete( p2 );
s.getTransaction().commit();
s.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
MedicalHistory.class,
Person.class
};
}
}

View File

@ -1,57 +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.derivedidentities.e6.a;
import org.hibernate.Session;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.orm.test.util.SchemaUtil;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Emmanuel Bernard
*/
public class DerivedIdentityEmbeddedIdParentSameIdTypeIdClassDepTest extends BaseNonConfigCoreFunctionalTestCase {
@Test
public void testOneToOneExplicitJoinColumn() throws Exception {
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", metadata() ) );
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", metadata() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", metadata() ) );
Person e = new Person();
e.id = new PersonId();
e.id.firstName = "Emmanuel";
e.id.lastName = "Bernard";
Session s = openSession( );
s.getTransaction().begin();
s.persist( e );
MedicalHistory d = new MedicalHistory();
d.patient = e;
s.persist( d );
s.flush();
s.clear();
PersonId pId = new PersonId();
pId.firstName = e.id.firstName;
pId.lastName = e.id.lastName;
d = (MedicalHistory) s.get( MedicalHistory.class, pId );
assertEquals( pId.firstName, d.patient.id.firstName );
s.delete( d );
s.delete( d.patient );
s.getTransaction().commit();
s.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
MedicalHistory.class,
Person.class
};
}
}

View File

@ -1,55 +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.derivedidentities.e6.b;
import org.hibernate.Session;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.orm.test.util.SchemaUtil;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Emmanuel Bernard
*/
public class DerivedIdentityEmbeddedIdParentSameIdTypeEmbeddedIdDepTest extends BaseNonConfigCoreFunctionalTestCase {
@Test
public void testOneToOneExplicitJoinColumn() throws Exception {
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", metadata() ) );
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", metadata() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", metadata() ) );
Person e = new Person();
e.id = new PersonId();
e.id.firstName = "Emmanuel";
e.id.lastName = "Bernard";
Session s = openSession( );
s.getTransaction().begin();
s.persist( e );
MedicalHistory d = new MedicalHistory();
// d.id = new PersonId();
// d.id.firstName = "Emmanuel"; //FIXME not needed when foreign is enabled
// d.id.lastName = "Bernard"; //FIXME not needed when foreign is enabled
d.patient = e;
s.persist( d );
s.flush();
s.clear();
d = (MedicalHistory) s.get( MedicalHistory.class, d.id );
assertEquals( d.id.firstName, d.patient.id.firstName );
s.getTransaction().rollback();
s.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
MedicalHistory.class,
Person.class
};
}
}