HHH-8703 - Remove NonFlushedChanges api

This commit is contained in:
Steve Ebersole 2013-11-18 08:29:14 -06:00
parent 220a27af64
commit d6fa2b2864
28 changed files with 0 additions and 3323 deletions

View File

@ -1,33 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009-2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.engine.spi;
import java.io.Serializable;
public interface NonFlushedChanges extends Serializable {
/**
* Remove the non-flushed changes from this NonFlushedChanges object.
*/
void clear();
}

View File

@ -284,16 +284,6 @@ public class SessionDelegatorBaseImpl implements SessionImplementor, Session {
return sessionImplementor.executeNativeUpdate( specification, queryParameters );
}
@Override
public NonFlushedChanges getNonFlushedChanges() throws HibernateException {
return sessionImplementor.getNonFlushedChanges();
}
@Override
public void applyNonFlushedChanges(NonFlushedChanges nonFlushedChanges) throws HibernateException {
sessionImplementor.applyNonFlushedChanges( nonFlushedChanges );
}
@Override
public CacheMode getCacheMode() {
return sessionImplementor.getCacheMode();

View File

@ -306,23 +306,6 @@ public interface SessionImplementor extends Serializable, LobCreationContext {
int executeNativeUpdate(NativeSQLQuerySpecification specification, QueryParameters queryParameters) throws HibernateException;
/**
* Return changes to this session that have not been flushed yet.
*
* @return The non-flushed changes.
*/
public NonFlushedChanges getNonFlushedChanges() throws HibernateException;
/**
* Apply non-flushed changes from a different session to this session. It is assumed
* that this SessionImpl is "clean" (e.g., has no non-flushed changes, no cached entities,
* no cached collections, no queued actions). The specified NonFlushedChanges object cannot
* be bound to any session.
* <p/>
* @param nonFlushedChanges the non-flushed changes
*/
public void applyNonFlushedChanges(NonFlushedChanges nonFlushedChanges) throws HibernateException;
// copied from Session:
public CacheMode getCacheMode();

View File

@ -1,75 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009-2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.internal;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.jboss.logging.Logger;
import org.hibernate.engine.internal.StatefulPersistenceContext;
import org.hibernate.engine.spi.ActionQueue;
import org.hibernate.engine.spi.NonFlushedChanges;
import org.hibernate.event.spi.EventSource;
public final class NonFlushedChangesImpl implements NonFlushedChanges, Serializable {
private static final Logger LOG = Logger.getLogger( NonFlushedChangesImpl.class.getName() );
private transient ActionQueue actionQueue;
private transient StatefulPersistenceContext persistenceContext;
public NonFlushedChangesImpl(EventSource session) {
this.actionQueue = session.getActionQueue();
this.persistenceContext = ( StatefulPersistenceContext ) session.getPersistenceContext();
}
/* package-protected */
ActionQueue getActionQueue() {
return actionQueue;
}
/* package-protected */
StatefulPersistenceContext getPersistenceContext() {
return persistenceContext;
}
public void clear() {
}
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
LOG.trace( "Deserializing NonFlushedChangesImpl" );
ois.defaultReadObject();
persistenceContext = StatefulPersistenceContext.deserialize( ois, null );
actionQueue = ActionQueue.deserialize( ois, null );
}
private void writeObject(ObjectOutputStream oos) throws IOException {
LOG.trace( "Serializing NonFlushedChangesImpl" );
oos.defaultWriteObject();
persistenceContext.serialize( oos );
actionQueue.serialize( oos );
}
}

View File

@ -97,7 +97,6 @@ import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.engine.spi.NonFlushedChanges;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.QueryParameters;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -416,129 +415,6 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
flush();
}
/**
* Return changes to this session and its child sessions that have not been flushed yet.
* <p/>
* @return The non-flushed changes.
*/
public NonFlushedChanges getNonFlushedChanges() throws HibernateException {
errorIfClosed();
checkTransactionSynchStatus();
return new NonFlushedChangesImpl( this );
}
/**
* Apply non-flushed changes from a different session to this session. It is assumed
* that this SessionImpl is "clean" (e.g., has no non-flushed changes, no cached entities,
* no cached collections, no queued actions). The specified NonFlushedChanges object cannot
* be bound to any session.
* <p/>
* @param nonFlushedChanges the non-flushed changes
*/
public void applyNonFlushedChanges(NonFlushedChanges nonFlushedChanges) throws HibernateException {
errorIfClosed();
checkTransactionSynchStatus();
// todo : why aren't these just part of the NonFlushedChanges API ?
replacePersistenceContext( ((NonFlushedChangesImpl) nonFlushedChanges).getPersistenceContext() );
replaceActionQueue( ((NonFlushedChangesImpl) nonFlushedChanges).getActionQueue() );
}
private void replacePersistenceContext(StatefulPersistenceContext persistenceContextNew) {
if ( persistenceContextNew.getSession() != null ) {
throw new IllegalStateException( "new persistence context is already connected to a session " );
}
persistenceContext.clear();
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream( new ByteArrayInputStream( serializePersistenceContext( persistenceContextNew ) ) );
this.persistenceContext = StatefulPersistenceContext.deserialize( ois, this );
}
catch (IOException ex) {
throw new SerializationException( "could not deserialize the persistence context", ex );
}
catch (ClassNotFoundException ex) {
throw new SerializationException( "could not deserialize the persistence context", ex );
}
finally {
try {
if (ois != null) ois.close();
}
catch (IOException ignore) {
}
}
}
private static byte[] serializePersistenceContext(StatefulPersistenceContext pc) {
ByteArrayOutputStream baos = new ByteArrayOutputStream( 512 );
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream( baos );
( pc ).serialize( oos );
}
catch (IOException ex) {
throw new SerializationException( "could not serialize persistence context", ex );
}
finally {
if ( oos != null ) {
try {
oos.close();
}
catch( IOException ignore ) {
//ignore
}
}
}
return baos.toByteArray();
}
private void replaceActionQueue(ActionQueue actionQueueNew) {
if ( actionQueue.hasAnyQueuedActions() ) {
throw new IllegalStateException( "cannot replace an ActionQueue with queued actions " );
}
actionQueue.clear();
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream( new ByteArrayInputStream( serializeActionQueue( actionQueueNew ) ) );
actionQueue = ActionQueue.deserialize( ois, this );
}
catch (IOException ex) {
throw new SerializationException( "could not deserialize the action queue", ex );
}
catch (ClassNotFoundException ex) {
throw new SerializationException( "could not deserialize the action queue", ex );
}
finally {
try {
if (ois != null) ois.close();
}
catch (IOException ignore) {
}
}
}
private static byte[] serializeActionQueue(ActionQueue actionQueue) {
ByteArrayOutputStream baos = new ByteArrayOutputStream( 512 );
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream( baos );
actionQueue.serialize( oos );
}
catch (IOException ex) {
throw new SerializationException( "could not serialize action queue", ex );
}
finally {
if ( oos != null ) {
try {
oos.close();
}
catch( IOException ex ) {
//ignore
}
}
}
return baos.toByteArray();
}
@Override
public boolean shouldAutoClose() {
if ( isClosed() ) {

View File

@ -56,7 +56,6 @@ import org.hibernate.engine.query.spi.NativeSQLQueryPlan;
import org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.engine.spi.NonFlushedChanges;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.QueryParameters;
import org.hibernate.engine.spi.SessionEventListenerManager;
@ -759,16 +758,6 @@ public class StatelessSessionImpl extends AbstractSessionImpl implements Statele
public void flush() {
}
@Override
public NonFlushedChanges getNonFlushedChanges() {
throw new UnsupportedOperationException();
}
@Override
public void applyNonFlushedChanges(NonFlushedChanges nonFlushedChanges) {
throw new UnsupportedOperationException();
}
@Override
public String getFetchProfile() {
return null;

View File

@ -1,152 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.nonflushedchanges;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.ConnectionReleaseMode;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.internal.StatefulPersistenceContext;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.NonFlushedChanges;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory;
import org.hibernate.internal.util.SerializationHelper;
import org.hibernate.testing.jta.TestingJtaBootstrap;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
/**
* (adapted this from "ops" tests version)
*
* @author Gail Badner
* @author Steve Ebersole
*/
public abstract class AbstractOperationTestCase extends BaseCoreFunctionalTestCase {
private Map oldToNewEntityRefs = new HashMap();
public void configure(Configuration cfg) {
super.configure( cfg );
TestingJtaBootstrap.prepare( cfg.getProperties() );
cfg.setProperty( Environment.TRANSACTION_STRATEGY, CMTTransactionFactory.class.getName() );
cfg.setProperty( Environment.AUTO_CLOSE_SESSION, "true" );
cfg.setProperty( Environment.FLUSH_BEFORE_COMPLETION, "true" );
cfg.setProperty( Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.AFTER_STATEMENT.toString() );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
cfg.setProperty( Environment.STATEMENT_BATCH_SIZE, "0" );
}
public String[] getMappings() {
return new String[] {
"nonflushedchanges/Node.hbm.xml",
"nonflushedchanges/Employer.hbm.xml",
"nonflushedchanges/OptLockEntity.hbm.xml",
"nonflushedchanges/OneToOne.hbm.xml",
"nonflushedchanges/Competition.hbm.xml"
};
}
@Override
public String getCacheConcurrencyStrategy() {
return null;
}
protected void clearCounts() {
sessionFactory().getStatistics().clear();
}
protected void assertInsertCount(int expected) {
int inserts = ( int ) sessionFactory().getStatistics().getEntityInsertCount();
assertEquals( "unexpected insert count", expected, inserts );
}
protected void assertUpdateCount(int expected) {
int updates = ( int ) sessionFactory().getStatistics().getEntityUpdateCount();
assertEquals( "unexpected update counts", expected, updates );
}
protected void assertDeleteCount(int expected) {
int deletes = ( int ) sessionFactory().getStatistics().getEntityDeleteCount();
assertEquals( "unexpected delete counts", expected, deletes );
}
protected void assertFetchCount(int count) {
int fetches = ( int ) sessionFactory().getStatistics().getEntityFetchCount();
assertEquals( count, fetches );
}
@SuppressWarnings( {"unchecked"})
protected Session applyNonFlushedChangesToNewSessionCloseOldSession(Session oldSession) {
NonFlushedChanges nfc = ( ( SessionImplementor ) oldSession ).getNonFlushedChanges();
byte[] bytes = SerializationHelper.serialize( nfc );
NonFlushedChanges nfc2 = ( NonFlushedChanges ) SerializationHelper.deserialize( bytes );
Session newSession = openSession();
( ( SessionImplementor ) newSession ).applyNonFlushedChanges( nfc2 );
oldToNewEntityRefs.clear();
for ( Object o : ((SessionImplementor) oldSession).getPersistenceContext()
.getEntitiesByKey()
.entrySet() ) {
Map.Entry entry = (Map.Entry) o;
EntityKey entityKey = (EntityKey) entry.getKey();
Object oldEntityRef = entry.getValue();
oldToNewEntityRefs.put(
oldEntityRef,
((SessionImplementor) newSession).getPersistenceContext().getEntity( entityKey )
);
}
for ( Object o : ((StatefulPersistenceContext) ((SessionImplementor) oldSession).getPersistenceContext())
.getProxiesByKey()
.entrySet() ) {
Map.Entry entry = (Map.Entry) o;
EntityKey entityKey = (EntityKey) entry.getKey();
Object oldProxyRef = entry.getValue();
oldToNewEntityRefs.put(
oldProxyRef,
((SessionImplementor) newSession).getPersistenceContext().getProxy( entityKey )
);
}
oldSession.clear();
oldSession.close();
return newSession;
}
// protected void applyNonFlushedChangesToClearedSession(Session s) {
// NonFlushedChanges nfc = ( ( SessionImplementor ) s ).getNonFlushedChanges();
// byte[] bytes = SerializationHelper.serialize( nfc );
// NonFlushedChanges nfc2 = ( NonFlushedChanges ) SerializationHelper.deserialize( bytes );
// s.clear();
// ( ( SessionImplementor ) s ).applyNonFlushedChanges( nfc2 );
// }
@SuppressWarnings( {"unchecked"})
protected Map getOldToNewEntityRefMap() {
return Collections.unmodifiableMap( oldToNewEntityRefs );
}
}

View File

@ -1,66 +0,0 @@
package org.hibernate.test.nonflushedchanges;
import java.io.Serializable;
/**
* {@inheritDoc}
*
* @author Steve Ebersole, Gail Badner (adapted this from "ops" tests version)
*/
public class Address implements Serializable {
private Long id;
private String streetAddress;
private String city;
private String country;
private Person resident;
public Address() {
}
public Address(String streetAddress, String city, String country, Person resident) {
this.streetAddress = streetAddress;
this.city = city;
this.country = country;
this.resident = resident;
resident.setAddress( this );
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStreetAddress() {
return streetAddress;
}
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public Person getResident() {
return resident;
}
public void setResident(Person resident) {
this.resident = resident;
}
}

View File

@ -1,31 +0,0 @@
<?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.nonflushedchanges">
<class name="Competition">
<id name="id">
<generator class="native"/>
</id>
<list name="competitors" table="COMPET_ION_OR" cascade="persist,merge,delete">
<key column="TION_ID"/>
<list-index column="INDEX_COL"/>
<many-to-many class="Competitor" column="TOR_ID"/>
</list>
</class>
<class name="Competitor">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>

View File

@ -1,31 +0,0 @@
//$Id: $
package org.hibernate.test.nonflushedchanges;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* @author Emmanuel Bernard, Gail Badner (adapted this from "ops" tests version)
*/
public class Competition implements Serializable {
private Integer id;
private List competitors = new ArrayList();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List getCompetitors() {
return competitors;
}
public void setCompetitors(List competitors) {
this.competitors = competitors;
}
}

View File

@ -1,35 +0,0 @@
//$Id: $
package org.hibernate.test.nonflushedchanges;
import java.io.Serializable;
/**
* @author Emmanuel Bernard, Gail Badner (adapted this from "ops" tests version)
*/
public class Competitor implements Serializable {
public Integer id;
private String name;
public Competitor() {
}
public Competitor(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -1,293 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.nonflushedchanges;
import java.util.ArrayList;
import java.util.Collection;
import javax.transaction.RollbackException;
import org.junit.Assert;
import org.junit.Test;
import org.hibernate.Hibernate;
import org.hibernate.PersistentObjectException;
import org.hibernate.Session;
import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
/**
* @author Gavin King, Gail Badner (adapted this from "ops" tests version)
*/
public class CreateTest extends AbstractOperationTestCase {
@Test
@SuppressWarnings( {"unchecked"})
public void testNoUpdatesOnCreateVersionedWithCollection() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
VersionedEntity root = new VersionedEntity( "root", "root" );
VersionedEntity child = new VersionedEntity( "c1", "child-1" );
root.getChildren().add( child );
child.setParent( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
s.save( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( VersionedEntity ) getOldToNewEntityRefMap().get( root );
applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( VersionedEntity ) getOldToNewEntityRefMap().get( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 2 );
assertUpdateCount( 0 );
assertDeleteCount( 0 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.delete( root );
applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertUpdateCount( 0 );
assertDeleteCount( 2 );
}
@Test
public void testCreateTree() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Node root = new Node( "root" );
Node child = new Node( "child" );
root.addChild( child );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
s.persist( root );
applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 2 );
assertUpdateCount( 0 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
System.out.println( "getting" );
root = ( Node ) s.get( Node.class, "root" );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( Node ) getOldToNewEntityRefMap().get( root );
Node child2 = new Node( "child2" );
root.addChild( child2 );
System.out.println( "committing" );
applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 3 );
assertUpdateCount( 0 );
}
@Test
public void testCreateTreeWithGeneratedId() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
NumberedNode root = new NumberedNode( "root" );
NumberedNode child = new NumberedNode( "child" );
root.addChild( child );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
s.persist( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 2 );
assertUpdateCount( 0 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) s.get( NumberedNode.class, Long.valueOf( root.getId() ) );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
NumberedNode child2 = new NumberedNode( "child2" );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
root.addChild( child2 );
applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 3 );
assertUpdateCount( 0 );
}
@Test
public void testCreateException() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Node dupe = new Node( "dupe" );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
s.persist( dupe );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
dupe = ( Node ) getOldToNewEntityRefMap().get( dupe );
s.persist( dupe );
applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
s.persist( dupe );
applyNonFlushedChangesToNewSessionCloseOldSession( s );
try {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
Assert.fail();
}
catch ( ConstraintViolationException cve ) {
//verify that an exception is thrown!
}
catch ( RollbackException e ) {
if ( ! ConstraintViolationException.class.isInstance( e.getCause() ) ) {
throw (Exception) e.getCause();
}
}
if ( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ) {
// ugh! really!?!
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
}
Node nondupe = new Node( "nondupe" );
nondupe.addChild( dupe );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
s.persist( nondupe );
applyNonFlushedChangesToNewSessionCloseOldSession( s );
try {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
Assert.fail();
}
catch ( ConstraintViolationException cve ) {
//verify that an exception is thrown!
}
catch ( RollbackException e ) {
if ( ! ConstraintViolationException.class.isInstance( e.getCause() ) ) {
throw (Exception) e.getCause();
}
}
if ( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ) {
// ugh! really!?!
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
}
}
@Test
public void testCreateExceptionWithGeneratedId() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
NumberedNode dupe = new NumberedNode( "dupe" );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
s.persist( dupe );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
dupe = ( NumberedNode ) getOldToNewEntityRefMap().get( dupe );
s.persist( dupe );
applyNonFlushedChangesToNewSessionCloseOldSession( s );
dupe = ( NumberedNode ) getOldToNewEntityRefMap().get( dupe );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
try {
s.persist( dupe );
s.flush();
assertFalse( true );
}
catch ( PersistentObjectException poe ) {
//verify that an exception is thrown!
}
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
NumberedNode nondupe = new NumberedNode( "nondupe" );
nondupe.addChild( dupe );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
try {
s.persist( nondupe );
assertFalse( true );
}
catch ( PersistentObjectException poe ) {
//verify that an exception is thrown!
}
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
}
@Test
@SuppressWarnings( {"unchecked"})
public void testBasic() throws Exception {
Session s;
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
Employer er = new Employer();
Employee ee = new Employee();
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
s.persist( ee );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
ee = ( Employee ) getOldToNewEntityRefMap().get( ee );
Collection erColl = new ArrayList();
Collection eeColl = new ArrayList();
erColl.add( ee );
eeColl.add( er );
er.setEmployees( erColl );
ee.setEmployers( eeColl );
applyNonFlushedChangesToNewSessionCloseOldSession( s );
ee = ( Employee ) getOldToNewEntityRefMap().get( ee );
er = ( Employer ) ee.getEmployers().iterator().next();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
er = ( Employer ) s.load( Employer.class, er.getId() );
assertNotNull( er );
assertFalse( Hibernate.isInitialized( er ) );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
er = ( Employer ) getOldToNewEntityRefMap().get( er );
assertNotNull( er );
assertFalse( Hibernate.isInitialized( er ) );
assertNotNull( er.getEmployees() );
assertEquals( 1, er.getEmployees().size() );
Employee eeFromDb = ( Employee ) er.getEmployees().iterator().next();
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
eeFromDb = ( Employee ) getOldToNewEntityRefMap().get( eeFromDb );
assertEquals( ee.getId(), eeFromDb.getId() );
applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
}

View File

@ -1,114 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.nonflushedchanges;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
/**
* adapted this from "ops" tests version
*
* @author Gail Badner
* @author Steve Ebersole
*/
public class DeleteTest extends AbstractOperationTestCase {
@Test
@SuppressWarnings( {"unchecked"})
public void testDeleteVersionedWithCollectionNoUpdate() throws Exception {
// test adapted from HHH-1564...
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
VersionedEntity c = new VersionedEntity( "c1", "child-1" );
VersionedEntity p = new VersionedEntity( "root", "root" );
p.getChildren().add( c );
c.setParent( p );
s.save( p );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
VersionedEntity loadedParent = ( VersionedEntity ) s.get( VersionedEntity.class, "root" );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
loadedParent = ( VersionedEntity ) getOldToNewEntityRefMap().get( loadedParent );
s.delete( loadedParent );
applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 0 );
assertUpdateCount( 0 );
assertDeleteCount( 2 );
}
@Test
public void testNoUpdateOnDelete() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Node node = new Node( "test" );
s.persist( node );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
s.delete( node );
applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertUpdateCount( 0 );
assertInsertCount( 0 );
}
@Test
@SuppressWarnings( {"unchecked"})
public void testNoUpdateOnDeleteWithCollection() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Node parent = new Node( "parent" );
Node child = new Node( "child" );
parent.getCascadingChildren().add( child );
s.persist( parent );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
parent = ( Node ) s.get( Node.class, "parent" );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
parent = ( Node ) getOldToNewEntityRefMap().get( parent );
s.delete( parent );
applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertUpdateCount( 0 );
assertInsertCount( 0 );
assertDeleteCount( 2 );
}
}

View File

@ -1,34 +0,0 @@
//$Id: Employee.java 5686 2005-02-12 07:27:32Z steveebersole $
package org.hibernate.test.nonflushedchanges;
import java.io.Serializable;
import java.util.Collection;
/**
* Employee in an Employer-Employee relationship
*
* @author Emmanuel Bernard, Gail Badner (adapted this from "ops" tests version)
*/
public class Employee implements Serializable {
private Integer id;
private Collection employers;
public Integer getId() {
return id;
}
public void setId(Integer integer) {
id = integer;
}
public Collection getEmployers() {
return employers;
}
public void setEmployers(Collection employers) {
this.employers = employers;
}
}

View File

@ -1,39 +0,0 @@
<?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.nonflushedchanges">
<class name="Employer" polymorphism="explicit">
<id name="id">
<generator class="native"/>
</id>
<version column="vers" name="vers"/>
<bag name="employees"
cascade="persist,merge"
table="EMPLOYER_EMPLOYEE">
<key column="EMPER_ID"/>
<many-to-many class="Employee" column="EMPEE_ID"/>
</bag>
</class>
<class name="Employee" polymorphism="explicit">
<id name="id">
<generator class="native"/>
</id>
<bag name="employers"
inverse="true"
cascade="persist,merge,save-update"
table="EMPLOYER_EMPLOYEE">
<key column="EMPEE_ID"/>
<many-to-many class="Employer" column="EMPER_ID"/>
</bag>
</class>
</hibernate-mapping>

View File

@ -1,43 +0,0 @@
//$Id: Employer.java 8670 2005-11-25 17:36:29Z epbernard $
package org.hibernate.test.nonflushedchanges;
import java.io.Serializable;
import java.util.Collection;
/**
* Employer in a employer-Employee relationship
*
* @author Emmanuel Bernard, Gail Badner (adapted this from "ops" tests version)
*/
public class Employer implements Serializable {
private Integer id;
private Collection employees;
private Integer vers;
public Integer getVers() {
return vers;
}
public void setVers(Integer vers) {
this.vers = vers;
}
public Collection getEmployees() {
return employees;
}
public Integer getId() {
return id;
}
public void setEmployees(Collection set) {
employees = set;
}
public void setId(Integer integer) {
id = integer;
}
}

View File

@ -1,278 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.nonflushedchanges;
import java.util.List;
import org.junit.Test;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* adapted this from "ops" tests version
*
* @author Gail Badner
* @author Gavin King
*/
public class GetLoadTest extends AbstractOperationTestCase {
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
cfg.setProperty( Environment.STATEMENT_BATCH_SIZE, "0" );
}
@Test
@SuppressWarnings( {"UnusedAssignment"})
public void testGetLoad() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Employer emp = new Employer();
s.persist( emp );
Node node = new Node( "foo" );
Node parent = new Node( "bar" );
parent.addChild( node );
s.persist( parent );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
emp = ( Employer ) s.get( Employer.class, emp.getId() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
emp = ( Employer ) getOldToNewEntityRefMap().get( emp );
assertTrue( Hibernate.isInitialized( emp ) );
assertFalse( Hibernate.isInitialized( emp.getEmployees() ) );
node = ( Node ) s.get( Node.class, node.getName() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
node = ( Node ) getOldToNewEntityRefMap().get( node );
emp = ( Employer ) getOldToNewEntityRefMap().get( emp );
assertTrue( Hibernate.isInitialized( node ) );
assertFalse( Hibernate.isInitialized( node.getChildren() ) );
assertFalse( Hibernate.isInitialized( node.getParent() ) );
assertNull( s.get( Node.class, "xyz" ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
emp = ( Employer ) s.load( Employer.class, emp.getId() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
emp = ( Employer ) getOldToNewEntityRefMap().get( emp );
emp.getId();
assertFalse( Hibernate.isInitialized( emp ) );
node = ( Node ) s.load( Node.class, node.getName() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
node = ( Node ) getOldToNewEntityRefMap().get( node );
assertEquals( node.getName(), "foo" );
assertFalse( Hibernate.isInitialized( node ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
emp = ( Employer ) s.get( "org.hibernate.test.nonflushedchanges.Employer", emp.getId() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
emp = ( Employer ) getOldToNewEntityRefMap().get( emp );
assertTrue( Hibernate.isInitialized( emp ) );
node = ( Node ) s.get( "org.hibernate.test.nonflushedchanges.Node", node.getName() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
node = ( Node ) getOldToNewEntityRefMap().get( node );
assertTrue( Hibernate.isInitialized( node ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
emp = ( Employer ) s.load( "org.hibernate.test.nonflushedchanges.Employer", emp.getId() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
emp = ( Employer ) getOldToNewEntityRefMap().get( emp );
emp.getId();
assertFalse( Hibernate.isInitialized( emp ) );
node = ( Node ) s.load( "org.hibernate.test.nonflushedchanges.Node", node.getName() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
emp = ( Employer ) getOldToNewEntityRefMap().get( emp );
node = ( Node ) getOldToNewEntityRefMap().get( node );
assertEquals( node.getName(), "foo" );
assertFalse( Hibernate.isInitialized( node ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertFetchCount( 0 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.createQuery( "delete from Employer" ).executeUpdate();
List list = s.createQuery( "from Node" ).list();
for ( Object aList : list ) {
s.delete( aList );
}
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
public void testGetReadOnly() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Employer emp = new Employer();
s.persist( emp );
Node node = new Node( "foo" );
Node parent = new Node( "bar" );
parent.addChild( node );
s.persist( parent );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
assertFalse( s.isDefaultReadOnly() );
s.setDefaultReadOnly( true );
emp = ( Employer ) s.get( Employer.class, emp.getId() );
assertTrue( s.isDefaultReadOnly() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
assertTrue( s.isDefaultReadOnly() );
emp = ( Employer ) getOldToNewEntityRefMap().get( emp );
assertTrue( Hibernate.isInitialized( emp ) );
assertFalse( Hibernate.isInitialized( emp.getEmployees() ) );
node = ( Node ) s.get( Node.class, node.getName() );
assertTrue( s.isReadOnly( emp ) );
assertTrue( s.isReadOnly( node ) );
s.setDefaultReadOnly( false );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
assertFalse( s.isDefaultReadOnly() );
node = ( Node ) getOldToNewEntityRefMap().get( node );
emp = ( Employer ) getOldToNewEntityRefMap().get( emp );
assertTrue( Hibernate.isInitialized( node ) );
assertTrue( s.isReadOnly( node ) );
assertFalse( Hibernate.isInitialized( node.getParent() ) );
assertTrue( s.isReadOnly( emp ) );
assertFalse( Hibernate.isInitialized( node.getChildren() ) );
Hibernate.initialize( node.getChildren() );
for ( Object o : node.getChildren() ) {
assertFalse( s.isReadOnly( o ) );
}
assertFalse( Hibernate.isInitialized( node.getParent() ) );
assertNull( s.get( Node.class, "xyz" ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
assertFalse( s.isDefaultReadOnly() );
emp = ( Employer ) s.get( "org.hibernate.test.nonflushedchanges.Employer", emp.getId() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
assertFalse( s.isDefaultReadOnly() );
emp = ( Employer ) getOldToNewEntityRefMap().get( emp );
assertTrue( Hibernate.isInitialized( emp ) );
assertFalse( s.isReadOnly( emp ) );
s.setReadOnly( emp, true );
node = ( Node ) s.get( "org.hibernate.test.nonflushedchanges.Node", node.getName() );
assertFalse( s.isReadOnly( node ) );
s.setReadOnly( node, true );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
emp = ( Employer ) getOldToNewEntityRefMap().get( emp );
assertTrue( s.isReadOnly( emp ) );
node = ( Node ) getOldToNewEntityRefMap().get( node );
assertTrue( Hibernate.isInitialized( node ) );
assertTrue( s.isReadOnly( node ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertFetchCount( 0 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.createQuery( "delete from Employer" ).executeUpdate();
List list = s.createQuery( "from Node" ).list();
for ( Object aList : list ) {
s.delete( aList );
}
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
public void testLoadReadOnly() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Employer emp = new Employer();
s.persist( emp );
Node node = new Node( "foo" );
Node parent = new Node( "bar" );
parent.addChild( node );
s.persist( parent );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
assertFalse( s.isDefaultReadOnly() );
s.setDefaultReadOnly( true );
emp = ( Employer ) s.load( Employer.class, emp.getId() );
assertFalse( Hibernate.isInitialized( emp ) );
assertTrue( s.isReadOnly( emp ) );
assertTrue( s.isDefaultReadOnly() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
assertTrue( s.isDefaultReadOnly() );
emp = ( Employer ) getOldToNewEntityRefMap().get( emp );
assertFalse( Hibernate.isInitialized( emp ) );
assertTrue( s.isReadOnly( emp ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.createQuery( "delete from Employer" ).executeUpdate();
List list = s.createQuery( "from Node" ).list();
for ( Object aList : list ) {
s.delete( aList );
}
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
public void testGetAfterDelete() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Employer emp = new Employer();
s.persist( emp );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.delete( emp );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
emp = ( Employer ) s.get( Employee.class, emp.getId() );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertNull( "get did not return null after delete", emp );
}
}

View File

@ -1,880 +0,0 @@
//$Id: MergeTest.java 11037 2007-01-09 16:04:16Z steve.ebersole@jboss.com $
package org.hibernate.test.nonflushedchanges;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.junit.Test;
import org.hibernate.Hibernate;
import org.hibernate.NonUniqueObjectException;
import org.hibernate.Session;
import org.hibernate.StaleObjectStateException;
import org.hibernate.criterion.Projections;
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* adapted this from "ops" tests version
*
* @author Gail Badner
* @author Gavin King
*/
public class MergeTest extends AbstractOperationTestCase {
@Test
@SuppressWarnings( {"UnusedAssignment"})
public void testMergeStaleVersionFails() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
VersionedEntity entity = new VersionedEntity( "entity", "entity" );
s.persist( entity );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
// make the detached 'entity' reference stale...
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
VersionedEntity entity2 = ( VersionedEntity ) s.get( VersionedEntity.class, entity.getId() );
entity2.setName( "entity-name" );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
// now try to reattach it
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
try {
s.merge( entity );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
entity = ( VersionedEntity ) getOldToNewEntityRefMap().get( entity );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
fail( "was expecting staleness error" );
}
catch ( StaleObjectStateException expected ) {
// expected outcome...
}
finally {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
}
}
@Test
@SuppressWarnings( {"UnusedAssignment"})
public void testMergeBidiPrimayKeyOneToOne() throws Exception {
rebuildSessionFactory();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Person p = new Person( "steve" );
new PersonalDetails( "I have big feet", p );
s.persist( p );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
clearCounts();
p.getDetails().setSomePersonalDetail( p.getDetails().getSomePersonalDetail() + " and big hands too" );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
p = ( Person ) s.merge( p );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
p = ( Person ) getOldToNewEntityRefMap().get( p );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 0 );
assertUpdateCount( 1 );
assertDeleteCount( 0 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.delete( p );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
@SuppressWarnings( {"UnusedAssignment"})
public void testMergeBidiForeignKeyOneToOne() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Person p = new Person( "steve" );
Address a = new Address( "123 Main", "Austin", "US", p );
s.persist( a );
s.persist( p );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
clearCounts();
p.getAddress().setStreetAddress( "321 Main" );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
p = ( Person ) s.merge( p );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 0 );
assertUpdateCount( 0 ); // no cascade
assertDeleteCount( 0 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.delete( a );
s.delete( p );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
@SuppressWarnings( {"UnusedAssignment"})
public void testNoExtraUpdatesOnMerge() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Node node = new Node( "test" );
s.persist( node );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
clearCounts();
// node is now detached, but we have made no changes. so attempt to merge it
// into this new session; this should cause no updates...
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
node = ( Node ) s.merge( node );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertUpdateCount( 0 );
assertInsertCount( 0 );
///////////////////////////////////////////////////////////////////////
// as a control measure, now update the node while it is detached and
// make sure we get an update as a result...
node.setDescription( "new description" );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
node = ( Node ) s.merge( node );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertUpdateCount( 1 );
assertInsertCount( 0 );
///////////////////////////////////////////////////////////////////////
}
@Test
@SuppressWarnings( {"unchecked", "UnusedAssignment"})
public void testNoExtraUpdatesOnMergeWithCollection() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Node parent = new Node( "parent" );
Node child = new Node( "child" );
parent.getChildren().add( child );
child.setParent( parent );
s.persist( parent );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
clearCounts();
// parent is now detached, but we have made no changes. so attempt to merge it
// into this new session; this should cause no updates...
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
parent = ( Node ) s.merge( parent );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertUpdateCount( 0 );
assertInsertCount( 0 );
///////////////////////////////////////////////////////////////////////
// as a control measure, now update the node while it is detached and
// make sure we get an update as a result...
( ( Node ) parent.getChildren().iterator().next() ).setDescription( "child's new description" );
parent.addChild( new Node( "second child" ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
parent = ( Node ) s.merge( parent );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertUpdateCount( 1 );
assertInsertCount( 1 );
///////////////////////////////////////////////////////////////////////
}
@Test
@SuppressWarnings( {"UnusedAssignment"})
public void testNoExtraUpdatesOnMergeVersioned() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
VersionedEntity entity = new VersionedEntity( "entity", "entity" );
s.persist( entity );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
clearCounts();
// entity is now detached, but we have made no changes. so attempt to merge it
// into this new session; this should cause no updates...
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
VersionedEntity mergedEntity = ( VersionedEntity ) s.merge( entity );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
mergedEntity = ( VersionedEntity ) getOldToNewEntityRefMap().get( mergedEntity );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertUpdateCount( 0 );
assertInsertCount( 0 );
assertEquals( "unexpected version increment", entity.getVersion(), mergedEntity.getVersion() );
///////////////////////////////////////////////////////////////////////
// as a control measure, now update the node while it is detached and
// make sure we get an update as a result...
entity.setName( "new name" );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
entity = ( VersionedEntity ) s.merge( entity );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertUpdateCount( 1 );
assertInsertCount( 0 );
///////////////////////////////////////////////////////////////////////
}
@Test
@SuppressWarnings( {"unchecked", "UnusedAssignment"})
public void testNoExtraUpdatesOnMergeVersionedWithCollection() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
VersionedEntity parent = new VersionedEntity( "parent", "parent" );
VersionedEntity child = new VersionedEntity( "child", "child" );
parent.getChildren().add( child );
child.setParent( parent );
s.persist( parent );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
clearCounts();
// parent is now detached, but we have made no changes. so attempt to merge it
// into this new session; this should cause no updates...
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
VersionedEntity mergedParent = ( VersionedEntity ) s.merge( parent );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
mergedParent = ( VersionedEntity ) getOldToNewEntityRefMap().get( mergedParent );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertUpdateCount( 0 );
assertInsertCount( 0 );
assertEquals( "unexpected parent version increment", parent.getVersion(), mergedParent.getVersion() );
VersionedEntity mergedChild = ( VersionedEntity ) mergedParent.getChildren().iterator().next();
assertEquals( "unexpected child version increment", child.getVersion(), mergedChild.getVersion() );
///////////////////////////////////////////////////////////////////////
// as a control measure, now update the node while it is detached and
// make sure we get an update as a result...
mergedParent.setName( "new name" );
mergedParent.getChildren().add( new VersionedEntity( "child2", "new child" ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
parent = ( VersionedEntity ) s.merge( mergedParent );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
parent = ( VersionedEntity ) getOldToNewEntityRefMap().get( parent );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertUpdateCount( 1 );
assertInsertCount( 1 );
///////////////////////////////////////////////////////////////////////
}
@Test
@SuppressWarnings( {"unchecked", "UnusedAssignment"})
public void testNoExtraUpdatesOnPersistentMergeVersionedWithCollection() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
VersionedEntity parent = new VersionedEntity( "parent", "parent" );
VersionedEntity child = new VersionedEntity( "child", "child" );
parent.getChildren().add( child );
child.setParent( parent );
s.persist( parent );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
clearCounts();
// parent is now detached, but we have made no changes. so attempt to merge it
// into this new session; this should cause no updates...
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
// load parent so that merge will follow entityIsPersistent path
VersionedEntity persistentParent = ( VersionedEntity ) s.get( VersionedEntity.class, parent.getId() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
persistentParent = ( VersionedEntity ) getOldToNewEntityRefMap().get( persistentParent );
// load children
persistentParent.getChildren().iterator().next();
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
persistentParent = ( VersionedEntity ) getOldToNewEntityRefMap().get( persistentParent );
VersionedEntity mergedParent = ( VersionedEntity ) s.merge( persistentParent ); // <-- This merge leads to failure
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
mergedParent = ( VersionedEntity ) getOldToNewEntityRefMap().get( mergedParent );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertUpdateCount( 0 );
assertInsertCount( 0 );
assertEquals( "unexpected parent version increment", parent.getVersion(), mergedParent.getVersion() );
VersionedEntity mergedChild = ( VersionedEntity ) mergedParent.getChildren().iterator().next();
assertEquals( "unexpected child version increment", child.getVersion(), mergedChild.getVersion() );
///////////////////////////////////////////////////////////////////////
// as a control measure, now update the node once it is loaded and
// make sure we get an update as a result...
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
persistentParent = ( VersionedEntity ) s.get( VersionedEntity.class, parent.getId() );
persistentParent.setName( "new name" );
persistentParent.getChildren().add( new VersionedEntity( "child2", "new child" ) );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
persistentParent = ( VersionedEntity ) getOldToNewEntityRefMap().get( persistentParent );
persistentParent = ( VersionedEntity ) s.merge( persistentParent );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertUpdateCount( 1 );
assertInsertCount( 1 );
///////////////////////////////////////////////////////////////////////
//
}
@Test
public void testPersistThenMergeInSameTxnWithVersion() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
VersionedEntity entity = new VersionedEntity( "test", "test" );
s.persist( entity );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
s.merge( new VersionedEntity( "test", "test-2" ) );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
try {
// control operation...
s.saveOrUpdate( new VersionedEntity( "test", "test-3" ) );
fail( "saveOrUpdate() should fail here" );
}
catch ( NonUniqueObjectException expected ) {
// expected behavior
}
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
public void testPersistThenMergeInSameTxnWithTimestamp() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
TimestampedEntity entity = new TimestampedEntity( "test", "test" );
s.persist( entity );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
s.merge( new TimestampedEntity( "test", "test-2" ) );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
try {
// control operation...
s.saveOrUpdate( new TimestampedEntity( "test", "test-3" ) );
fail( "saveOrUpdate() should fail here" );
}
catch ( NonUniqueObjectException expected ) {
// expected behavior
}
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
@SuppressWarnings( {"UnusedAssignment"})
public void testMergeDeepTree() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Node root = new Node( "root" );
Node child = new Node( "child" );
Node grandchild = new Node( "grandchild" );
root.addChild( child );
child.addChild( grandchild );
s.merge( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 3 );
assertUpdateCount( 0 );
clearCounts();
grandchild.setDescription( "the grand child" );
Node grandchild2 = new Node( "grandchild2" );
child.addChild( grandchild2 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.merge( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 1 );
assertUpdateCount( 1 );
clearCounts();
Node child2 = new Node( "child2" );
Node grandchild3 = new Node( "grandchild3" );
child2.addChild( grandchild3 );
root.addChild( child2 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.merge( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 2 );
assertUpdateCount( 0 );
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.delete( grandchild );
s.delete( grandchild2 );
s.delete( grandchild3 );
s.delete( child );
s.delete( child2 );
s.delete( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
@SuppressWarnings( {"UnusedAssignment"})
public void testMergeDeepTreeWithGeneratedId() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
NumberedNode root = new NumberedNode( "root" );
NumberedNode child = new NumberedNode( "child" );
NumberedNode grandchild = new NumberedNode( "grandchild" );
root.addChild( child );
child.addChild( grandchild );
root = ( NumberedNode ) s.merge( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 3 );
assertUpdateCount( 0 );
clearCounts();
child = ( NumberedNode ) root.getChildren().iterator().next();
grandchild = ( NumberedNode ) child.getChildren().iterator().next();
grandchild.setDescription( "the grand child" );
NumberedNode grandchild2 = new NumberedNode( "grandchild2" );
child.addChild( grandchild2 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
root = ( NumberedNode ) s.merge( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 1 );
assertUpdateCount( 1 );
clearCounts();
sessionFactory().getCache().evictEntityRegion( NumberedNode.class );
NumberedNode child2 = new NumberedNode( "child2" );
NumberedNode grandchild3 = new NumberedNode( "grandchild3" );
child2.addChild( grandchild3 );
root.addChild( child2 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
root = ( NumberedNode ) s.merge( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 2 );
assertUpdateCount( 0 );
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.createQuery( "delete from NumberedNode where name like 'grand%'" ).executeUpdate();
s.createQuery( "delete from NumberedNode where name like 'child%'" ).executeUpdate();
s.createQuery( "delete from NumberedNode" ).executeUpdate();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
@SuppressWarnings( {"UnusedAssignment"})
public void testMergeTree() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Node root = new Node( "root" );
Node child = new Node( "child" );
root.addChild( child );
s.persist( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( Node ) getOldToNewEntityRefMap().get( root );
child = ( Node ) root.getChildren().iterator().next();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 2 );
clearCounts();
root.setDescription( "The root node" );
child.setDescription( "The child node" );
Node secondChild = new Node( "second child" );
root.addChild( secondChild );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.merge( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 1 );
assertUpdateCount( 2 );
}
@Test
@SuppressWarnings( {"UnusedAssignment"})
public void testMergeTreeWithGeneratedId() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
NumberedNode root = new NumberedNode( "root" );
NumberedNode child = new NumberedNode( "child" );
root.addChild( child );
s.persist( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
child = ( NumberedNode ) root.getChildren().iterator().next();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 2 );
clearCounts();
root.setDescription( "The root node" );
child.setDescription( "The child node" );
NumberedNode secondChild = new NumberedNode( "second child" );
root.addChild( secondChild );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.merge( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 1 );
assertUpdateCount( 2 );
}
@Test
public void testMergeManaged() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
NumberedNode root = new NumberedNode( "root" );
s.persist( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
NumberedNode child = new NumberedNode( "child" );
root = ( NumberedNode ) s.merge( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
root.addChild( child );
assertSame( root, s.merge( root ) );
Object mergedChild = root.getChildren().iterator().next();
assertNotSame( mergedChild, child );
assertTrue( s.contains( mergedChild ) );
assertFalse( s.contains( child ) );
assertEquals( root.getChildren().size(), 1 );
assertTrue( root.getChildren().contains( mergedChild ) );
//assertNotSame( mergedChild, s.merge(child) ); //yucky :(
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
mergedChild = root.getChildren().iterator().next();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 1 );
assertUpdateCount( 0 );
assertEquals( root.getChildren().size(), 1 );
assertTrue( root.getChildren().contains( mergedChild ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
assertEquals(
Long.valueOf( 2 ),
s.createCriteria( NumberedNode.class )
.setProjection( Projections.rowCount() )
.uniqueResult()
);
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
public void testMergeManagedUninitializedCollection() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
NumberedNode root = new NumberedNode( "root" );
root.addChild( new NumberedNode( "child" ) );
s.persist( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
clearCounts();
NumberedNode newRoot = new NumberedNode( "root" );
newRoot.setId( root.getId() );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
root = ( NumberedNode ) s.get( NumberedNode.class, root.getId() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
Set managedChildren = root.getChildren();
assertFalse( Hibernate.isInitialized( managedChildren ) );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
managedChildren = root.getChildren();
newRoot.setChildren( managedChildren );
assertSame( root, s.merge( newRoot ) );
assertSame( managedChildren, root.getChildren() );
assertFalse( Hibernate.isInitialized( managedChildren ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 0 );
assertUpdateCount( 0 );
assertDeleteCount( 0 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
assertEquals(
Long.valueOf( 2 ),
s.createCriteria( NumberedNode.class )
.setProjection( Projections.rowCount() )
.uniqueResult()
);
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
public void testMergeManagedInitializedCollection() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
NumberedNode root = new NumberedNode( "root" );
root.addChild( new NumberedNode( "child" ) );
s.persist( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
clearCounts();
NumberedNode newRoot = new NumberedNode( "root" );
newRoot.setId( root.getId() );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
root = ( NumberedNode ) s.get( NumberedNode.class, root.getId() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
Set managedChildren = root.getChildren();
Hibernate.initialize( managedChildren );
assertTrue( Hibernate.isInitialized( managedChildren ) );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
managedChildren = root.getChildren();
newRoot.setChildren( managedChildren );
assertSame( root, s.merge( newRoot ) );
assertSame( managedChildren, root.getChildren() );
assertTrue( Hibernate.isInitialized( managedChildren ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 0 );
assertUpdateCount( 0 );
assertDeleteCount( 0 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
assertEquals(
Long.valueOf( 2 ),
s.createCriteria( NumberedNode.class )
.setProjection( Projections.rowCount() )
.uniqueResult()
);
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
@SuppressWarnings( {"unchecked"})
public void testRecursiveMergeTransient() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Employer jboss = new Employer();
Employee gavin = new Employee();
jboss.setEmployees( new ArrayList() );
jboss.getEmployees().add( gavin );
s.merge( jboss );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
s.flush();
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
jboss = ( Employer ) s.createQuery( "from Employer e join fetch e.employees" ).uniqueResult();
assertTrue( Hibernate.isInitialized( jboss.getEmployees() ) );
assertEquals( 1, jboss.getEmployees().size() );
s.clear();
s.merge( jboss.getEmployees().iterator().next() );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
public void testDeleteAndMerge() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Employer jboss = new Employer();
s.persist( jboss );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
Employer otherJboss;
otherJboss = ( Employer ) s.get( Employer.class, jboss.getId() );
s.delete( otherJboss );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
jboss.setVers( Integer.valueOf( 1 ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.merge( jboss );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
@SuppressWarnings( {"unchecked", "UnusedAssignment"})
public void testMergeManyToManyWithCollectionDeference() throws Exception {
// setup base data...
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Competition competition = new Competition();
competition.getCompetitors().add( new Competitor( "Name" ) );
competition.getCompetitors().add( new Competitor() );
competition.getCompetitors().add( new Competitor() );
s.persist( competition );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
// the competition graph is now detached:
// 1) create a new List reference to represent the competitors
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
List newComp = new ArrayList();
Competitor originalCompetitor = ( Competitor ) competition.getCompetitors().get( 0 );
originalCompetitor.setName( "Name2" );
newComp.add( originalCompetitor );
newComp.add( new Competitor() );
// 2) set that new List reference unto the Competition reference
competition.setCompetitors( newComp );
// 3) attempt the merge
Competition competition2 = ( Competition ) s.merge( competition );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
Competition competition2copy = ( Competition ) getOldToNewEntityRefMap().get( competition2 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertFalse( competition == competition2 );
assertFalse( competition2 == competition2copy );
assertFalse( competition.getCompetitors() == competition2.getCompetitors() );
assertEquals( 2, competition2.getCompetitors().size() );
assertEquals( 2, competition2copy.getCompetitors().size() );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
competition = ( Competition ) s.get( Competition.class, competition.getId() );
assertEquals( 2, competition.getCompetitors().size() );
s.delete( competition );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@SuppressWarnings( {"unchecked"})
protected void cleanupTestData() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
s.createQuery( "delete from NumberedNode where parent is not null" ).executeUpdate();
s.createQuery( "delete from NumberedNode" ).executeUpdate();
s.createQuery( "delete from Node where parent is not null" ).executeUpdate();
s.createQuery( "delete from Node" ).executeUpdate();
s.createQuery( "delete from VersionedEntity where parent is not null" ).executeUpdate();
s.createQuery( "delete from VersionedEntity" ).executeUpdate();
s.createQuery( "delete from TimestampedEntity" ).executeUpdate();
s.createQuery( "delete from Competitor" ).executeUpdate();
s.createQuery( "delete from Competition" ).executeUpdate();
for ( Employer employer : (List<Employer>)s.createQuery( "from Employer" ).list() ) {
s.delete( employer );
}
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Override
protected boolean isCleanupTestDataRequired() {
return true;
}
}

View File

@ -1,51 +0,0 @@
<?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.nonflushedchanges">
<class name="Node" polymorphism="explicit">
<id name="name">
<generator class="assigned"/>
</id>
<property name="description"/>
<many-to-one name="parent"/>
<property name="created" not-null="true"/>
<set name="children"
inverse="true"
cascade="persist,merge,save-update,evict">
<key column="parent"/>
<one-to-many class="Node"/>
</set>
<set name="cascadingChildren" inverse="false" cascade="persist,merge,save-update,evict,delete">
<key column="CASC_PARENT"/>
<one-to-many class="Node"/>
</set>
</class>
<class name="NumberedNode" polymorphism="explicit">
<id name="id" unsaved-value="0">
<generator class="native"/>
</id>
<property name="name">
<column name="name" index="iname" not-null="true"/>
</property>
<property name="description"/>
<property name="created" not-null="true"
type="imm_date"/>
<many-to-one name="parent" class="NumberedNode"/>
<set name="children"
inverse="true"
cascade="persist,merge,save-update">
<key column="parent"/>
<one-to-many class="NumberedNode"/>
</set>
</class>
</hibernate-mapping>

View File

@ -1,88 +0,0 @@
//$Id: Node.java 10759 2006-11-08 00:00:53Z steve.ebersole@jboss.com $
package org.hibernate.test.nonflushedchanges;
import java.io.Serializable;
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;
/**
* @author Gavin King, Gail Badner (adapted this from "ops" tests version)
*/
public class Node implements Serializable {
private String name;
private String description;
private Date created;
private Node parent;
private Set children = new HashSet();
private Set cascadingChildren = new HashSet();
public Node() {
}
public Node(String name) {
this.name = name;
created = generateCurrentDate();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public Set getChildren() {
return children;
}
public void setChildren(Set children) {
this.children = children;
}
public Node addChild(Node child) {
children.add( child );
child.setParent( this );
return this;
}
public Set getCascadingChildren() {
return cascadingChildren;
}
public void setCascadingChildren(Set cascadingChildren) {
this.cascadingChildren = cascadingChildren;
}
private Date generateCurrentDate() {
// Note : done as java.sql.Date mainly to work around issue with
// MySQL and its lack of milli-second precision on its DATETIME
// and TIMESTAMP datatypes.
return new Date( new java.util.Date().getTime() );
}
}

View File

@ -1,82 +0,0 @@
//$Id: NumberedNode.java 7236 2005-06-20 03:19:34Z oneovthafew $
package org.hibernate.test.nonflushedchanges;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
/**
* @author Gavin King, Gail Badner (adapted this from "ops" tests version)
*/
public class NumberedNode implements Serializable {
private long id;
private String name;
private NumberedNode parent;
private Set children = new HashSet();
private String description;
private Date created;
public NumberedNode() {
super();
}
public NumberedNode(String name) {
this.name = name;
created = new Date();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Set getChildren() {
return children;
}
public void setChildren(Set children) {
this.children = children;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public NumberedNode getParent() {
return parent;
}
public void setParent(NumberedNode parent) {
this.parent = parent;
}
public NumberedNode addChild(NumberedNode child) {
children.add( child );
child.setParent( this );
return this;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
}

View File

@ -1,43 +0,0 @@
<?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">
<!--
Mappings demonstrating bidirectional one-to-one mappings for testing
with various operations.
Person -> Address is modeled as a bidirectional one to one based on FK.
Person -> Details is modeled as a bidirectional one to one based on PK.
-->
<hibernate-mapping package="org.hibernate.test.nonflushedchanges">
<class name="Person" table="OPS_PERSON">
<id name="id" column="ID" type="long">
<generator class="increment"/>
</id>
<property name="name" column="NAME" type="string"/>
<one-to-one name="address" class="Address" property-ref="resident"/>
<one-to-one name="details" class="PersonalDetails" cascade="all"/>
</class>
<class name="Address" table="OPS_ADDRESS">
<id name="id" column="ID" type="long">
<generator class="increment"/>
</id>
<property name="streetAddress" column="STREET" type="string"/>
<property name="city" column="CITY" type="string"/>
<property name="country" column="CTRY" type="string"/>
<many-to-one name="resident" column="RESIDENT" class="Person"/>
</class>
<class name="PersonalDetails" table="OPS_PERS_DETAIL">
<id name="id" column="ID" type="long">
<generator class="increment"/>
</id>
<property name="somePersonalDetail" column="SOME_DETAIL" type="string"/>
<one-to-one name="person" class="Person" cascade="none" constrained="true"/>
</class>
</hibernate-mapping>

View File

@ -1,36 +0,0 @@
<?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.nonflushedchanges">
<class name="VersionedEntity" table="V_ENTITY">
<id name="id" column="ID" type="string">
<generator class="assigned"/>
</id>
<version name="version" column="VERS" type="long"/>
<property name="name" column="NAME" type="string"/>
<many-to-one name="parent" class="VersionedEntity"/>
<set name="children"
inverse="true"
cascade="persist,merge,save-update,evict,delete">
<key column="parent"/>
<one-to-many class="VersionedEntity"/>
</set>
</class>
<class name="TimestampedEntity" table="T_ENTITY">
<id name="id" column="ID" type="string">
<generator class="assigned"/>
</id>
<timestamp name="timestamp" column="TS"/>
<property name="name" column="NAME" type="string"/>
</class>
</hibernate-mapping>

View File

@ -1,53 +0,0 @@
package org.hibernate.test.nonflushedchanges;
import java.io.Serializable;
/**
* {@inheritDoc}
*
* @author Steve Ebersole, Gail Badner (adapted this from "ops" tests version)
*/
public class Person implements Serializable {
private Long id;
private String name;
private Address address;
private PersonalDetails details;
public Person() {
}
public Person(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public PersonalDetails getDetails() {
return details;
}
public void setDetails(PersonalDetails details) {
this.details = details;
}
}

View File

@ -1,46 +0,0 @@
package org.hibernate.test.nonflushedchanges;
import java.io.Serializable;
/**
* {@inheritDoc}
*
* @author Steve Ebersole, Gail Badner (adapted this from "ops" tests version)
*/
public class PersonalDetails implements Serializable {
private Long id;
private String somePersonalDetail;
private Person person;
public PersonalDetails() {
}
public PersonalDetails(String somePersonalDetail, Person person) {
this.somePersonalDetail = somePersonalDetail;
this.person = person;
person.setDetails( this );
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSomePersonalDetail() {
return somePersonalDetail;
}
public void setSomePersonalDetail(String somePersonalDetail) {
this.somePersonalDetail = somePersonalDetail;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}

View File

@ -1,545 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.nonflushedchanges;
import org.junit.Test;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.bytecode.instrumentation.internal.FieldInterceptionHelper;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Projections;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* adapted this from "ops" tests version
*
* @author Gail Badner
* @author Gavin King
*/
public class SaveOrUpdateTest extends AbstractOperationTestCase {
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
cfg.setProperty( Environment.STATEMENT_BATCH_SIZE, "0" );
}
public String[] getMappings() {
return new String[] { "nonflushedchanges/Node.hbm.xml" };
}
@Test
@SuppressWarnings( {"UnusedAssignment"})
public void testSaveOrUpdateDeepTree() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Node root = new Node( "root" );
Node child = new Node( "child" );
Node grandchild = new Node( "grandchild" );
root.addChild( child );
child.addChild( grandchild );
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( Node ) getOldToNewEntityRefMap().get( root );
child = ( Node ) getOldToNewEntityRefMap().get( child );
grandchild = ( Node ) getOldToNewEntityRefMap().get( grandchild );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 3 );
assertUpdateCount( 0 );
clearCounts();
grandchild.setDescription( "the grand child" );
Node grandchild2 = new Node( "grandchild2" );
child.addChild( grandchild2 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( Node ) getOldToNewEntityRefMap().get( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 1 );
assertUpdateCount( 1 );
clearCounts();
Node child2 = new Node( "child2" );
Node grandchild3 = new Node( "grandchild3" );
child2.addChild( grandchild3 );
root.addChild( child2 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 2 );
assertUpdateCount( 0 );
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.delete( grandchild );
s.delete( grandchild2 );
s.delete( grandchild3 );
s.delete( child );
s.delete( child2 );
s.delete( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
@SuppressWarnings( {"UnusedAssignment"})
public void testSaveOrUpdateDeepTreeWithGeneratedId() throws Exception {
boolean instrumented = FieldInterceptionHelper.isInstrumented( new NumberedNode() );
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
NumberedNode root = new NumberedNode( "root" );
NumberedNode child = new NumberedNode( "child" );
NumberedNode grandchild = new NumberedNode( "grandchild" );
root.addChild( child );
child.addChild( grandchild );
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
child = ( NumberedNode ) getOldToNewEntityRefMap().get( child );
grandchild = ( NumberedNode ) getOldToNewEntityRefMap().get( grandchild );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 3 );
assertUpdateCount( 0 );
clearCounts();
child = ( NumberedNode ) root.getChildren().iterator().next();
grandchild = ( NumberedNode ) child.getChildren().iterator().next();
grandchild.setDescription( "the grand child" );
NumberedNode grandchild2 = new NumberedNode( "grandchild2" );
child.addChild( grandchild2 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 1 );
assertUpdateCount( instrumented ? 1 : 3 );
clearCounts();
NumberedNode child2 = new NumberedNode( "child2" );
NumberedNode grandchild3 = new NumberedNode( "grandchild3" );
child2.addChild( grandchild3 );
root.addChild( child2 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 2 );
assertUpdateCount( instrumented ? 0 : 4 );
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.createQuery( "delete from NumberedNode where name like 'grand%'" ).executeUpdate();
s.createQuery( "delete from NumberedNode where name like 'child%'" ).executeUpdate();
s.createQuery( "delete from NumberedNode" ).executeUpdate();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
@SuppressWarnings( {"UnusedAssignment"})
public void testSaveOrUpdateTree() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Node root = new Node( "root" );
Node child = new Node( "child" );
root.addChild( child );
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( Node ) getOldToNewEntityRefMap().get( root );
child = ( Node ) getOldToNewEntityRefMap().get( child );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 2 );
clearCounts();
root.setDescription( "The root node" );
child.setDescription( "The child node" );
Node secondChild = new Node( "second child" );
root.addChild( secondChild );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 1 );
assertUpdateCount( 2 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.createQuery( "delete from Node where parent is not null" ).executeUpdate();
s.createQuery( "delete from Node" ).executeUpdate();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
@SuppressWarnings( {"UnusedAssignment"})
public void testSaveOrUpdateTreeWithGeneratedId() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
NumberedNode root = new NumberedNode( "root" );
NumberedNode child = new NumberedNode( "child" );
root.addChild( child );
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
child = ( NumberedNode ) getOldToNewEntityRefMap().get( child );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 2 );
clearCounts();
root.setDescription( "The root node" );
child.setDescription( "The child node" );
NumberedNode secondChild = new NumberedNode( "second child" );
root.addChild( secondChild );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 1 );
assertUpdateCount( 2 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.createQuery( "delete from NumberedNode where parent is not null" ).executeUpdate();
s.createQuery( "delete from NumberedNode" ).executeUpdate();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
public void testSaveOrUpdateManaged() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
NumberedNode root = new NumberedNode( "root" );
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
root = ( NumberedNode ) s.get( NumberedNode.class, root.getId() );
NumberedNode child = new NumberedNode( "child" );
root.addChild( child );
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
assertNull( getOldToNewEntityRefMap().get( child ) );
s.flush();
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
child = ( NumberedNode ) getOldToNewEntityRefMap().get( child );
child = ( NumberedNode ) root.getChildren().iterator().next();
assertTrue( s.contains( child ) );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
child = ( NumberedNode ) getOldToNewEntityRefMap().get( child );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertTrue( root.getChildren().contains( child ) );
assertEquals( root.getChildren().size(), 1 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
assertEquals(
Long.valueOf( 2 ),
s.createCriteria( NumberedNode.class )
.setProjection( Projections.rowCount() )
.uniqueResult()
);
s.delete( root );
s.delete( child );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
public void testSaveOrUpdateGot() throws Exception {
boolean instrumented = FieldInterceptionHelper.isInstrumented( new NumberedNode() );
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
NumberedNode root = new NumberedNode( "root" );
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 1 );
assertUpdateCount( 0 );
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 0 );
assertUpdateCount( instrumented ? 0 : 1 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
root = ( NumberedNode ) s.get( NumberedNode.class, Long.valueOf( root.getId() ) );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
Hibernate.initialize( root.getChildren() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
NumberedNode child = new NumberedNode( "child" );
root.addChild( child );
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( NumberedNode ) getOldToNewEntityRefMap().get( root );
assertTrue( Hibernate.isInitialized( root.getChildren() ) );
child = ( NumberedNode ) root.getChildren().iterator().next();
assertTrue( s.contains( child ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 1 );
assertUpdateCount( instrumented ? 0 : 1 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
assertEquals(
s.createCriteria( NumberedNode.class )
.setProjection( Projections.rowCount() )
.uniqueResult(),
new Long( 2 )
);
s.delete( root );
s.delete( child );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
public void testSaveOrUpdateGotWithMutableProp() throws Exception {
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Node root = new Node( "root" );
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( Node ) getOldToNewEntityRefMap().get( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 1 );
assertUpdateCount( 0 );
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( Node ) getOldToNewEntityRefMap().get( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 0 );
assertUpdateCount( 0 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
root = ( Node ) s.get( Node.class, "root" );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( Node ) getOldToNewEntityRefMap().get( root );
Hibernate.initialize( root.getChildren() );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( Node ) getOldToNewEntityRefMap().get( root );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
clearCounts();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
Node child = new Node( "child" );
root.addChild( child );
s.saveOrUpdate( root );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( Node ) getOldToNewEntityRefMap().get( root );
child = ( Node ) root.getChildren().iterator().next();
assertTrue( s.contains( child ) );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
root = ( Node ) getOldToNewEntityRefMap().get( root );
child = ( Node ) getOldToNewEntityRefMap().get( child );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
assertInsertCount( 1 );
//assertUpdateCount( 1 ); //note: will fail here if no second-level cache
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
assertEquals(
Long.valueOf( 2 ),
s.createCriteria( Node.class )
.setProjection( Projections.rowCount() )
.uniqueResult()
);
s.delete( root );
s.delete( child );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
@SuppressWarnings( {"UnusedAssignment"})
public void testEvictThenSaveOrUpdate() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s = openSession();
Node parent = new Node( "1:parent" );
Node child = new Node( "2:child" );
Node grandchild = new Node( "3:grandchild" );
parent.addChild( child );
child.addChild( grandchild );
s.saveOrUpdate( parent );
s = applyNonFlushedChangesToNewSessionCloseOldSession( s );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s1 = openSession();
child = ( Node ) s1.load( Node.class, "2:child" );
s1 = applyNonFlushedChangesToNewSessionCloseOldSession( s1 );
child = ( Node ) getOldToNewEntityRefMap().get( child );
assertTrue( s1.contains( child ) );
assertFalse( Hibernate.isInitialized( child ) );
assertTrue( s1.contains( child.getParent() ) );
assertTrue( Hibernate.isInitialized( child ) );
assertFalse( Hibernate.isInitialized( child.getChildren() ) );
assertFalse( Hibernate.isInitialized( child.getParent() ) );
assertTrue( s1.contains( child ) );
s1 = applyNonFlushedChangesToNewSessionCloseOldSession( s1 );
// child is an initialized proxy; after serialization, it is
// the proxy is replaced by its implementation
// TODO: find out if this is how this should work...
child = ( Node ) getOldToNewEntityRefMap().get(
( ( HibernateProxy ) child ).getHibernateLazyInitializer().getImplementation()
);
s1.evict( child );
assertFalse( s1.contains( child ) );
assertTrue( s1.contains( child.getParent() ) );
javax.transaction.Transaction tx1 = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().suspend();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
Session s2 = openSession();
try {
s2.getTransaction().begin();
s2.saveOrUpdate( child );
fail();
}
catch ( HibernateException ex ) {
// expected because parent is connected to s1
}
finally {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
}
s1.evict( child.getParent() );
assertFalse( s1.contains( child.getParent() ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s2 = openSession();
s2.saveOrUpdate( child );
s2 = applyNonFlushedChangesToNewSessionCloseOldSession( s2 );
child = ( Node ) getOldToNewEntityRefMap().get( child );
assertTrue( s2.contains( child ) );
assertFalse( s1.contains( child ) );
assertTrue( s2.contains( child.getParent() ) );
assertFalse( s1.contains( child.getParent() ) );
assertFalse( Hibernate.isInitialized( child.getChildren() ) );
assertFalse( Hibernate.isInitialized( child.getParent() ) );
assertEquals( 1, child.getChildren().size() );
assertEquals( "1:parent", child.getParent().getName() );
assertTrue( Hibernate.isInitialized( child.getChildren() ) );
assertFalse( Hibernate.isInitialized( child.getParent() ) );
assertNull( child.getParent().getDescription() );
assertTrue( Hibernate.isInitialized( child.getParent() ) );
s1 = applyNonFlushedChangesToNewSessionCloseOldSession( s1 );
s2 = applyNonFlushedChangesToNewSessionCloseOldSession( s2 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx1 );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
// tx1.commit();
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
s = openSession();
s.delete( s.get( Node.class, "3:grandchild" ) );
s.delete( s.get( Node.class, "2:child" ) );
s.delete( s.get( Node.class, "1:parent" ) );
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
}

View File

@ -1,47 +0,0 @@
package org.hibernate.test.nonflushedchanges;
import java.io.Serializable;
import java.util.Date;
/**
* todo: describe TimestampedEntity
*
* @author Steve Ebersole, Gail Badner (adapted this from "ops" tests version)
*/
public class TimestampedEntity implements Serializable {
private String id;
private String name;
private Date timestamp;
public TimestampedEntity() {
}
public TimestampedEntity(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
}

View File

@ -1,66 +0,0 @@
package org.hibernate.test.nonflushedchanges;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
/**
* VersionedEntity
*
* @author Steve Ebersole, Gail Badner (adapted this from "ops" tests version)
*/
public class VersionedEntity implements Serializable {
private String id;
private String name;
private long version;
private VersionedEntity parent;
private Set children = new HashSet();
public VersionedEntity() {
}
public VersionedEntity(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
public VersionedEntity getParent() {
return parent;
}
public void setParent(VersionedEntity parent) {
this.parent = parent;
}
public Set getChildren() {
return children;
}
public void setChildren(Set children) {
this.children = children;
}
}