Fix issue with join type for subclass tables

This commit is contained in:
Andrea Boriero 2021-06-21 11:41:09 +02:00 committed by Andrea Boriero
parent 5c8849c824
commit 7bbc4ce618
19 changed files with 744 additions and 730 deletions

View File

@ -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<String> 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(

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.join;
package org.hibernate.orm.test.join;
/**
* @author Gail Badner

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.join;
package org.hibernate.orm.test.join;
import java.util.List;

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.join;
package org.hibernate.orm.test.join;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;

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.join;
package org.hibernate.orm.test.join;
import java.math.BigDecimal;
import javax.persistence.Column;

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Person> personCriteria = criteriaBuilder.createQuery( Person.class );
Root<Person> 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<User> userCriteria = criteriaBuilder.createQuery( User.class );
Root<User> 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() );
}
);
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Thing> 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<Thing> 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<Thing> 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<Thing> 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<Thing> 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<Thing> 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<Thing> 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<Thing> 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<Thing> 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<Thing> 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<Thing> 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<Thing> 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<Thing> 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<Thing> 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<Thing> 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<Thing> 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<Thing> 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<Thing> 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 );
}
);
}
}

View File

@ -29,7 +29,7 @@
-->
<hibernate-mapping package="org.hibernate.test.join" default-access="field">
<hibernate-mapping package="org.hibernate.orm.test.join" default-access="field">
<class name="Person" table="person" lazy="true" discriminator-value="null">

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.join;
package org.hibernate.orm.test.join;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;

View File

@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.join">
<hibernate-mapping package="org.hibernate.orm.test.join">
<class name="Reportable">
<id name="id" type="java.lang.Long">

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.join;
package org.hibernate.orm.test.join;
/**
* @author Gail Badner

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Reportable> criteria = criteriaBuilder.createQuery( Reportable.class );
Root<Reportable> 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<BlogEntry> criteria = criteriaBuilder.createQuery( BlogEntry.class );
Root<BlogEntry> 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" )

View File

@ -13,7 +13,7 @@
This mapping demonstrates optional joined properties
-->
<hibernate-mapping package="org.hibernate.test.join" default-access="field">
<hibernate-mapping package="org.hibernate.orm.test.join" default-access="field">
<class name="Thing" table="thing">

View File

@ -6,7 +6,7 @@
*/
//$Id: $
package org.hibernate.test.join;
package org.hibernate.orm.test.join;
/**

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.join;
package org.hibernate.orm.test.join;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Person> personCriteria = criteriaBuilder.createQuery( Person.class );
Root<Person> 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<User> userCriteria = criteriaBuilder.createQuery( User.class );
Root<User> 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() );
}
);
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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();
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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
}
}