diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index fadc259ced..add0c50b94 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -1405,7 +1405,6 @@ public abstract class AbstractEntityPersister return new TableReferenceJoin( determineSubclassTableJoinType( i, - true, Collections.emptySet() ), joinedTableReference, @@ -1448,7 +1447,7 @@ public abstract class AbstractEntityPersister lhs, joinTableExpression, sqlAliasBase, - determineSubclassTableJoinType( i, true, Collections.emptySet() ), + determineSubclassTableJoinType( i, Collections.emptySet() ), getSubclassTableKeyColumns( i ), sqlExpressionResolver ); @@ -4328,9 +4327,8 @@ public abstract class AbstractEntityPersister protected SqlAstJoinType determineSubclassTableJoinType( int subclassTableNumber, - boolean includeSubclasses, Set treatAsDeclarations) { - if ( isClassOrSuperclassTable( subclassTableNumber ) ) { + if ( isClassOrSuperclassJoin( subclassTableNumber ) ) { final boolean shouldInnerJoin = !isInverseTable( subclassTableNumber ) && !isNullableTable( subclassTableNumber ); // the table is either this persister's driving table or (one of) its super class persister's driving @@ -4348,12 +4346,7 @@ public abstract class AbstractEntityPersister return SqlAstJoinType.INNER; } - if ( includeSubclasses - && !isSubclassTableSequentialSelect( subclassTableNumber ) - && !isSubclassTableLazy( subclassTableNumber ) ) { - return SqlAstJoinType.LEFT; - } - return SqlAstJoinType.INNER; + return SqlAstJoinType.LEFT; } protected JoinType determineSubclassTableJoinType( diff --git a/hibernate-core/src/test/java/org/hibernate/test/join/BlogEntry.java b/hibernate-core/src/test/java/org/hibernate/orm/test/join/BlogEntry.java similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/test/join/BlogEntry.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/join/BlogEntry.java index 119772f0b3..531265cd9c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/join/BlogEntry.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/join/BlogEntry.java @@ -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 . */ -package org.hibernate.test.join; +package org.hibernate.orm.test.join; /** * @author Gail Badner diff --git a/hibernate-core/src/test/java/org/hibernate/test/join/Bug.java b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Bug.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/join/Bug.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/join/Bug.java index a723508023..d95b1a66f1 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/join/Bug.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Bug.java @@ -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 . */ -package org.hibernate.test.join; +package org.hibernate.orm.test.join; import java.util.List; diff --git a/hibernate-core/src/test/java/org/hibernate/test/join/Customer.java b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Customer.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/join/Customer.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/join/Customer.java index 623a5442f0..2fdb2122d0 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/join/Customer.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Customer.java @@ -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 . */ -package org.hibernate.test.join; +package org.hibernate.orm.test.join; import javax.persistence.Column; import javax.persistence.DiscriminatorValue; diff --git a/hibernate-core/src/test/java/org/hibernate/test/join/Employee.java b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Employee.java similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/join/Employee.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/join/Employee.java index 2a8260c69c..8bfe0328a3 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/join/Employee.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Employee.java @@ -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 . */ -package org.hibernate.test.join; +package org.hibernate.orm.test.join; import java.math.BigDecimal; import javax.persistence.Column; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/join/JoinTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/join/JoinTest.java new file mode 100755 index 0000000000..b70b4b2bce --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/join/JoinTest.java @@ -0,0 +1,256 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.join; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Iterator; +import java.util.List; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +import org.hibernate.Hibernate; +import org.hibernate.Session; +import org.hibernate.jdbc.AbstractWork; + +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.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author Gavin King + */ +@DomainModel( + xmlMappings = "org/hibernate/orm/test/join/Person.hbm.xml" +) +@SessionFactory +public class JoinTest { + + @Test + public void testSequentialSelects(SessionFactoryScope scope) { + scope.inTransaction( + s -> { + Employee mark = new Employee(); + mark.setName( "Mark" ); + mark.setTitle( "internal sales" ); + mark.setSex( 'M' ); + mark.setAddress( "buckhead" ); + mark.setZip( "30305" ); + mark.setCountry( "USA" ); + + Customer joe = new Customer(); + joe.setName( "Joe" ); + joe.setAddress( "San Francisco" ); + joe.setZip( "XXXXX" ); + joe.setCountry( "USA" ); + joe.setComments( "Very demanding" ); + joe.setSex( 'M' ); + joe.setSalesperson( mark ); + + Person yomomma = new Person(); + yomomma.setName( "mum" ); + yomomma.setSex( 'F' ); + + s.save( yomomma ); + s.save( mark ); + s.save( joe ); + +// assertEquals( s.createQuery("from java.io.Serializable").list().size(), 0 ); + + assertEquals( 3, s.createQuery( "from Person" ).list().size() ); + assertEquals( 1, s.createQuery( "from Person p where p.class is null" ).list().size() ); + assertEquals( 1, s.createQuery( "from Person p where p.class = Customer" ).list().size() ); + assertEquals( 1, s.createQuery( "from Customer c" ).list().size() ); + s.clear(); + + List customers = s.createQuery( "from Customer c left join fetch c.salesperson" ).list(); + for ( Iterator iter = customers.iterator(); iter.hasNext(); ) { + Customer c = (Customer) iter.next(); + assertTrue( Hibernate.isInitialized( c.getSalesperson() ) ); + assertEquals( "Mark", c.getSalesperson().getName() ); + } + assertEquals( 1, customers.size() ); + s.clear(); + + customers = s.createQuery( "from Customer" ).list(); + for ( Iterator iter = customers.iterator(); iter.hasNext(); ) { + Customer c = (Customer) iter.next(); + assertFalse( Hibernate.isInitialized( c.getSalesperson() ) ); + assertEquals( "Mark", c.getSalesperson().getName() ); + } + assertEquals( 1, customers.size() ); + s.clear(); + + + mark = s.get( Employee.class, new Long( mark.getId() ) ); + joe = s.get( Customer.class, new Long( joe.getId() ) ); + + mark.setZip( "30306" ); + assertEquals( 1, s.createQuery( "from Person p where p.zip = '30306'" ).list().size() ); + s.delete( mark ); + s.delete( joe ); + s.delete( yomomma ); + assertTrue( s.createQuery( "from Person" ).list().isEmpty() ); + } + ); + } + + @Test + public void testSequentialSelectsOptionalData(SessionFactoryScope scope) { + scope.inTransaction( + s -> { + User jesus = new User(); + jesus.setName( "Jesus Olvera y Martinez" ); + jesus.setSex( 'M' ); + + s.save( jesus ); + +// assertEquals( 0, s.createQuery("from java.io.Serializable").list().size() ); + + assertEquals( 1, s.createQuery( "from Person" ).list().size() ); + assertEquals( 0, s.createQuery( "from Person p where p.class is null" ).list().size() ); + assertEquals( 1, s.createQuery( "from Person p where p.class = User" ).list().size() ); + assertTrue( s.createQuery( "from User u" ).list().size() == 1 ); + s.clear(); + + // Remove the optional row from the join table and requery the User obj + doWork( s ); + s.clear(); + + jesus = (User) s.get( Person.class, new Long( jesus.getId() ) ); + s.clear(); + + // Cleanup the test data + s.delete( jesus ); + + assertTrue( s.createQuery( "from Person" ).list().isEmpty() ); + + } + ); + } + + private void doWork(final Session s) { + s.doWork( + new AbstractWork() { + @Override + public void execute(Connection connection) throws SQLException { + try (PreparedStatement ps = connection.prepareStatement( "delete from t_user" )) { + ps.execute(); + } + } + } + ); + } + + @Test + public void testCustomColumnReadAndWrite(SessionFactoryScope scope) { + scope.inTransaction( + s -> { + final double HEIGHT_INCHES = 73; + final double HEIGHT_CENTIMETERS = HEIGHT_INCHES * 2.54d; + Person p = new Person(); + p.setName( "Emmanuel" ); + p.setSex( 'M' ); + p.setHeightInches( HEIGHT_INCHES ); + s.persist( p ); + final double PASSWORD_EXPIRY_WEEKS = 4; + final double PASSWORD_EXPIRY_DAYS = PASSWORD_EXPIRY_WEEKS * 7d; + User u = new User(); + u.setName( "Steve" ); + u.setSex( 'M' ); + u.setPasswordExpiryDays( PASSWORD_EXPIRY_DAYS ); + s.persist( u ); + s.flush(); + + // Test value conversion during insert + // Oracle returns BigDecimaal while other dialects return Double; + // casting to Number so it works on all dialects + Number heightViaSql = (Number) s.createNativeQuery( + "select height_centimeters from person where name='Emmanuel'" ).uniqueResult(); + assertEquals( HEIGHT_CENTIMETERS, heightViaSql.doubleValue(), 0.01d ); + Number expiryViaSql = (Number) s.createNativeQuery( + "select pwd_expiry_weeks from t_user where person_id=?" ) + .setParameter( 1, u.getId() ) + .uniqueResult(); + assertEquals( PASSWORD_EXPIRY_WEEKS, expiryViaSql.doubleValue(), 0.01d ); + + // Test projection + Double heightViaHql = (Double) s.createQuery( + "select p.heightInches from Person p where p.name = 'Emmanuel'" ).uniqueResult(); + assertEquals( HEIGHT_INCHES, heightViaHql, 0.01d ); + Double expiryViaHql = (Double) s.createQuery( + "select u.passwordExpiryDays from User u where u.name = 'Steve'" ).uniqueResult(); + assertEquals( PASSWORD_EXPIRY_DAYS, expiryViaHql, 0.01d ); + + // Test restriction and entity load via criteria + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery personCriteria = criteriaBuilder.createQuery( Person.class ); + Root rootPerson = personCriteria.from( Person.class ); + personCriteria.where( criteriaBuilder.between( + rootPerson.get( "heightInches" ), + HEIGHT_INCHES - 0.01d, + HEIGHT_INCHES + 0.01d + ) ); + p = s.createQuery( personCriteria ).uniqueResult(); +// p = (Person)s.createCriteria(Person.class) +// .add(Restrictions.between("heightInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d)) +// .uniqueResult(); + assertEquals( HEIGHT_INCHES, p.getHeightInches(), 0.01d ); + CriteriaQuery userCriteria = criteriaBuilder.createQuery( User.class ); + Root userRoot = userCriteria.from( User.class ); + userCriteria.where( criteriaBuilder.between( + userRoot.get( "passwordExpiryDays" ), + PASSWORD_EXPIRY_DAYS - 0.01d, + PASSWORD_EXPIRY_DAYS + 0.01d + ) ); + u = s.createQuery( userCriteria ).uniqueResult(); +// u = (User)s.createCriteria(User.class) +// .add(Restrictions.between("passwordExpiryDays", PASSWORD_EXPIRY_DAYS - 0.01d, PASSWORD_EXPIRY_DAYS + 0.01d)) +// .uniqueResult(); + assertEquals( PASSWORD_EXPIRY_DAYS, u.getPasswordExpiryDays(), 0.01d ); + + // Test predicate and entity load via HQL + p = (Person) s.createQuery( "from Person p where p.heightInches between ?1 and ?2" ) + .setParameter( 1, HEIGHT_INCHES - 0.01d ) + .setParameter( 2, HEIGHT_INCHES + 0.01d ) + .uniqueResult(); + assertNotNull( p ); + assertEquals( 0.01d, HEIGHT_INCHES, p.getHeightInches() ); + u = (User) s.createQuery( "from User u where u.passwordExpiryDays between ?1 and ?2" ) + .setParameter( 1, PASSWORD_EXPIRY_DAYS - 0.01d ) + .setParameter( 2, PASSWORD_EXPIRY_DAYS + 0.01d ) + .uniqueResult(); + assertEquals( PASSWORD_EXPIRY_DAYS, u.getPasswordExpiryDays(), 0.01d ); + + // Test update + p.setHeightInches( 1 ); + u.setPasswordExpiryDays( 7d ); + s.flush(); + heightViaSql = (Number) s.createNativeQuery( + "select height_centimeters from person where name='Emmanuel'" ).uniqueResult(); + assertEquals( 2.54d, heightViaSql.doubleValue(), 0.01d ); + expiryViaSql = (Number) s.createNativeQuery( "select pwd_expiry_weeks from t_user where person_id=?" ) + .setParameter( 1, u.getId() ) + .uniqueResult(); + assertEquals( 1d, expiryViaSql.doubleValue(), 0.01d ); + + s.delete( p ); + s.delete( u ); + assertTrue( s.createQuery( "from Person" ).list().isEmpty() ); + + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/join/OptionalJoinTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/join/OptionalJoinTest.java new file mode 100644 index 0000000000..aff833a834 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/join/OptionalJoinTest.java @@ -0,0 +1,407 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.join; + +import java.util.List; + +import org.hibernate.dialect.AbstractHANADialect; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.SkipForDialect; +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.assertNull; + +/** + * @author Chris Jones and Gail Badner + */ +@SkipForDialect(dialectClass = AbstractHANADialect.class, reason = " HANA doesn't support tables consisting of only a single auto-generated column") +@DomainModel( + xmlMappings = "org/hibernate/orm/test/join/Thing.hbm.xml" +) +@SessionFactory +public class OptionalJoinTest { + + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createQuery( "delete from Thing" ).executeUpdate() + ); + } + + @Test + public void testUpdateNonNullOptionalJoinToDiffNonNull(SessionFactoryScope scope) { + + scope.inTransaction( + session -> { + // create a new thing with a non-null name + Thing thing = new Thing(); + thing.setName( "one" ); + session.save( thing ); + } + ); + + scope.inTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertEquals( "one", thing.getName() ); + assertEquals( "ONE", thing.getNameUpper() ); + // give it a new non-null name and save it + thing.setName( "one_changed" ); + session.update( thing ); + } + ); + + scope.inTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertEquals( "one_changed", thing.getName() ); + assertEquals( "ONE_CHANGED", thing.getNameUpper() ); + session.delete( thing ); + } + ); + + } + + @Test + public void testUpdateNonNullOptionalJoinToDiffNonNullDetached(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + // create a new thing with a non-null name + Thing thing = new Thing(); + thing.setName( "one" ); + session.save( thing ); + } + ); + + Thing aThing = scope.fromTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertEquals( "one", thing.getName() ); + assertEquals( "ONE", thing.getNameUpper() ); + return thing; + } + ); + + // change detached thing name to a new non-null name and save it + aThing.setName( "one_changed" ); + + scope.inTransaction( + session -> + session.update( aThing ) + ); + + scope.inTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertEquals( "one_changed", thing.getName() ); + assertEquals( "ONE_CHANGED", thing.getNameUpper() ); + session.delete( thing ); + } + ); + } + + @Test + public void testMergeNonNullOptionalJoinToDiffNonNullDetached(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + // create a new thing with a non-null name + Thing thing = new Thing(); + thing.setName( "one" ); + session.save( thing ); + } + ); + + Thing aThing = scope.fromTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertEquals( "one", thing.getName() ); + assertEquals( "ONE", thing.getNameUpper() ); + return thing; + } + ); + + + // change detached thing name to a new non-null name and save it + aThing.setName( "one_changed" ); + + scope.inTransaction( + session -> + session.merge( aThing ) + ); + + scope.inTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertEquals( "one_changed", thing.getName() ); + assertEquals( "ONE_CHANGED", thing.getNameUpper() ); + session.delete( thing ); + } + ); + + } + + @Test + public void testUpdateNonNullOptionalJoinToNull(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + // create a new thing with a non-null name + Thing thing = new Thing(); + thing.setName( "one" ); + session.save( thing ); + } + ); + + scope.inTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertEquals( "one", thing.getName() ); + assertEquals( "ONE", thing.getNameUpper() ); + // give it a null name and save it + thing.setName( null ); + session.update( thing ); + } + ); + + scope.inTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertNull( thing.getName() ); + assertNull( thing.getNameUpper() ); + session.delete( thing ); + } + ); + } + + @Test + public void testUpdateNonNullOptionalJoinToNullDetached(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + // create a new thing with a non-null name + Thing thing = new Thing(); + thing.setName( "one" ); + session.save( thing ); + } + ); + + Thing aThing = scope.fromTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertEquals( "one", thing.getName() ); + assertEquals( "ONE", thing.getNameUpper() ); + return thing; + } + ); + + // give detached thing a null name and save it + aThing.setName( null ); + + scope.inTransaction( + session -> + session.update( aThing ) + ); + + scope.inTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertNull( thing.getName() ); + assertNull( thing.getNameUpper() ); + session.delete( thing ); + } + ); + } + + @Test + public void testMergeNonNullOptionalJoinToNullDetached(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + // create a new thing with a non-null name + Thing thing = new Thing(); + thing.setName( "one" ); + session.save( thing ); + } + ); + + Thing aThing = scope.fromTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertEquals( "one", thing.getName() ); + assertEquals( "ONE", thing.getNameUpper() ); + return thing; + } + ); + + // give detached thing a null name and save it + aThing.setName( null ); + + scope.inTransaction( + session -> + session.merge( aThing ) + ); + + scope.inTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertNull( thing.getName() ); + assertNull( thing.getNameUpper() ); + session.delete( thing ); + } + ); + } + + @Test + public void testUpdateNullOptionalJoinToNonNull(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + // create a new thing with a null name + Thing thing = new Thing(); + thing.setName( null ); + session.save( thing ); + } + ); + + + scope.inTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertNull( thing.getName() ); + // change name to a non-null value + thing.setName( "two" ); + session.update( thing ); + } + ); + + scope.inTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertEquals( "two", thing.getName() ); + assertEquals( "TWO", thing.getNameUpper() ); + session.delete( thing ); + } + ); + } + + @Test + public void testUpdateNullOptionalJoinToNonNullDetached(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + // create a new thing with a null name + Thing thing = new Thing(); + thing.setName( null ); + session.save( thing ); + } + ); + + Thing aThing = scope.fromTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertNull( thing.getName() ); + assertNull( thing.getNameUpper() ); + return thing; + } + ); + + + // change detached thing name to a non-null value + aThing.setName( "two" ); + + scope.inTransaction( + session -> + session.update( aThing ) + ); + + scope.inTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertEquals( "two", thing.getName() ); + assertEquals( "TWO", thing.getNameUpper() ); + session.delete( thing ); + } + ); + } + + @Test + public void testMergeNullOptionalJoinToNonNullDetached(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + // create a new thing with a null name + Thing thing = new Thing(); + thing.setName( null ); + session.save( thing ); + } + ); + + Thing aThing = scope.fromTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertNull( thing.getName() ); + assertNull( thing.getNameUpper() ); + return thing; + } + ); + + + // change detached thing name to a non-null value + aThing.setName( "two" ); + + scope.inTransaction( + session -> + session.update( aThing ) + ); + + scope.inTransaction( + session -> { + List things = session.createQuery( "from Thing" ).list(); + assertEquals( 1, things.size() ); + Thing thing = things.get( 0 ); + assertEquals( "two", thing.getName() ); + assertEquals( "TWO", thing.getNameUpper() ); + session.delete( thing ); + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/join/Person.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Person.hbm.xml similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/join/Person.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/join/Person.hbm.xml index 94dbee2225..d38059e834 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/join/Person.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Person.hbm.xml @@ -29,7 +29,7 @@ --> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/join/Person.java b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Person.java similarity index 98% rename from hibernate-core/src/test/java/org/hibernate/test/join/Person.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/join/Person.java index 88d7cefb66..07fb38e8a6 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/join/Person.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Person.java @@ -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 . */ -package org.hibernate.test.join; +package org.hibernate.orm.test.join; import javax.persistence.Column; import javax.persistence.DiscriminatorColumn; diff --git a/hibernate-core/src/test/java/org/hibernate/test/join/Reportable.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Reportable.hbm.xml similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/join/Reportable.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/join/Reportable.hbm.xml index 96cc9a3912..e5548a9687 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/join/Reportable.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Reportable.hbm.xml @@ -1,7 +1,7 @@ - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/join/Reportable.java b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Reportable.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/join/Reportable.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/join/Reportable.java index 2ca052905b..a8246fb8a9 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/join/Reportable.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Reportable.java @@ -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 . */ -package org.hibernate.test.join; +package org.hibernate.orm.test.join; /** * @author Gail Badner diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/join/SecondaryTableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/join/SecondaryTableTest.java new file mode 100755 index 0000000000..569fd05fbc --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/join/SecondaryTableTest.java @@ -0,0 +1,36 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.join; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +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.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Copy of the model used in JoinTest, but using annotations rather than hbm.xml to look + * for the duplicate joins warning + * + * @author Steve Ebersole + */ +@DomainModel( + annotatedClasses = {Person.class, Employee.class, Customer.class, User.class} +) +@SessionFactory +public class SecondaryTableTest { + + + @Test + public void testIt() { + // really we have nothing to test + } +} + diff --git a/hibernate-core/src/test/java/org/hibernate/test/join/SubclassesWithSamePropertyNameTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/join/SubclassesWithSamePropertyNameTest.java similarity index 74% rename from hibernate-core/src/test/java/org/hibernate/test/join/SubclassesWithSamePropertyNameTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/join/SubclassesWithSamePropertyNameTest.java index 0f4b8d00e8..1700993c17 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/join/SubclassesWithSamePropertyNameTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/join/SubclassesWithSamePropertyNameTest.java @@ -4,21 +4,21 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.join; +package org.hibernate.orm.test.join; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; -import org.junit.Test; - -import org.hibernate.Session; -import org.hibernate.Transaction; - import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +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 static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Two subclasses of Reportable each have a property with the same name: @@ -29,18 +29,17 @@ import static org.junit.Assert.assertEquals; * @author Gail Badner */ @TestForIssue(jiraKey = "HHH-11241") -public class SubclassesWithSamePropertyNameTest extends BaseCoreFunctionalTestCase { +@DomainModel( + xmlMappings = "org/hibernate/orm/test/join/Reportable.hbm.xml" +) +@SessionFactory +public class SubclassesWithSamePropertyNameTest { private Long blogEntryId; - @Override - public String[] getMappings() { - return new String[] { "join/Reportable.hbm.xml" }; - } - - @Override - protected void prepareTest() { + @BeforeEach + public void prepareTest(SessionFactoryScope scope) { BlogEntry blogEntry = new BlogEntry(); - inTransaction( + scope.inTransaction( s -> { blogEntry.setDetail( "detail" ); blogEntry.setReportedBy( "John Doe" ); @@ -50,17 +49,17 @@ public class SubclassesWithSamePropertyNameTest extends BaseCoreFunctionalTestCa blogEntryId = blogEntry.getId(); } - @Override - protected void cleanupTest() { - inTransaction( + @AfterEach + public void cleanupTest(SessionFactoryScope scope) { + scope.inTransaction( s -> s.createQuery( "delete from BlogEntry" ).executeUpdate() ); } @Test @TestForIssue(jiraKey = "HHH-11241") - public void testGetSuperclass() { - inTransaction( + public void testGetSuperclass(SessionFactoryScope scope) { + scope.inTransaction( s -> { Reportable reportable = s.get( Reportable.class, blogEntryId ); assertEquals( "John Doe", reportable.getReportedBy() ); @@ -71,8 +70,8 @@ public class SubclassesWithSamePropertyNameTest extends BaseCoreFunctionalTestCa @Test @TestForIssue(jiraKey = "HHH-11241") - public void testQuerySuperclass() { - inTransaction( + public void testQuerySuperclass(SessionFactoryScope scope) { + scope.inTransaction( s -> { Reportable reportable = (Reportable) s.createQuery( "from Reportable where reportedBy='John Doe'" @@ -86,13 +85,13 @@ public class SubclassesWithSamePropertyNameTest extends BaseCoreFunctionalTestCa @Test @TestForIssue(jiraKey = "HHH-11241") - public void testCriteriaSuperclass() { - inTransaction( + public void testCriteriaSuperclass(SessionFactoryScope scope) { + scope.inTransaction( s -> { CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); CriteriaQuery criteria = criteriaBuilder.createQuery( Reportable.class ); Root root = criteria.from( Reportable.class ); - criteria.where( criteriaBuilder.equal( root.get( "reportedBy" ),"John Doe" ) ); + criteria.where( criteriaBuilder.equal( root.get( "reportedBy" ), "John Doe" ) ); Reportable reportable = s.createQuery( criteria ).uniqueResult(); // Reportable reportable = // (Reportable) s.createCriteria( Reportable.class, "r" ) @@ -107,8 +106,8 @@ public class SubclassesWithSamePropertyNameTest extends BaseCoreFunctionalTestCa @Test @TestForIssue(jiraKey = "HHH-11241") - public void testQuerySubclass() { - inTransaction( + public void testQuerySubclass(SessionFactoryScope scope) { + scope.inTransaction( s -> { BlogEntry blogEntry = (BlogEntry) s.createQuery( "from BlogEntry where reportedBy='John Doe'" @@ -122,13 +121,13 @@ public class SubclassesWithSamePropertyNameTest extends BaseCoreFunctionalTestCa @Test @TestForIssue(jiraKey = "HHH-11241") - public void testCriteriaSubclass() { - inTransaction( + public void testCriteriaSubclass(SessionFactoryScope scope) { + scope.inTransaction( s -> { CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); CriteriaQuery criteria = criteriaBuilder.createQuery( BlogEntry.class ); Root root = criteria.from( BlogEntry.class ); - criteria.where( criteriaBuilder.equal( root.get( "reportedBy" ),"John Doe" ) ); + criteria.where( criteriaBuilder.equal( root.get( "reportedBy" ), "John Doe" ) ); BlogEntry blogEntry = s.createQuery( criteria ).uniqueResult(); // BlogEntry blogEntry = // (BlogEntry) s.createCriteria( BlogEntry.class, "r" ) diff --git a/hibernate-core/src/test/java/org/hibernate/test/join/Thing.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Thing.hbm.xml similarity index 90% rename from hibernate-core/src/test/java/org/hibernate/test/join/Thing.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/join/Thing.hbm.xml index f18980894a..9e9d7bfa82 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/join/Thing.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Thing.hbm.xml @@ -13,7 +13,7 @@ This mapping demonstrates optional joined properties --> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/join/Thing.java b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Thing.java similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/join/Thing.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/join/Thing.java index 39340159c8..40736cf3d5 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/join/Thing.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/join/Thing.java @@ -6,7 +6,7 @@ */ //$Id: $ -package org.hibernate.test.join; +package org.hibernate.orm.test.join; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/join/User.java b/hibernate-core/src/test/java/org/hibernate/orm/test/join/User.java similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/join/User.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/join/User.java index b729a6e07e..b8609b8b20 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/join/User.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/join/User.java @@ -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 . */ -package org.hibernate.test.join; +package org.hibernate.orm.test.join; import javax.persistence.Column; import javax.persistence.DiscriminatorValue; diff --git a/hibernate-core/src/test/java/org/hibernate/test/join/JoinTest.java b/hibernate-core/src/test/java/org/hibernate/test/join/JoinTest.java deleted file mode 100755 index 56ae5f2eb0..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/join/JoinTest.java +++ /dev/null @@ -1,247 +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 . - */ -package org.hibernate.test.join; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.Iterator; -import java.util.List; - -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Root; - -import org.hibernate.Hibernate; -import org.hibernate.Session; -import org.hibernate.cfg.Configuration; -import org.hibernate.jdbc.AbstractWork; - -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** - * @author Gavin King - */ -public class JoinTest extends BaseCoreFunctionalTestCase { - @Override - public String[] getMappings() { - return new String[] { "join/Person.hbm.xml" }; - } - - @Override - protected void configure(Configuration configuration) { - super.afterConfigurationBuilt( configuration ); - } - - @Test - public void testSequentialSelects() { - inTransaction( - s -> { - Employee mark = new Employee(); - mark.setName("Mark"); - mark.setTitle("internal sales"); - mark.setSex('M'); - mark.setAddress("buckhead"); - mark.setZip("30305"); - mark.setCountry("USA"); - - Customer joe = new Customer(); - joe.setName("Joe"); - joe.setAddress("San Francisco"); - joe.setZip("XXXXX"); - joe.setCountry("USA"); - joe.setComments("Very demanding"); - joe.setSex('M'); - joe.setSalesperson(mark); - - Person yomomma = new Person(); - yomomma.setName("mum"); - yomomma.setSex('F'); - - s.save(yomomma); - s.save(mark); - s.save(joe); - - assertEquals( s.createQuery("from java.io.Serializable").list().size(), 0 ); - - assertEquals( s.createQuery("from Person").list().size(), 3 ); - assertEquals( s.createQuery("from Person p where p.class is null").list().size(), 1 ); - assertEquals( s.createQuery("from Person p where p.class = Customer").list().size(), 1 ); - assertTrue(s.createQuery("from Customer c").list().size()==1); - s.clear(); - - List customers = s.createQuery("from Customer c left join fetch c.salesperson").list(); - for ( Iterator iter = customers.iterator(); iter.hasNext(); ) { - Customer c = (Customer) iter.next(); - assertTrue( Hibernate.isInitialized( c.getSalesperson() ) ); - assertEquals( c.getSalesperson().getName(), "Mark" ); - } - assertEquals( customers.size(), 1 ); - s.clear(); - - customers = s.createQuery("from Customer").list(); - for ( Iterator iter = customers.iterator(); iter.hasNext(); ) { - Customer c = (Customer) iter.next(); - assertFalse( Hibernate.isInitialized( c.getSalesperson() ) ); - assertEquals( c.getSalesperson().getName(), "Mark" ); - } - assertEquals( customers.size(), 1 ); - s.clear(); - - - mark = s.get( Employee.class, new Long( mark.getId() ) ); - joe = s.get( Customer.class, new Long( joe.getId() ) ); - - mark.setZip("30306"); - assertEquals( s.createQuery("from Person p where p.zip = '30306'").list().size(), 1 ); - s.delete(mark); - s.delete(joe); - s.delete(yomomma); - assertTrue( s.createQuery("from Person").list().isEmpty() ); - } - ); - } - - @Test - public void testSequentialSelectsOptionalData() { - inTransaction( - s -> { - User jesus = new User(); - jesus.setName("Jesus Olvera y Martinez"); - jesus.setSex('M'); - - s.save(jesus); - - assertEquals( s.createQuery("from java.io.Serializable").list().size(), 0 ); - - assertEquals( s.createQuery("from Person").list().size(), 1 ); - assertEquals( s.createQuery("from Person p where p.class is null").list().size(), 0 ); - assertEquals( s.createQuery("from Person p where p.class = User").list().size(), 1 ); - assertTrue(s.createQuery("from User u").list().size()==1); - s.clear(); - - // Remove the optional row from the join table and requery the User obj - doWork(s); - s.clear(); - - jesus = (User) s.get( Person.class, new Long( jesus.getId() ) ); - s.clear(); - - // Cleanup the test data - s.delete(jesus); - - assertTrue( s.createQuery("from Person").list().isEmpty() ); - - } - ); - } - - private void doWork(final Session s) { - s.doWork( - new AbstractWork() { - @Override - public void execute(Connection connection) throws SQLException { - PreparedStatement ps = connection.prepareStatement( "delete from t_user" ); - ps.execute(); - } - } - ); - } - - @Test - public void testCustomColumnReadAndWrite() { - inTransaction( - s -> { - final double HEIGHT_INCHES = 73; - final double HEIGHT_CENTIMETERS = HEIGHT_INCHES * 2.54d; - Person p = new Person(); - p.setName("Emmanuel"); - p.setSex('M'); - p.setHeightInches(HEIGHT_INCHES); - s.persist(p); - final double PASSWORD_EXPIRY_WEEKS = 4; - final double PASSWORD_EXPIRY_DAYS = PASSWORD_EXPIRY_WEEKS * 7d; - User u = new User(); - u.setName("Steve"); - u.setSex('M'); - u.setPasswordExpiryDays(PASSWORD_EXPIRY_DAYS); - s.persist(u); - s.flush(); - - // Test value conversion during insert - // Oracle returns BigDecimaal while other dialects return Double; - // casting to Number so it works on all dialects - Number heightViaSql = (Number)s.createNativeQuery("select height_centimeters from person where name='Emmanuel'").uniqueResult(); - assertEquals(HEIGHT_CENTIMETERS, heightViaSql.doubleValue(), 0.01d); - Number expiryViaSql = (Number)s.createNativeQuery("select pwd_expiry_weeks from t_user where person_id=?") - .setParameter(1, u.getId()) - .uniqueResult(); - assertEquals(PASSWORD_EXPIRY_WEEKS, expiryViaSql.doubleValue(), 0.01d); - - // Test projection - Double heightViaHql = (Double)s.createQuery("select p.heightInches from Person p where p.name = 'Emmanuel'").uniqueResult(); - assertEquals(HEIGHT_INCHES, heightViaHql, 0.01d); - Double expiryViaHql = (Double)s.createQuery("select u.passwordExpiryDays from User u where u.name = 'Steve'").uniqueResult(); - assertEquals(PASSWORD_EXPIRY_DAYS, expiryViaHql, 0.01d); - - // Test restriction and entity load via criteria - CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); - CriteriaQuery personCriteria = criteriaBuilder.createQuery( Person.class ); - Root rootPerson = personCriteria.from( Person.class ); - personCriteria.where( criteriaBuilder.between( rootPerson.get( "heightInches" ),HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d ) ); - p = s.createQuery( personCriteria ).uniqueResult(); -// p = (Person)s.createCriteria(Person.class) -// .add(Restrictions.between("heightInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d)) -// .uniqueResult(); - assertEquals(HEIGHT_INCHES, p.getHeightInches(), 0.01d); - CriteriaQuery userCriteria = criteriaBuilder.createQuery( User.class ); - Root userRoot = userCriteria.from( User.class ); - userCriteria.where( criteriaBuilder.between( userRoot.get( "passwordExpiryDays" ), PASSWORD_EXPIRY_DAYS - 0.01d, PASSWORD_EXPIRY_DAYS + 0.01d ) ); - u = s.createQuery( userCriteria ).uniqueResult(); -// u = (User)s.createCriteria(User.class) -// .add(Restrictions.between("passwordExpiryDays", PASSWORD_EXPIRY_DAYS - 0.01d, PASSWORD_EXPIRY_DAYS + 0.01d)) -// .uniqueResult(); - assertEquals(PASSWORD_EXPIRY_DAYS, u.getPasswordExpiryDays(), 0.01d); - - // Test predicate and entity load via HQL - p = (Person)s.createQuery("from Person p where p.heightInches between ?1 and ?2") - .setParameter(1, HEIGHT_INCHES - 0.01d) - .setParameter(2, HEIGHT_INCHES + 0.01d) - .uniqueResult(); - assertNotNull( p ); - assertEquals(HEIGHT_INCHES, p.getHeightInches(), 0.01d); - u = (User)s.createQuery("from User u where u.passwordExpiryDays between ?1 and ?2") - .setParameter(1, PASSWORD_EXPIRY_DAYS - 0.01d) - .setParameter(2, PASSWORD_EXPIRY_DAYS + 0.01d) - .uniqueResult(); - assertEquals(PASSWORD_EXPIRY_DAYS, u.getPasswordExpiryDays(), 0.01d); - - // Test update - p.setHeightInches(1); - u.setPasswordExpiryDays(7d); - s.flush(); - heightViaSql = (Number)s.createNativeQuery("select height_centimeters from person where name='Emmanuel'").uniqueResult(); - assertEquals(2.54d, heightViaSql.doubleValue(), 0.01d); - expiryViaSql = (Number)s.createNativeQuery("select pwd_expiry_weeks from t_user where person_id=?") - .setParameter(1, u.getId()) - .uniqueResult(); - assertEquals(1d, expiryViaSql.doubleValue(), 0.01d); - - s.delete(p); - s.delete(u); - assertTrue( s.createQuery("from Person").list().isEmpty() ); - - } - ); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/join/OptionalJoinTest.java b/hibernate-core/src/test/java/org/hibernate/test/join/OptionalJoinTest.java deleted file mode 100644 index 01c36afd16..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/join/OptionalJoinTest.java +++ /dev/null @@ -1,396 +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 . - */ -package org.hibernate.test.join; - -import java.util.List; - -import org.junit.Test; - -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.dialect.AbstractHANADialect; -import org.hibernate.testing.SkipForDialect; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -/** - * @author Chris Jones and Gail Badner - */ -@SkipForDialect(value = AbstractHANADialect.class, comment = " HANA doesn't support tables consisting of only a single auto-generated column") -public class OptionalJoinTest extends BaseCoreFunctionalTestCase { - @Override - public String[] getMappings() { - return new String[] { "join/Thing.hbm.xml" }; - } - - @Test - public void testUpdateNonNullOptionalJoinToDiffNonNull() throws Exception { - Session s = openSession(); - Transaction t = s.beginTransaction(); - - // create a new thing with a non-null name - Thing thing = new Thing(); - thing.setName("one"); - s.save(thing); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - List things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = (Thing)things.get(0); - assertEquals("one", thing.getName()); - assertEquals( "ONE", thing.getNameUpper() ); - // give it a new non-null name and save it - thing.setName("one_changed"); - s.update( thing ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = (Thing)things.get(0); - assertEquals("one_changed", thing.getName()); - assertEquals("ONE_CHANGED", thing.getNameUpper()); - s.delete(thing); - t.commit(); - s.close(); - } - - @Test - public void testUpdateNonNullOptionalJoinToDiffNonNullDetached() throws Exception { - Session s = openSession(); - Transaction t = s.beginTransaction(); - - // create a new thing with a non-null name - Thing thing = new Thing(); - thing.setName("one"); - s.save(thing); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - List things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = (Thing)things.get(0); - assertEquals("one", thing.getName()); - assertEquals("ONE", thing.getNameUpper()); - t.commit(); - s.close(); - - // change detached thing name to a new non-null name and save it - thing.setName("one_changed"); - - s = openSession(); - t = s.beginTransaction(); - s.update(thing); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = (Thing)things.get(0); - assertEquals("one_changed", thing.getName()); - assertEquals("ONE_CHANGED", thing.getNameUpper()); - s.delete(thing); - t.commit(); - s.close(); - } - - @Test - public void testMergeNonNullOptionalJoinToDiffNonNullDetached() throws Exception { - Session s = openSession(); - Transaction t = s.beginTransaction(); - - // create a new thing with a non-null name - Thing thing = new Thing(); - thing.setName("one"); - s.save(thing); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - List things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = (Thing)things.get(0); - assertEquals("one", thing.getName()); - assertEquals("ONE", thing.getNameUpper()); - t.commit(); - s.close(); - - // change detached thing name to a new non-null name and save it - thing.setName("one_changed"); - - s = openSession(); - t = s.beginTransaction(); - s.merge(thing); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = (Thing)things.get(0); - assertEquals("one_changed", thing.getName()); - assertEquals("ONE_CHANGED", thing.getNameUpper()); - s.delete(thing); - t.commit(); - s.close(); - } - - @Test - public void testUpdateNonNullOptionalJoinToNull() throws Exception { - Session s = openSession(); - Transaction t = s.beginTransaction(); - - // create a new thing with a non-null name - Thing thing = new Thing(); - thing.setName("one"); - s.save(thing); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - List things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = (Thing)things.get(0); - assertEquals("one", thing.getName()); - assertEquals("ONE", thing.getNameUpper()); - // give it a null name and save it - thing.setName(null); - s.update( thing ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = (Thing)things.get(0); - assertNull(thing.getName()); - assertNull(thing.getNameUpper()); - s.delete(thing); - t.commit(); - s.close(); - } - - @Test - public void testUpdateNonNullOptionalJoinToNullDetached() throws Exception { - Session s = openSession(); - Transaction t = s.beginTransaction(); - - // create a new thing with a non-null name - Thing thing = new Thing(); - thing.setName("one"); - s.save(thing); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - List things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = (Thing)things.get(0); - assertEquals("one", thing.getName()); - assertEquals("ONE", thing.getNameUpper()); - t.commit(); - s.close(); - - // give detached thing a null name and save it - thing.setName(null); - - s = openSession(); - t = s.beginTransaction(); - s.update(thing); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = (Thing)things.get(0); - assertNull(thing.getName()); - assertNull(thing.getNameUpper()); - s.delete(thing); - t.commit(); - s.close(); - } - - @Test - public void testMergeNonNullOptionalJoinToNullDetached() throws Exception { - Session s = openSession(); - Transaction t = s.beginTransaction(); - - // create a new thing with a non-null name - Thing thing = new Thing(); - thing.setName("one"); - s.save(thing); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - List things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = (Thing)things.get(0); - assertEquals("one", thing.getName()); - assertEquals("ONE", thing.getNameUpper()); - t.commit(); - s.close(); - - // give detached thing a null name and save it - thing.setName(null); - - s = openSession(); - t = s.beginTransaction(); - s.merge(thing); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = (Thing)things.get(0); - assertNull(thing.getName()); - assertNull(thing.getNameUpper()); - s.delete(thing); - t.commit(); - s.close(); - } - - @Test - public void testUpdateNullOptionalJoinToNonNull() throws Exception { - Session s = openSession(); - Transaction t = s.beginTransaction(); - // create a new thing with a null name - Thing thing = new Thing(); - thing.setName(null); - s.save(thing); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - List things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = (Thing)things.get(0); - assertNull(thing.getName()); - // change name to a non-null value - thing.setName("two"); - s.update( thing ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = ((Thing) things.get(0)); - assertEquals("two", thing.getName()); - assertEquals("TWO", thing.getNameUpper()); - s.delete(thing); - t.commit(); - s.close(); - } - - @Test - public void testUpdateNullOptionalJoinToNonNullDetached() throws Exception { - Session s = openSession(); - Transaction t = s.beginTransaction(); - // create a new thing with a null name - Thing thing = new Thing(); - thing.setName(null); - s.save(thing); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - List things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = (Thing)things.get(0); - assertNull(thing.getName()); - assertNull(thing.getNameUpper()); - t.commit(); - s.close(); - - // change detached thing name to a non-null value - thing.setName("two"); - - s = openSession(); - t = s.beginTransaction(); - s.update(thing); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = ((Thing) things.get(0)); - assertEquals("two", thing.getName()); - assertEquals("TWO", thing.getNameUpper()); - s.delete(thing); - t.commit(); - s.close(); - } - - @Test - public void testMergeNullOptionalJoinToNonNullDetached() throws Exception { - Session s = openSession(); - Transaction t = s.beginTransaction(); - // create a new thing with a null name - Thing thing = new Thing(); - thing.setName(null); - s.save(thing); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - List things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = (Thing)things.get(0); - assertNull(thing.getName()); - assertNull(thing.getNameUpper()); - t.commit(); - s.close(); - - // change detached thing name to a non-null value - thing.setName("two"); - - s = openSession(); - t = s.beginTransaction(); - s.merge(thing); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - things = s.createQuery("from Thing").list(); - assertEquals(1, things.size()); - thing = ((Thing) things.get(0)); - assertEquals("two", thing.getName()); - assertEquals("TWO", thing.getNameUpper()); - s.delete(thing); - t.commit(); - s.close(); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/join/SecondaryTableTest.java b/hibernate-core/src/test/java/org/hibernate/test/join/SecondaryTableTest.java deleted file mode 100755 index 6d567a78d4..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/join/SecondaryTableTest.java +++ /dev/null @@ -1,34 +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 . - */ -package org.hibernate.test.join; - -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; - -/** - * Copy of the model used in JoinTest, but using annotations rather than hbm.xml to look - * for the duplicate joins warning - * - * @author Steve Ebersole - */ -public class SecondaryTableTest extends BaseCoreFunctionalTestCase { - @Override - public String[] getMappings() { - return null; - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] {Person.class, Employee.class, Customer.class, User.class}; - } - - @Test - public void testIt() { - // really we have nothing to tes - } -} -