HHH-13725 - Fix determining FK key name for OneToOne with PrimaryKeyJoinColumn

This commit is contained in:
Andrea Boriero 2019-11-18 09:14:11 +00:00 committed by Steve Ebersole
parent 4c614e0315
commit 4ca9617b6f
2 changed files with 101 additions and 15 deletions

View File

@ -934,24 +934,24 @@ public class MappingModelCreationHelper {
final Iterator<Selectable> columnIterator = bootValueMapping.getColumnIterator();
String keyColumnExpression;
final Identifier identifier = creationProcess.getCreationContext()
final Identifier tableIdentifier = creationProcess.getCreationContext()
.getBootstrapContext()
.getMetadataBuildingOptions()
.getPhysicalNamingStrategy().toPhysicalTableName(
bootValueMapping.getTable().getNameIdentifier(),
jdbcServices.getJdbcEnvironment()
);
if ( columnIterator.hasNext() ) {
keyColumnExpression = columnIterator.next().getText( dialect );
}
else {
// case of ToOne with @PrimaryKeyJoinColumn
keyColumnExpression = identifier.getText();
keyColumnExpression = bootValueMapping.getTable().getColumn( 0 ).getName();
}
return new SimpleForeignKeyDescriptor(
((AssociationType)bootValueMapping.getType()).getForeignKeyDirection(),
identifier.getText(),
tableIdentifier.getText(),
keyColumnExpression,
simpleFkTarget.getContainingTableExpression(),
simpleFkTarget.getMappedColumnExpression(),

View File

@ -8,17 +8,24 @@ package org.hibernate.orm.test.sql.exec.onetoone;
import java.util.Calendar;
import org.hibernate.Hibernate;
import org.hibernate.stat.spi.StatisticsImplementor;
import org.hibernate.testing.orm.domain.gambit.EntityWithOneToOneSharingPrimaryKey;
import org.hibernate.testing.orm.domain.gambit.SimpleEntity;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.FailureExpected;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
/**
* @author Andrea Boriero
@ -31,12 +38,10 @@ import static org.hamcrest.MatcherAssert.assertThat;
)
@ServiceRegistry
@SessionFactory(generateStatistics = true)
@FailureExpected
public class EntityWithOneToOneSharingPrimaryKeyTest {
@Test
public void testOperations(SessionFactoryScope scope) {
@BeforeEach
public void setUp(SessionFactoryScope scope) {
SimpleEntity other = new SimpleEntity(
2,
Calendar.getInstance().getTime(),
@ -55,22 +60,42 @@ public class EntityWithOneToOneSharingPrimaryKeyTest {
entity.setOther( other );
scope.inTransaction(
session -> {
session.save( other );
session.save( entity );
} );
}
);
}
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from EntityWithOneToOneSharingPrimaryKey" ).executeUpdate();
session.createQuery( "delete from SimpleEntity" ).executeUpdate();
}
);
}
@Test
public void testGet(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final EntityWithOneToOneSharingPrimaryKey loaded = session.get(
EntityWithOneToOneSharingPrimaryKey.class,
2
);
assert loaded != null;
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
assertThat( loaded, notNullValue() );
assertThat( loaded.getName(), equalTo( "first" ) );
assert loaded.getOther() != null;
assertThat( loaded.getOther().getId(), equalTo( 2 ) );
SimpleEntity other = loaded.getOther();
assertTrue( Hibernate.isInitialized( other ) );
assertThat( other, notNullValue() );
assertThat( other.getId(), equalTo( 2 ) );
}
);
@ -80,10 +105,70 @@ public class EntityWithOneToOneSharingPrimaryKeyTest {
SimpleEntity.class,
2
);
assert loaded != null;
assertThat( loaded, notNullValue() );
}
);
}
@Test
public void testHqlSelectWithImplicitJoin(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final EntityWithOneToOneSharingPrimaryKey value = session.createQuery(
"select e from EntityWithOneToOneSharingPrimaryKey e where e.other.id = 2",
EntityWithOneToOneSharingPrimaryKey.class
).uniqueResult();
assertThat( value.getName(), equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
assertTrue( Hibernate.isInitialized( value.getOther() ) );
}
);
}
@Test
public void testHqlSelectWithJoin(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final EntityWithOneToOneSharingPrimaryKey value = session.createQuery(
"select e from EntityWithOneToOneSharingPrimaryKey e join e.other t where t.id = 2",
EntityWithOneToOneSharingPrimaryKey.class
).uniqueResult();
assertThat( value.getName(), equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 2L ) );
assertTrue( Hibernate.isInitialized( value.getOther() ) );
}
);
}
@Test
public void testHqlSelectFetchWithJoin(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final EntityWithOneToOneSharingPrimaryKey value = session.createQuery(
"select e from EntityWithOneToOneSharingPrimaryKey e join fetch e.other t where t.id = 2",
EntityWithOneToOneSharingPrimaryKey.class
).uniqueResult();
assertThat( value.getName(), equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
assertTrue( Hibernate.isInitialized( value.getOther() ) );
}
);
}
@Test
public void testHqlSelectAField(SessionFactoryScope scope) {
StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
statistics.clear();
scope.inTransaction(
session -> {
final String value = session.createQuery(
@ -91,6 +176,7 @@ public class EntityWithOneToOneSharingPrimaryKeyTest {
String.class
).uniqueResult();
assertThat( value, equalTo( "first" ) );
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
}
);
}