Fix load toOne association not referencing a PK with FetchMode.SELECT

This commit is contained in:
Andrea Boriero 2021-08-09 15:15:11 +02:00 committed by Andrea Boriero
parent 5160c8ac6a
commit 9710bfcf39

View File

@ -1,4 +1,4 @@
package org.hibernate.test.notfound;
package org.hibernate.orm.test.notfound;
import java.io.Serializable;
import javax.persistence.CascadeType;
@ -19,55 +19,55 @@
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.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.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
/**
* @author Gail Badner
*/
@TestForIssue(jiraKey = "HHH-12436")
public class OptionalEagerRefNonPKNotFoundTest extends BaseCoreFunctionalTestCase {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
PersonManyToOneJoinIgnore.class,
PersonManyToOneSelectIgnore.class,
PersonOneToOneJoinIgnore.class,
PersonOneToOneSelectIgnore.class,
PersonMapsIdJoinIgnore.class,
PersonMapsIdSelectIgnore.class,
PersonPkjcJoinException.class,
PersonPkjcJoinIgnore.class,
PersonPkjcSelectException.class,
PersonPkjcSelectIgnore.class,
PersonMapsIdColumnJoinIgnore.class,
PersonMapsIdColumnSelectIgnore.class,
City.class
};
@DomainModel(
annotatedClasses = {
OptionalEagerRefNonPKNotFoundTest.PersonManyToOneJoinIgnore.class,
OptionalEagerRefNonPKNotFoundTest.PersonManyToOneSelectIgnore.class,
OptionalEagerRefNonPKNotFoundTest.PersonOneToOneJoinIgnore.class,
OptionalEagerRefNonPKNotFoundTest.PersonOneToOneSelectIgnore.class,
OptionalEagerRefNonPKNotFoundTest.PersonMapsIdJoinIgnore.class,
OptionalEagerRefNonPKNotFoundTest.PersonMapsIdSelectIgnore.class,
OptionalEagerRefNonPKNotFoundTest.PersonPkjcJoinException.class,
OptionalEagerRefNonPKNotFoundTest.PersonPkjcJoinIgnore.class,
OptionalEagerRefNonPKNotFoundTest.PersonPkjcSelectException.class,
OptionalEagerRefNonPKNotFoundTest.PersonPkjcSelectIgnore.class,
OptionalEagerRefNonPKNotFoundTest.PersonMapsIdColumnJoinIgnore.class,
OptionalEagerRefNonPKNotFoundTest.PersonMapsIdColumnSelectIgnore.class,
OptionalEagerRefNonPKNotFoundTest.City.class
}
@Override
protected void configure(Configuration configuration) {
super.configure( configuration );
configuration.setProperty( AvailableSettings.SHOW_SQL, Boolean.TRUE.toString() );
configuration.setProperty( AvailableSettings.FORMAT_SQL, Boolean.TRUE.toString() );
)
@SessionFactory
@ServiceRegistry(
settings = {
@Setting(name = AvailableSettings.SHOW_SQL, value = "true"),
@Setting(name = AvailableSettings.FORMAT_SQL, value = "true")
}
)
public class OptionalEagerRefNonPKNotFoundTest {
@Test
public void testOneToOneJoinIgnore() {
setupTest( PersonOneToOneJoinIgnore.class, 1L, false );
doInHibernate(
this::sessionFactory, session -> {
public void testOneToOneJoinIgnore(SessionFactoryScope scope) {
setupTest( PersonOneToOneJoinIgnore.class, 1L, false, scope );
scope.inTransaction(
session -> {
Person pCheck = session.find( PersonOneToOneJoinIgnore.class, 1L );
checkIgnore( pCheck );
}
@ -75,10 +75,10 @@ public void testOneToOneJoinIgnore() {
}
@Test
public void testOneToOneSelectIgnore() {
setupTest( PersonOneToOneSelectIgnore.class, 1L, false );
doInHibernate(
this::sessionFactory, session -> {
public void testOneToOneSelectIgnore(SessionFactoryScope scope) {
setupTest( PersonOneToOneSelectIgnore.class, 1L, false, scope );
scope.inTransaction(
session -> {
Person pCheck = session.find( PersonOneToOneSelectIgnore.class, 1L );
checkIgnore( pCheck );
}
@ -86,10 +86,10 @@ public void testOneToOneSelectIgnore() {
}
@Test
public void testManyToOneJoinIgnore() {
setupTest( PersonManyToOneJoinIgnore.class, 1L, false );
doInHibernate(
this::sessionFactory, session -> {
public void testManyToOneJoinIgnore(SessionFactoryScope scope) {
setupTest( PersonManyToOneJoinIgnore.class, 1L, false, scope );
scope.inTransaction(
session -> {
Person pCheck = session.find( PersonManyToOneJoinIgnore.class, 1L );
checkIgnore( pCheck );
}
@ -97,10 +97,10 @@ public void testManyToOneJoinIgnore() {
}
@Test
public void testManyToOneSelectIgnore() {
setupTest( PersonManyToOneSelectIgnore.class, 1L, false );
doInHibernate(
this::sessionFactory, session -> {
public void testManyToOneSelectIgnore(SessionFactoryScope scope) {
setupTest( PersonManyToOneSelectIgnore.class, 1L, false, scope );
scope.inTransaction(
session -> {
Person pCheck = session.find( PersonManyToOneSelectIgnore.class, 1L );
checkIgnore( pCheck );
}
@ -108,10 +108,10 @@ public void testManyToOneSelectIgnore() {
}
@Test
public void testPkjcOneToOneJoinException() {
setupTest( PersonPkjcJoinException.class, 1L, false );
doInHibernate(
this::sessionFactory, session -> {
public void testPkjcOneToOneJoinException(SessionFactoryScope scope) {
setupTest( PersonPkjcJoinException.class, 1L, false, scope );
scope.inTransaction(
session -> {
Person pCheck = session.find( PersonPkjcJoinException.class, 1L );
// @OneToOne @PrimaryKeyJoinColumn always assumes @NotFound(IGNORE)
checkIgnore( pCheck );
@ -120,10 +120,10 @@ public void testPkjcOneToOneJoinException() {
}
@Test
public void testPkjcOneToOneJoinIgnore() {
setupTest( PersonPkjcJoinIgnore.class, 1L, false );
doInHibernate(
this::sessionFactory, session -> {
public void testPkjcOneToOneJoinIgnore(SessionFactoryScope scope) {
setupTest( PersonPkjcJoinIgnore.class, 1L, false, scope );
scope.inTransaction(
session -> {
Person pCheck = session.find( PersonPkjcJoinIgnore.class, 1L );
// Person is non-null and association is null.
checkIgnore( pCheck );
@ -132,10 +132,10 @@ public void testPkjcOneToOneJoinIgnore() {
}
@Test
public void testPkjcOneToOneSelectException() {
setupTest( PersonPkjcSelectException.class, 1L, false );
doInHibernate(
this::sessionFactory, session -> {
public void testPkjcOneToOneSelectException(SessionFactoryScope scope) {
setupTest( PersonPkjcSelectException.class, 1L, false, scope );
scope.inTransaction(
session -> {
Person pCheck = session.find( PersonPkjcSelectException.class, 1L );
// @OneToOne @PrimaryKeyJoinColumn always assumes @NotFound(IGNORE)
checkIgnore( pCheck );
@ -144,10 +144,10 @@ public void testPkjcOneToOneSelectException() {
}
@Test
public void testPkjcOneToOneSelectIgnore() {
setupTest( PersonPkjcSelectIgnore.class, 1L, false );
doInHibernate(
this::sessionFactory, session -> {
public void testPkjcOneToOneSelectIgnore(SessionFactoryScope scope) {
setupTest( PersonPkjcSelectIgnore.class, 1L, false, scope );
scope.inTransaction(
session -> {
Person pCheck = session.find( PersonPkjcSelectIgnore.class, 1L );
// Person is non-null and association is null.
checkIgnore( pCheck );
@ -156,10 +156,10 @@ public void testPkjcOneToOneSelectIgnore() {
}
@Test
public void testMapsIdOneToOneJoinIgnore() {
setupTest( PersonMapsIdJoinIgnore.class, 1L, true );
doInHibernate(
this::sessionFactory, session -> {
public void testMapsIdOneToOneJoinIgnore(SessionFactoryScope scope) {
setupTest( PersonMapsIdJoinIgnore.class, 1L, true, scope );
scope.inTransaction(
session -> {
Person pCheck = session.find( PersonMapsIdJoinIgnore.class, 1L );
// Person is non-null association is null.
checkIgnore( pCheck );
@ -168,10 +168,10 @@ public void testMapsIdOneToOneJoinIgnore() {
}
@Test
public void testMapsIdOneToOneSelectIgnore() {
setupTest( PersonMapsIdSelectIgnore.class, 1L, true );
doInHibernate(
this::sessionFactory, session -> {
public void testMapsIdOneToOneSelectIgnore(SessionFactoryScope scope) {
setupTest( PersonMapsIdSelectIgnore.class, 1L, true, scope );
scope.inTransaction(
session -> {
Person pCheck = session.find( PersonMapsIdSelectIgnore.class, 1L );
// Person is non-null association is null.
checkIgnore( pCheck );
@ -180,10 +180,10 @@ public void testMapsIdOneToOneSelectIgnore() {
}
@Test
public void testMapsIdJoinColumnOneToOneJoinIgnore() {
setupTest( PersonMapsIdColumnJoinIgnore.class, 1L, true );
doInHibernate(
this::sessionFactory, session -> {
public void testMapsIdJoinColumnOneToOneJoinIgnore(SessionFactoryScope scope) {
setupTest( PersonMapsIdColumnJoinIgnore.class, 1L, true, scope );
scope.inTransaction(
session -> {
Person pCheck = session.find( PersonMapsIdColumnJoinIgnore.class, 1L );
checkIgnore( pCheck );
}
@ -191,34 +191,33 @@ public void testMapsIdJoinColumnOneToOneJoinIgnore() {
}
@Test
public void testMapsIdJoinColumnOneToOneSelectIgnore() {
setupTest( PersonMapsIdColumnSelectIgnore.class, 1L, true );
doInHibernate(
this::sessionFactory, session -> {
public void testMapsIdJoinColumnOneToOneSelectIgnore(SessionFactoryScope scope) {
setupTest( PersonMapsIdColumnSelectIgnore.class, 1L, true, scope );
scope.inTransaction(
session -> {
Person pCheck = session.find( PersonMapsIdColumnSelectIgnore.class, 1L );
checkIgnore( pCheck );
}
);
}
private <T extends Person> void setupTest(Class<T> clazz, long id, boolean isMapsId ) {
persistData( clazz, id, isMapsId );
doInHibernate(
this::sessionFactory, session -> {
private <T extends Person> void setupTest(Class<T> clazz, long id, boolean isMapsId, SessionFactoryScope scope) {
persistData( clazz, id, isMapsId, scope );
scope.inTransaction(
session -> {
Person p = session.find( clazz, id );
assertEquals( "New York", p.getCity().getName() );
}
);
doInHibernate(
this::sessionFactory, session -> {
scope.inTransaction(
session ->
session.createNativeQuery( "delete from City where id = " + id )
.executeUpdate();
}
.executeUpdate()
);
}
private <T extends Person> void persistData(Class<T> clazz, long id, boolean isMapsId) {
private <T extends Person> void persistData(Class<T> clazz, long id, boolean isMapsId, SessionFactoryScope scope) {
final Person person;
try {
person = clazz.newInstance();
@ -227,8 +226,8 @@ private <T extends Person> void persistData(Class<T> clazz, long id, boolean isM
throw new RuntimeException( ex );
}
doInHibernate(
this::sessionFactory, session -> {
scope.inTransaction(
session -> {
City city = new City();
city.setId( id );
city.setName( "New York" );
@ -259,12 +258,15 @@ public abstract static class Person {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void setId(Long id);
public abstract City getCity();
public abstract void setCity(City city);
}