Skip to content

Commit

Permalink
Fix some code
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 committed Feb 27, 2019
1 parent a2934b0 commit 870b5b5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
Expand Up @@ -7,7 +7,9 @@
package org.hibernate.action.internal;

import org.hibernate.HibernateException;
import org.hibernate.collection.internal.AbstractPersistentCollection;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.model.domain.spi.PersistentCollectionDescriptor;

Expand Down Expand Up @@ -39,16 +41,16 @@ public QueuedOperationCollectionAction(
public void execute() throws HibernateException {
getPersistentCollectionDescriptor().processQueuedOps( getCollection(), getKey(), getSession() );

// // TODO: It would be nice if this could be done safely by CollectionPersister#processQueuedOps;
// // Can't change the SPI to do this though.
// ((AbstractPersistentCollection) getCollection() ).clearOperationQueue();
//
// // The other CollectionAction types call CollectionEntry#afterAction, which
// // clears the dirty flag. We don't want to call CollectionEntry#afterAction unless
// // there is no other CollectionAction that will be executed on the same collection.
// final CollectionEntry ce = getSession().getPersistenceContext().getCollectionEntry( getCollection() );
// if ( !ce.isDoremove() && !ce.isDoupdate() && !ce.isDorecreate() ) {
// ce.afterAction( getCollection() );
// }
// TODO: It would be nice if this could be done safely by CollectionPersister#processQueuedOps;
// Can't change the SPI to do this though.
((AbstractPersistentCollection) getCollection() ).clearOperationQueue();

// The other CollectionAction types call CollectionEntry#afterAction, which
// clears the dirty flag. We don't want to call CollectionEntry#afterAction unless
// there is no other CollectionAction that will be executed on the same collection.
final CollectionEntry ce = getSession().getPersistenceContext().getCollectionEntry( getCollection() );
if ( !ce.isDoremove() && !ce.isDoupdate() && !ce.isDorecreate() ) {
ce.afterAction( getCollection() );
}
}
}
Expand Up @@ -18,6 +18,7 @@
import javax.persistence.OneToMany;

import org.hibernate.Hibernate;
import org.hibernate.Transaction;
import org.hibernate.collection.internal.AbstractPersistentCollection;

import org.hibernate.testing.TestForIssue;
Expand Down Expand Up @@ -167,15 +168,26 @@ public void testMergeInitializedBagAndRemerge() {
);

// Merge detached Parent with initialized children
Parent mergedParent = inTransaction( session -> {
Parent p = (Parent) session.merge( savedParent );
// after merging, p#children will be uninitialized
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
assertTrue( ( (AbstractPersistentCollection) p.getChildren() ).hasQueuedOperations() );
return p;

} );
assertFalse( ( (AbstractPersistentCollection) mergedParent.getChildren() ).hasQueuedOperations() );
Parent mergedParent = inSession(
session -> {
Transaction transaction = session.beginTransaction();
Parent p;
try {
p = (Parent) session.merge( savedParent );
// after merging, p#children will be uninitialized
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
assertTrue( ( (AbstractPersistentCollection) p.getChildren() ).hasQueuedOperations() );
transaction.commit();
}catch (Exception e){
if(transaction.isActive()){
transaction.rollback();
}
throw e;
}
assertFalse( ( (AbstractPersistentCollection) p.getChildren() ).hasQueuedOperations() );
return p;
}
);

// Merge detached Parent, now with uninitialized children no queued operations
inTransaction(
Expand Down
Expand Up @@ -164,7 +164,9 @@ public void testRootEntityManyToOneAttributeReference() {
@Test
public void testJoinedSubclassRoot() {
inSession(
session -> session.createQuery( "select p from Payment p" ).list()
session -> {
session.createQuery( "select p from Payment p" ).list();
}
);
}

Expand Down
Expand Up @@ -200,6 +200,10 @@ protected <R> R inTransaction(Function<SessionImplementor, R> action) {
return sessionFactoryScope().inTransaction( action );
}

protected <R> R inSession(Function<SessionImplementor, R> action) {
return sessionFactoryScope.inSession( action );
}

protected void inSession(Consumer<SessionImplementor> action) {
sessionFactoryScope().inSession( action );
}
Expand Down
Expand Up @@ -70,6 +70,24 @@ public void inTransaction(Consumer<SessionImplementor> action) {
inTransaction( getSessionFactory(), action );
}

protected <R> R inSession(Function<SessionImplementor, R> action) {
return inSession( getSessionFactory(), action );
}

private <R> R inSession(SessionFactoryImplementor sfi, Function<SessionImplementor, R> action) {
log.trace( "##inSession(SF,action)" );

try (SessionImplementor session = (SessionImplementor) sfi.openSession()) {
log.trace( "Session opened, calling action" );
R result = action.apply( session );
log.trace( "called action" );
return result;
}
finally {
log.trace( "Session close - auto-close lock" );
}
}

private void inSession(SessionFactoryImplementor sfi, Consumer<SessionImplementor> action) {
log.trace( "##inSession(SF,action)" );

Expand Down

0 comments on commit 870b5b5

Please sign in to comment.