HHH-13725 - Fix determining FK key name for OneToOne with PrimaryKeyJoinColumn
This commit is contained in:
parent
4c614e0315
commit
4ca9617b6f
|
@ -934,24 +934,24 @@ public class MappingModelCreationHelper {
|
||||||
|
|
||||||
final Iterator<Selectable> columnIterator = bootValueMapping.getColumnIterator();
|
final Iterator<Selectable> columnIterator = bootValueMapping.getColumnIterator();
|
||||||
String keyColumnExpression;
|
String keyColumnExpression;
|
||||||
final Identifier identifier = creationProcess.getCreationContext()
|
|
||||||
|
final Identifier tableIdentifier = creationProcess.getCreationContext()
|
||||||
.getBootstrapContext()
|
.getBootstrapContext()
|
||||||
.getMetadataBuildingOptions()
|
.getMetadataBuildingOptions()
|
||||||
.getPhysicalNamingStrategy().toPhysicalTableName(
|
.getPhysicalNamingStrategy().toPhysicalTableName(
|
||||||
bootValueMapping.getTable().getNameIdentifier(),
|
bootValueMapping.getTable().getNameIdentifier(),
|
||||||
jdbcServices.getJdbcEnvironment()
|
jdbcServices.getJdbcEnvironment()
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( columnIterator.hasNext() ) {
|
if ( columnIterator.hasNext() ) {
|
||||||
keyColumnExpression = columnIterator.next().getText( dialect );
|
keyColumnExpression = columnIterator.next().getText( dialect );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// case of ToOne with @PrimaryKeyJoinColumn
|
// case of ToOne with @PrimaryKeyJoinColumn
|
||||||
keyColumnExpression = identifier.getText();
|
keyColumnExpression = bootValueMapping.getTable().getColumn( 0 ).getName();
|
||||||
}
|
}
|
||||||
return new SimpleForeignKeyDescriptor(
|
return new SimpleForeignKeyDescriptor(
|
||||||
((AssociationType)bootValueMapping.getType()).getForeignKeyDirection(),
|
((AssociationType)bootValueMapping.getType()).getForeignKeyDirection(),
|
||||||
identifier.getText(),
|
tableIdentifier.getText(),
|
||||||
keyColumnExpression,
|
keyColumnExpression,
|
||||||
simpleFkTarget.getContainingTableExpression(),
|
simpleFkTarget.getContainingTableExpression(),
|
||||||
simpleFkTarget.getMappedColumnExpression(),
|
simpleFkTarget.getMappedColumnExpression(),
|
||||||
|
|
|
@ -8,17 +8,24 @@ package org.hibernate.orm.test.sql.exec.onetoone;
|
||||||
|
|
||||||
import java.util.Calendar;
|
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.EntityWithOneToOneSharingPrimaryKey;
|
||||||
import org.hibernate.testing.orm.domain.gambit.SimpleEntity;
|
import org.hibernate.testing.orm.domain.gambit.SimpleEntity;
|
||||||
import org.hibernate.testing.orm.junit.DomainModel;
|
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.ServiceRegistry;
|
||||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
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 org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
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.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Andrea Boriero
|
* @author Andrea Boriero
|
||||||
|
@ -31,12 +38,10 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
)
|
)
|
||||||
@ServiceRegistry
|
@ServiceRegistry
|
||||||
@SessionFactory(generateStatistics = true)
|
@SessionFactory(generateStatistics = true)
|
||||||
@FailureExpected
|
|
||||||
public class EntityWithOneToOneSharingPrimaryKeyTest {
|
public class EntityWithOneToOneSharingPrimaryKeyTest {
|
||||||
|
|
||||||
@Test
|
@BeforeEach
|
||||||
public void testOperations(SessionFactoryScope scope) {
|
public void setUp(SessionFactoryScope scope) {
|
||||||
|
|
||||||
SimpleEntity other = new SimpleEntity(
|
SimpleEntity other = new SimpleEntity(
|
||||||
2,
|
2,
|
||||||
Calendar.getInstance().getTime(),
|
Calendar.getInstance().getTime(),
|
||||||
|
@ -55,22 +60,42 @@ public class EntityWithOneToOneSharingPrimaryKeyTest {
|
||||||
entity.setOther( other );
|
entity.setOther( other );
|
||||||
|
|
||||||
scope.inTransaction(
|
scope.inTransaction(
|
||||||
|
|
||||||
session -> {
|
session -> {
|
||||||
session.save( other );
|
session.save( other );
|
||||||
session.save( entity );
|
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(
|
scope.inTransaction(
|
||||||
session -> {
|
session -> {
|
||||||
final EntityWithOneToOneSharingPrimaryKey loaded = session.get(
|
final EntityWithOneToOneSharingPrimaryKey loaded = session.get(
|
||||||
EntityWithOneToOneSharingPrimaryKey.class,
|
EntityWithOneToOneSharingPrimaryKey.class,
|
||||||
2
|
2
|
||||||
);
|
);
|
||||||
assert loaded != null;
|
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
|
||||||
|
|
||||||
|
assertThat( loaded, notNullValue() );
|
||||||
assertThat( loaded.getName(), equalTo( "first" ) );
|
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,
|
SimpleEntity.class,
|
||||||
2
|
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(
|
scope.inTransaction(
|
||||||
session -> {
|
session -> {
|
||||||
final String value = session.createQuery(
|
final String value = session.createQuery(
|
||||||
|
@ -91,6 +176,7 @@ public class EntityWithOneToOneSharingPrimaryKeyTest {
|
||||||
String.class
|
String.class
|
||||||
).uniqueResult();
|
).uniqueResult();
|
||||||
assertThat( value, equalTo( "first" ) );
|
assertThat( value, equalTo( "first" ) );
|
||||||
|
assertThat( statistics.getPrepareStatementCount(), is( 1L ) );
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue