HHH-5757 - OneToOne SQL missing parameter
This commit is contained in:
parent
0a79454773
commit
9962cf9e8e
|
@ -7,6 +7,7 @@
|
|||
package org.hibernate.type;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
|
@ -262,6 +263,22 @@ public abstract class EntityType extends AbstractType implements AssociationType
|
|||
return resolve( hydrate( rs, names, session, owner ), session, owner );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nullSafeSet(PreparedStatement st, Object value, int index, boolean[] settable, SharedSessionContractImplementor session)
|
||||
throws SQLException {
|
||||
if ( settable.length > 0 ) {
|
||||
requireIdentifierOrUniqueKeyType( session.getFactory() )
|
||||
.nullSafeSet( st, getIdentifier( value, session ), index, settable, session );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
|
||||
throws SQLException {
|
||||
requireIdentifierOrUniqueKeyType( session.getFactory() )
|
||||
.nullSafeSet( st, getIdentifier( value, session ), index, session );
|
||||
}
|
||||
|
||||
/**
|
||||
* Two entities are considered the same when their instances are the same.
|
||||
*
|
||||
|
@ -708,4 +725,15 @@ public abstract class EntityType extends AbstractType implements AssociationType
|
|||
return result == null ? null : persistenceContext.proxyFor( result );
|
||||
}
|
||||
|
||||
protected Type requireIdentifierOrUniqueKeyType(Mapping mapping) {
|
||||
final Type fkTargetType = getIdentifierOrUniqueKeyType( mapping );
|
||||
if ( fkTargetType == null ) {
|
||||
throw new MappingException(
|
||||
"Unable to determine FK target Type for many-to-one or one-to-one mapping: " +
|
||||
"referenced-entity-name=[" + getAssociatedEntityName() +
|
||||
"], referenced-entity-attribute-name=[" + getLHSPropertyName() + "]"
|
||||
);
|
||||
}
|
||||
return fkTargetType;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
package org.hibernate.type;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
|
@ -113,18 +112,6 @@ public class ManyToOneType extends EntityType {
|
|||
return requireIdentifierOrUniqueKeyType( mapping ).getColumnSpan( mapping );
|
||||
}
|
||||
|
||||
private Type requireIdentifierOrUniqueKeyType(Mapping mapping) {
|
||||
final Type fkTargetType = getIdentifierOrUniqueKeyType( mapping );
|
||||
if ( fkTargetType == null ) {
|
||||
throw new MappingException(
|
||||
"Unable to determine FK target Type for many-to-one mapping: " +
|
||||
"referenced-entity-name=[" + getAssociatedEntityName() +
|
||||
"], referenced-entity-attribute-name=[" + getLHSPropertyName() + "]"
|
||||
);
|
||||
}
|
||||
return fkTargetType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] sqlTypes(Mapping mapping) throws MappingException {
|
||||
return requireIdentifierOrUniqueKeyType( mapping ).sqlTypes( mapping );
|
||||
|
@ -140,27 +127,6 @@ public class ManyToOneType extends EntityType {
|
|||
return requireIdentifierOrUniqueKeyType( mapping ).defaultSizes( mapping );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nullSafeSet(
|
||||
PreparedStatement st,
|
||||
Object value,
|
||||
int index,
|
||||
boolean[] settable,
|
||||
SharedSessionContractImplementor session) throws HibernateException, SQLException {
|
||||
requireIdentifierOrUniqueKeyType( session.getFactory() )
|
||||
.nullSafeSet( st, getIdentifier( value, session ), index, settable, session );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nullSafeSet(
|
||||
PreparedStatement st,
|
||||
Object value,
|
||||
int index,
|
||||
SharedSessionContractImplementor session) throws HibernateException, SQLException {
|
||||
requireIdentifierOrUniqueKeyType( session.getFactory() )
|
||||
.nullSafeSet( st, getIdentifier( value, session ), index, session );
|
||||
}
|
||||
|
||||
@Override
|
||||
public ForeignKeyDirection getForeignKeyDirection() {
|
||||
return ForeignKeyDirection.FROM_PARENT;
|
||||
|
|
|
@ -113,11 +113,6 @@ public class OneToOneType extends EntityType {
|
|||
//nothing to do
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) {
|
||||
//nothing to do
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOneToOne() {
|
||||
return true;
|
||||
|
|
|
@ -21,12 +21,16 @@ import org.hibernate.mapping.Table;
|
|||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.hibernate.testing.transaction.TransactionUtil;
|
||||
import org.hibernate.test.annotations.Customer;
|
||||
import org.hibernate.test.annotations.Discount;
|
||||
import org.hibernate.test.annotations.Passport;
|
||||
import org.hibernate.test.annotations.Ticket;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
@ -37,148 +41,125 @@ import static org.junit.Assert.assertTrue;
|
|||
public class OneToOneTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
@Test
|
||||
public void testEagerFetching() throws Exception {
|
||||
Session s;
|
||||
Transaction tx;
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
final String clientName = "Emmanuel";
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
Client c = new Client();
|
||||
c.setName( "Emmanuel" );
|
||||
c.setName( clientName );
|
||||
Address a = new Address();
|
||||
a.setCity( "Courbevoie" );
|
||||
c.setAddress( a );
|
||||
s.persist( c );
|
||||
tx.commit();
|
||||
s.close();
|
||||
session.persist( c );
|
||||
} );
|
||||
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
Query q = s.createQuery( "select c from Client c where c.name = :name" );
|
||||
q.setString( "name", c.getName() );
|
||||
c = ( Client ) q.uniqueResult();
|
||||
final Client client = TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
Query q = session.createQuery( "select c from Client c where c.name = :name" );
|
||||
q.setString( "name", clientName );
|
||||
Client c = (Client) q.uniqueResult();
|
||||
//c = (Client) s.get(Client.class, c.getId());
|
||||
assertNotNull( c );
|
||||
tx.commit();
|
||||
s.close();
|
||||
assertNotNull( c.getAddress() );
|
||||
//assertTrue( "Should be eager fetched", Hibernate.isInitialized( c.getAddress() ) );
|
||||
|
||||
assertNotNull( c );
|
||||
return c;
|
||||
} );
|
||||
|
||||
assertNotNull( client.getAddress() );
|
||||
//assertTrue( "Should be eager fetched", Hibernate.isInitialized( c.getAddress() ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultOneToOne() throws Exception {
|
||||
//test a default one to one and a mappedBy in the other side
|
||||
Session s;
|
||||
Transaction tx;
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
Long customerId = TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
Customer c = new Customer();
|
||||
c.setName( "Hibernatus" );
|
||||
Passport p = new Passport();
|
||||
p.setNumber( "123456789" );
|
||||
s.persist( c ); //we need the id to assigned it to passport
|
||||
session.persist( c ); //we need the id to assigned it to passport
|
||||
c.setPassport( p );
|
||||
p.setOwner( c );
|
||||
p.setId( c.getId() );
|
||||
tx.commit();
|
||||
s.close();
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
c = ( Customer ) s.get( Customer.class, c.getId() );
|
||||
return c.getId();
|
||||
} );
|
||||
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
Customer c = session.get( Customer.class, customerId );
|
||||
assertNotNull( c );
|
||||
p = c.getPassport();
|
||||
Passport p = c.getPassport();
|
||||
|
||||
assertNotNull( p );
|
||||
assertEquals( "123456789", p.getNumber() );
|
||||
assertNotNull( p.getOwner() );
|
||||
assertEquals( "Hibernatus", p.getOwner().getName() );
|
||||
tx.commit(); // commit or rollback is the same, we don't care for read queries
|
||||
s.close();
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneToOneWithExplicitFk() throws Exception {
|
||||
Client c = new Client();
|
||||
final Client c = new Client();
|
||||
Address a = new Address();
|
||||
a.setCity( "Paris" );
|
||||
c.setName( "Emmanuel" );
|
||||
c.setAddress( a );
|
||||
|
||||
Session s;
|
||||
Transaction tx;
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
s.persist( c );
|
||||
tx.commit();
|
||||
s.close();
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
session.persist( c );
|
||||
} );
|
||||
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
c = ( Client ) s.get( Client.class, c.getId() );
|
||||
assertNotNull( c );
|
||||
assertNotNull( c.getAddress() );
|
||||
assertEquals( "Paris", c.getAddress().getCity() );
|
||||
tx.commit();
|
||||
s.close();
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
Client client = session.get( Client.class, c.getId() );
|
||||
|
||||
assertNotNull( client );
|
||||
assertNotNull( client.getAddress() );
|
||||
assertEquals( "Paris", client.getAddress().getCity() );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneToOneWithExplicitSecondaryTableFk() throws Exception {
|
||||
Client c = new Client();
|
||||
final Client c = new Client();
|
||||
Address a = new Address();
|
||||
a.setCity( "Paris" );
|
||||
c.setName( "Emmanuel" );
|
||||
c.setSecondaryAddress( a );
|
||||
|
||||
Session s;
|
||||
Transaction tx;
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
s.persist( c );
|
||||
tx.commit();
|
||||
s.close();
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
session.persist( c );
|
||||
} );
|
||||
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
c = ( Client ) s.get( Client.class, c.getId() );
|
||||
assertNotNull( c );
|
||||
assertNotNull( c.getSecondaryAddress() );
|
||||
assertEquals( "Paris", c.getSecondaryAddress().getCity() );
|
||||
tx.commit();
|
||||
s.close();
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
final Client client = session.get( Client.class, c.getId() );
|
||||
|
||||
assertNotNull( client );
|
||||
assertNotNull( client.getSecondaryAddress() );
|
||||
assertEquals( "Paris", client.getSecondaryAddress().getCity() );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnidirectionalTrueOneToOne() throws Exception {
|
||||
Body b = new Body();
|
||||
Heart h = new Heart();
|
||||
final Body b = new Body();
|
||||
final Heart h = new Heart();
|
||||
b.setHeart( h );
|
||||
b.setId( 1 );
|
||||
h.setId( b.getId() ); //same PK
|
||||
Session s;
|
||||
Transaction tx;
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
s.persist( h );
|
||||
s.persist( b );
|
||||
tx.commit();
|
||||
s.close();
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
session.persist( h );
|
||||
session.persist( b );
|
||||
} );
|
||||
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
b = ( Body ) s.get( Body.class, b.getId() );
|
||||
assertNotNull( b );
|
||||
assertNotNull( b.getHeart() );
|
||||
assertEquals( h.getId(), b.getHeart().getId() );
|
||||
tx.commit();
|
||||
s.close();
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
final Body body = session.get( Body.class, b.getId() );
|
||||
|
||||
assertNotNull( body );
|
||||
assertNotNull( body.getHeart() );
|
||||
assertEquals( h.getId(), body.getHeart().getId() );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompositePk() throws Exception {
|
||||
Session s;
|
||||
Transaction tx;
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
ComputerPk cid = new ComputerPk();
|
||||
final ComputerPk cid = new ComputerPk();
|
||||
final SerialNumber sn = new SerialNumber();
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
cid.setBrand( "IBM" );
|
||||
cid.setModel( "ThinkPad" );
|
||||
Computer c = new Computer();
|
||||
|
@ -187,28 +168,23 @@ public class OneToOneTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
SerialNumberPk sid = new SerialNumberPk();
|
||||
sid.setBrand( cid.getBrand() );
|
||||
sid.setModel( cid.getModel() );
|
||||
SerialNumber sn = new SerialNumber();
|
||||
sn.setId( sid );
|
||||
sn.setValue( "REZREZ23424" );
|
||||
c.setSerial( sn );
|
||||
s.persist( c );
|
||||
tx.commit();
|
||||
s.close();
|
||||
session.persist( c );
|
||||
} );
|
||||
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
c = ( Computer ) s.get( Computer.class, cid );
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
Computer c = session.get( Computer.class, cid );
|
||||
assertNotNull( c );
|
||||
assertNotNull( c.getSerial() );
|
||||
assertEquals( sn.getValue(), c.getSerial().getValue() );
|
||||
tx.commit();
|
||||
s.close();
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBidirectionalTrueOneToOne() throws Exception {
|
||||
Session s = openSession();
|
||||
s.getTransaction().begin();
|
||||
try (Session s = openSession()) {
|
||||
Party party = new Party();
|
||||
PartyAffiliate affiliate = new PartyAffiliate();
|
||||
affiliate.partyId = "id";
|
||||
|
@ -216,34 +192,53 @@ public class OneToOneTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
party.partyAffiliate = affiliate;
|
||||
affiliate.party = party;
|
||||
|
||||
s.getTransaction().begin();
|
||||
try {
|
||||
|
||||
s.persist( party );
|
||||
s.getTransaction().commit();
|
||||
}
|
||||
catch (Exception e) {
|
||||
if ( s.getTransaction() != null && s.getTransaction().isActive() ) {
|
||||
s.getTransaction().rollback();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
s.clear();
|
||||
|
||||
Transaction tx = s.beginTransaction();
|
||||
affiliate = ( PartyAffiliate ) s.get( PartyAffiliate.class, "id" );
|
||||
try {
|
||||
affiliate = s.get( PartyAffiliate.class, "id" );
|
||||
assertNotNull( affiliate.party );
|
||||
assertEquals( affiliate.partyId, affiliate.party.partyId );
|
||||
|
||||
s.clear();
|
||||
|
||||
party = ( Party ) s.get( Party.class, "id" );
|
||||
party = s.get( Party.class, "id" );
|
||||
assertNotNull( party.partyAffiliate );
|
||||
assertEquals( party.partyId, party.partyAffiliate.partyId );
|
||||
|
||||
s.delete( party );
|
||||
s.delete( party.partyAffiliate );
|
||||
tx.commit();
|
||||
s.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
if ( s.getTransaction() != null && s.getTransaction().isActive() ) {
|
||||
s.getTransaction().rollback();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBidirectionalFkOneToOne() throws Exception {
|
||||
Session s = openSession();
|
||||
try (Session s = openSession()) {
|
||||
s.getTransaction().begin();
|
||||
Trousers trousers = new Trousers();
|
||||
TrousersZip zip = new TrousersZip();
|
||||
try {
|
||||
trousers.id = 1;
|
||||
zip.id = 2;
|
||||
trousers.zip = zip;
|
||||
|
@ -251,43 +246,56 @@ public class OneToOneTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
s.persist( trousers );
|
||||
s.persist( zip );
|
||||
s.getTransaction().commit();
|
||||
}
|
||||
catch (Exception e) {
|
||||
if ( s.getTransaction() != null && s.getTransaction().isActive() ) {
|
||||
s.getTransaction().rollback();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
s.clear();
|
||||
|
||||
Transaction tx = s.beginTransaction();
|
||||
trousers = ( Trousers ) s.get( Trousers.class, trousers.id );
|
||||
try {
|
||||
trousers = s.get( Trousers.class, trousers.id );
|
||||
assertNotNull( trousers.zip );
|
||||
assertEquals( zip.id, trousers.zip.id );
|
||||
|
||||
s.clear();
|
||||
|
||||
zip = ( TrousersZip ) s.get( TrousersZip.class, zip.id );
|
||||
zip = s.get( TrousersZip.class, zip.id );
|
||||
assertNotNull( zip.trousers );
|
||||
assertEquals( trousers.id, zip.trousers.id );
|
||||
|
||||
s.delete( zip );
|
||||
s.delete( zip.trousers );
|
||||
tx.commit();
|
||||
s.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
if ( s.getTransaction() != null && s.getTransaction().isActive() ) {
|
||||
s.getTransaction().rollback();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForeignGenerator() {
|
||||
Session s = openSession();
|
||||
Transaction tx = s.beginTransaction();
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
Owner owner = new Owner();
|
||||
OwnerAddress address = new OwnerAddress();
|
||||
owner.setAddress( address );
|
||||
address.setOwner( owner );
|
||||
s.persist( owner );
|
||||
s.flush();
|
||||
s.clear();
|
||||
owner = ( Owner ) s.get( Owner.class, owner.getId() );
|
||||
session.persist( owner );
|
||||
session.flush();
|
||||
session.clear();
|
||||
owner = session.get( Owner.class, owner.getId() );
|
||||
assertNotNull( owner );
|
||||
assertNotNull( owner.getAddress() );
|
||||
assertEquals( owner.getId(), owner.getAddress().getId() );
|
||||
tx.rollback();
|
||||
s.close();
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -314,11 +322,11 @@ public class OneToOneTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-6723" )
|
||||
@TestForIssue(jiraKey = "HHH-6723")
|
||||
public void testPkOneToOneSelectStatementDoesNotGenerateExtraJoin() {
|
||||
// This test uses an interceptor to verify that correct number of joins are generated.
|
||||
Session s = openSession(new JoinCounter(1));
|
||||
Transaction tx = s.beginTransaction();
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
|
||||
|
||||
Owner owner = new Owner();
|
||||
OwnerAddress address = new OwnerAddress();
|
||||
owner.setAddress( address );
|
||||
|
@ -327,14 +335,14 @@ public class OneToOneTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
s.flush();
|
||||
s.clear();
|
||||
|
||||
owner = ( Owner ) s.get( Owner.class, owner.getId() );
|
||||
owner = s.get( Owner.class, owner.getId() );
|
||||
assertNotNull( owner );
|
||||
assertNotNull( owner.getAddress() );
|
||||
assertEquals( owner.getId(), owner.getAddress().getId() );
|
||||
s.flush();
|
||||
s.clear();
|
||||
|
||||
address = ( OwnerAddress ) s.get( OwnerAddress.class, address.getId() );
|
||||
address = s.get( OwnerAddress.class, address.getId() );
|
||||
assertNotNull( address );
|
||||
assertNotNull( address.getOwner() );
|
||||
assertEquals( address.getId(), address.getOwner().getId() );
|
||||
|
@ -342,7 +350,7 @@ public class OneToOneTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
s.flush();
|
||||
s.clear();
|
||||
|
||||
owner = ( Owner ) s.createCriteria( Owner.class )
|
||||
owner = (Owner) s.createCriteria( Owner.class )
|
||||
.add( Restrictions.idEq( owner.getId() ) )
|
||||
.uniqueResult();
|
||||
|
||||
|
@ -352,20 +360,50 @@ public class OneToOneTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
s.flush();
|
||||
s.clear();
|
||||
|
||||
address = ( OwnerAddress ) s.createCriteria( OwnerAddress.class )
|
||||
address = (OwnerAddress) s.createCriteria( OwnerAddress.class )
|
||||
.add( Restrictions.idEq( address.getId() ) )
|
||||
.uniqueResult();
|
||||
|
||||
address = ( OwnerAddress ) s.get( OwnerAddress.class, address.getId() );
|
||||
address = s.get( OwnerAddress.class, address.getId() );
|
||||
assertNotNull( address );
|
||||
assertNotNull( address.getOwner() );
|
||||
assertEquals( address.getId(), address.getOwner().getId() );
|
||||
|
||||
s.flush();
|
||||
s.clear();
|
||||
} );
|
||||
}
|
||||
|
||||
tx.rollback();
|
||||
s.close();
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-5757")
|
||||
public void testHqlQuery() throws Exception {
|
||||
//test a default one to one and a mappedBy in the other side
|
||||
final Passport passport = TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
Customer c = new Customer();
|
||||
c.setName( "Hibernatus" );
|
||||
Passport p = new Passport();
|
||||
p.setNumber( "123456789" );
|
||||
session.persist( c ); //we need the id to assigned it to passport
|
||||
c.setPassport( p );
|
||||
p.setOwner( c );
|
||||
p.setId( c.getId() );
|
||||
return p;
|
||||
} );
|
||||
|
||||
final Customer customer = TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
final Customer c = (Customer) session.createQuery( "from Customer c where c.passport = :passport " )
|
||||
.setParameter( "passport", passport ).getSingleResult();
|
||||
|
||||
assertThat( c, is( notNullValue() ) );
|
||||
return c;
|
||||
} );
|
||||
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
final Passport p = (Passport) session.createQuery( "from Passport p where p.owner = :owner " )
|
||||
.setParameter( "owner", customer ).getSingleResult();
|
||||
|
||||
assertThat( p, is( notNullValue() ) );
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -6,17 +6,18 @@
|
|||
*/
|
||||
package org.hibernate.test.annotations.onetoone;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.id.IdentifierGenerationException;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
@ -31,84 +32,133 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase {
|
|||
// @OneToOne(mappedBy="address") with foreign generator
|
||||
@Test
|
||||
public void testBidirForeignIdGenerator() {
|
||||
Session s = openSession();
|
||||
Transaction tx = s.beginTransaction();
|
||||
try {
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
OwnerAddress address = new OwnerAddress();
|
||||
address.setOwner( null );
|
||||
try {
|
||||
s.persist( address );
|
||||
s.flush();
|
||||
|
||||
session.persist( address );
|
||||
session.flush();
|
||||
fail( "should have failed with IdentifierGenerationException" );
|
||||
} );
|
||||
}
|
||||
catch (PersistenceException ex) {
|
||||
assertTyping(IdentifierGenerationException.class, ex.getCause());
|
||||
assertTyping( IdentifierGenerationException.class, ex.getCause() );
|
||||
// expected
|
||||
}
|
||||
finally {
|
||||
tx.rollback();
|
||||
}
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBidirAssignedId() throws Exception {
|
||||
Session s = openSession();
|
||||
s.getTransaction().begin();
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
PartyAffiliate affiliate = new PartyAffiliate();
|
||||
affiliate.partyId = "id";
|
||||
|
||||
s.persist( affiliate );
|
||||
s.getTransaction().commit();
|
||||
session.persist( affiliate );
|
||||
} );
|
||||
|
||||
s.clear();
|
||||
|
||||
Transaction tx = s.beginTransaction();
|
||||
|
||||
affiliate = ( PartyAffiliate ) s.createCriteria(PartyAffiliate.class)
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
PartyAffiliate affiliate = (PartyAffiliate) session.createCriteria(
|
||||
PartyAffiliate.class )
|
||||
.add( Restrictions.idEq( "id" ) )
|
||||
.uniqueResult();
|
||||
assertNotNull( affiliate );
|
||||
assertEquals( "id", affiliate.partyId );
|
||||
assertNull( affiliate.party );
|
||||
} );
|
||||
|
||||
s.clear();
|
||||
|
||||
affiliate = ( PartyAffiliate ) s.get( PartyAffiliate.class, "id" );
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
PartyAffiliate affiliate = session.get(
|
||||
PartyAffiliate.class,
|
||||
"id"
|
||||
);
|
||||
assertNull( affiliate.party );
|
||||
|
||||
s.delete( affiliate );
|
||||
tx.commit();
|
||||
s.close();
|
||||
session.delete( affiliate );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBidirDefaultIdGenerator() throws Exception {
|
||||
Session s = openSession();
|
||||
s.getTransaction().begin();
|
||||
PersonAddress _personAddress = doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
PersonAddress personAddress = new PersonAddress();
|
||||
personAddress.setPerson( null );
|
||||
|
||||
s.persist( personAddress );
|
||||
s.getTransaction().commit();
|
||||
session.persist( personAddress );
|
||||
|
||||
s.clear();
|
||||
return personAddress;
|
||||
}
|
||||
);
|
||||
|
||||
Transaction tx = s.beginTransaction();
|
||||
|
||||
personAddress = ( PersonAddress ) s.createCriteria(PersonAddress.class)
|
||||
.add( Restrictions.idEq( personAddress.getId() ) )
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
PersonAddress personAddress = (PersonAddress) session.createCriteria(
|
||||
PersonAddress.class )
|
||||
.add( Restrictions.idEq( _personAddress.getId() ) )
|
||||
.uniqueResult();
|
||||
assertNotNull( personAddress );
|
||||
assertNull( personAddress.getPerson() );
|
||||
} );
|
||||
|
||||
s.clear();
|
||||
|
||||
personAddress = ( PersonAddress ) s.get( PersonAddress.class, personAddress.getId() );
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
PersonAddress personAddress = session.get(
|
||||
PersonAddress.class,
|
||||
_personAddress.getId()
|
||||
);
|
||||
assertNull( personAddress.getPerson() );
|
||||
|
||||
s.delete( personAddress );
|
||||
tx.commit();
|
||||
s.close();
|
||||
session.delete( personAddress );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-5757")
|
||||
public void testBidirQueryEntityProperty() throws Exception {
|
||||
|
||||
AtomicReference<Person> personHolder = new AtomicReference<>();
|
||||
|
||||
PersonAddress _personAddress = doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
PersonAddress personAddress = new PersonAddress();
|
||||
Person person = new Person();
|
||||
personAddress.setPerson( person );
|
||||
person.setPersonAddress( personAddress );
|
||||
|
||||
session.persist( person );
|
||||
session.persist( personAddress );
|
||||
|
||||
personHolder.set( person );
|
||||
|
||||
return personAddress;
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
PersonAddress personAddress = (PersonAddress) session.createCriteria(
|
||||
PersonAddress.class )
|
||||
.add( Restrictions.idEq( _personAddress.getId() ) )
|
||||
.uniqueResult();
|
||||
assertNotNull( personAddress );
|
||||
assertNotNull( personAddress.getPerson() );
|
||||
} );
|
||||
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
Person person = personHolder.get();
|
||||
// this call throws GenericJDBCException
|
||||
PersonAddress personAddress = (PersonAddress) session.createQuery(
|
||||
"select pa from PersonAddress pa where pa.person = :person", PersonAddress.class )
|
||||
.setParameter( "person", person )
|
||||
.getSingleResult();
|
||||
|
||||
// the other way should also work
|
||||
person = (Person) session.createCriteria( Person.class )
|
||||
.add( Restrictions.eq( "personAddress", personAddress ) )
|
||||
.uniqueResult();
|
||||
|
||||
session.delete( personAddress );
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -6,26 +6,29 @@
|
|||
*/
|
||||
package org.hibernate.test.onetoone.formula;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.FetchMode;
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.criterion.Property;
|
||||
import org.hibernate.dialect.Oracle8iDialect;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
|
||||
import org.hibernate.type.TextType;
|
||||
import org.hibernate.type.descriptor.sql.ClobTypeDescriptor;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.hibernate.testing.transaction.TransactionUtil;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.core.IsNull.notNullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
|
@ -55,129 +58,144 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase {
|
|||
cfg.setProperty(Environment.DEFAULT_BATCH_FETCH_SIZE, "2");
|
||||
}
|
||||
|
||||
private Person person;
|
||||
private Address address;
|
||||
|
||||
@Before
|
||||
public void setUp(){
|
||||
person = new Person();
|
||||
person.setName( "Gavin King" );
|
||||
address = new Address();
|
||||
address.setPerson( person );
|
||||
address.setType( "HOME" );
|
||||
address.setZip( "3181" );
|
||||
address.setState( "VIC" );
|
||||
address.setStreet( "Karbarook Ave" );
|
||||
person.setAddress( address );
|
||||
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
session.persist( person );
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void cleanupTest() {
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
session.delete( person );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneToOneFormula() {
|
||||
Person p = new Person();
|
||||
p.setName("Gavin King");
|
||||
Address a = new Address();
|
||||
a.setPerson(p);
|
||||
a.setType("HOME");
|
||||
a.setZip("3181");
|
||||
a.setState("VIC");
|
||||
a.setStreet("Karbarook Ave");
|
||||
p.setAddress(a);
|
||||
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
s.persist(p);
|
||||
t.commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
p = (Person) s.createQuery("from Person").uniqueResult();
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
|
||||
Person p = (Person) s.createQuery( "from Person" ).uniqueResult();
|
||||
|
||||
assertNotNull( p.getAddress() );
|
||||
assertTrue( Hibernate.isInitialized( p.getAddress() ) );
|
||||
assertNull( p.getMailingAddress() );
|
||||
} );
|
||||
|
||||
s.clear();
|
||||
|
||||
p = (Person) s.createQuery("from Person p left join fetch p.mailingAddress left join fetch p.address").uniqueResult();
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
|
||||
Person p = (Person) s.createQuery(
|
||||
"from Person p left join fetch p.mailingAddress left join fetch p.address" ).uniqueResult();
|
||||
|
||||
assertNotNull( p.getAddress() );
|
||||
assertTrue( Hibernate.isInitialized( p.getAddress() ) );
|
||||
assertNull( p.getMailingAddress() );
|
||||
} );
|
||||
|
||||
s.clear();
|
||||
|
||||
p = (Person) s.createQuery("from Person p left join fetch p.address").uniqueResult();
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
|
||||
Person p = (Person) s.createQuery( "from Person p left join fetch p.address" ).uniqueResult();
|
||||
|
||||
assertNotNull( p.getAddress() );
|
||||
assertTrue( Hibernate.isInitialized( p.getAddress() ) );
|
||||
assertNull( p.getMailingAddress() );
|
||||
} );
|
||||
|
||||
s.clear();
|
||||
|
||||
p = (Person) s.createCriteria(Person.class)
|
||||
.createCriteria("address")
|
||||
.add( Property.forName("zip").eq("3181") )
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
|
||||
Person p = (Person) s.createCriteria( Person.class )
|
||||
.createCriteria( "address" )
|
||||
.add( Property.forName( "zip" ).eq( "3181" ) )
|
||||
.uniqueResult();
|
||||
assertNotNull(p);
|
||||
assertNotNull( p );
|
||||
} );
|
||||
|
||||
s.clear();
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
|
||||
Person p = (Person) s.createCriteria( Person.class )
|
||||
.setFetchMode( "address", FetchMode.JOIN )
|
||||
.uniqueResult();
|
||||
|
||||
p = (Person) s.createCriteria(Person.class)
|
||||
.setFetchMode("address", FetchMode.JOIN)
|
||||
assertNotNull( p.getAddress() );
|
||||
assertTrue( Hibernate.isInitialized( p.getAddress() ) );
|
||||
assertNull( p.getMailingAddress() );
|
||||
} );
|
||||
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
|
||||
Person p = (Person) s.createCriteria( Person.class )
|
||||
.setFetchMode( "mailingAddress", FetchMode.JOIN )
|
||||
.uniqueResult();
|
||||
|
||||
assertNotNull( p.getAddress() );
|
||||
assertTrue( Hibernate.isInitialized( p.getAddress() ) );
|
||||
assertNull( p.getMailingAddress() );
|
||||
|
||||
s.clear();
|
||||
} );
|
||||
}
|
||||
|
||||
p = (Person) s.createCriteria(Person.class)
|
||||
.setFetchMode("mailingAddress", FetchMode.JOIN)
|
||||
.uniqueResult();
|
||||
|
||||
assertNotNull( p.getAddress() );
|
||||
assertTrue( Hibernate.isInitialized( p.getAddress() ) );
|
||||
assertNull( p.getMailingAddress() );
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-5757")
|
||||
public void testQuery() {
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
Person p = (Person) session.createQuery( "from Person p where p.address = :address" ).setParameter(
|
||||
"address",
|
||||
address ).uniqueResult();
|
||||
assertThat( p, notNullValue() );
|
||||
} );
|
||||
|
||||
s.delete(p);
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
Address a = (Address) session.createQuery( "from Address a where a.person = :person" ).setParameter(
|
||||
"person",
|
||||
person ).uniqueResult();
|
||||
assertThat( a, notNullValue() );
|
||||
|
||||
t.commit();
|
||||
s.close();
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneToOneEmbeddedCompositeKey() {
|
||||
Person p = new Person();
|
||||
p.setName("Gavin King");
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
Address a = new Address();
|
||||
a.setPerson(p);
|
||||
a.setType("HOME");
|
||||
a.setZip("3181");
|
||||
a.setState("VIC");
|
||||
a.setStreet("Karbarook Ave");
|
||||
p.setAddress(a);
|
||||
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
s.persist(p);
|
||||
t.commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
|
||||
a = new Address();
|
||||
a.setType("HOME");
|
||||
a.setPerson(p);
|
||||
a = (Address) s.load(Address.class, a);
|
||||
a.setPerson(person);
|
||||
a = session.load(Address.class, a);
|
||||
assertFalse( Hibernate.isInitialized(a) );
|
||||
a.getPerson();
|
||||
a.getType();
|
||||
assertFalse( Hibernate.isInitialized(a) );
|
||||
assertEquals(a.getZip(), "3181");
|
||||
} );
|
||||
|
||||
s.clear();
|
||||
|
||||
a = new Address();
|
||||
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
|
||||
Address a = new Address();
|
||||
a.setType("HOME");
|
||||
a.setPerson(p);
|
||||
Address a2 = (Address) s.get(Address.class, a);
|
||||
a.setPerson(person);
|
||||
Address a2 = session.get(Address.class, a);
|
||||
assertTrue( Hibernate.isInitialized(a) );
|
||||
assertSame(a2, a);
|
||||
assertSame(a2.getPerson(), p); //this is a little bit desirable
|
||||
assertSame(a2.getPerson(), person); //this is a little bit desirable
|
||||
assertEquals(a.getZip(), "3181");
|
||||
} );
|
||||
|
||||
s.delete(a2);
|
||||
s.delete( s.get( Person.class, p.getName() ) ); //this is certainly undesirable! oh well...
|
||||
|
||||
t.commit();
|
||||
s.close();
|
||||
|
||||
|
||||
// s.delete(a2);
|
||||
// s.delete( s.get( Person.class, p.getName() ) ); //this is certainly undesirable! oh well...
|
||||
//
|
||||
// t.commit();
|
||||
// s.close();
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue