HHH-9078 correct OrderColumn indexes for inverse, extra lazy collections

This commit is contained in:
Brett Meyer 2014-03-24 13:24:35 -04:00
parent ea2a7d50d1
commit 87d04ef3ed
4 changed files with 32 additions and 17 deletions

View File

@ -1759,11 +1759,13 @@ public abstract class AbstractCollectionPersister
public void processQueuedOps(PersistentCollection collection, Serializable key, SessionImplementor session)
throws HibernateException {
if ( collection.hasQueuedOperations() ) {
doProcessQueuedOps( collection, key, session );
int nextIndex = getSize( key, session );
doProcessQueuedOps( collection, key, nextIndex, session );
}
}
protected abstract void doProcessQueuedOps(PersistentCollection collection, Serializable key, SessionImplementor session)
protected abstract void doProcessQueuedOps(PersistentCollection collection, Serializable key,
int nextIndex, SessionImplementor session)
throws HibernateException;
@Override

View File

@ -158,7 +158,8 @@ public class BasicCollectionPersister extends AbstractCollectionPersister {
}
@Override
protected void doProcessQueuedOps(PersistentCollection collection, Serializable id, SessionImplementor session)
protected void doProcessQueuedOps(PersistentCollection collection, Serializable id,
int nextIndex, SessionImplementor session)
throws HibernateException {
// nothing to do
}

View File

@ -197,34 +197,33 @@ public class OneToManyPersister extends AbstractCollectionPersister {
public void recreate(PersistentCollection collection, Serializable id, SessionImplementor session)
throws HibernateException {
super.recreate( collection, id, session );
writeIndex( collection, collection.entries( this ), id, session );
writeIndex( collection, collection.entries( this ), id, 0, session );
}
@Override
public void insertRows(PersistentCollection collection, Serializable id, SessionImplementor session)
throws HibernateException {
super.insertRows( collection, id, session );
writeIndex( collection, collection.entries( this ), id, session );
writeIndex( collection, collection.entries( this ), id, 0, session );
}
@Override
protected void doProcessQueuedOps(PersistentCollection collection, Serializable id, SessionImplementor session)
throws HibernateException {
writeIndex( collection, collection.queuedAdditionIterator(), id, session );
protected void doProcessQueuedOps(PersistentCollection collection, Serializable id,
int nextIndex, SessionImplementor session) throws HibernateException {
writeIndex( collection, collection.queuedAdditionIterator(), id, nextIndex, session );
}
private void writeIndex(PersistentCollection collection, Iterator entries, Serializable id, SessionImplementor session) {
private void writeIndex(PersistentCollection collection, Iterator entries, Serializable id,
int nextIndex, SessionImplementor session) {
// If one-to-many and inverse, still need to create the index. See HHH-5732.
if ( isInverse && hasIndex && !indexContainsFormula ) {
try {
if ( entries.hasNext() ) {
Expectation expectation = Expectations.appropriateExpectation( getUpdateCheckStyle() );
int i = 0;
int count = 0;
while ( entries.hasNext() ) {
final Object entry = entries.next();
if ( entry != null && collection.entryExists( entry, i ) ) {
if ( entry != null && collection.entryExists( entry, nextIndex ) ) {
int offset = 1;
PreparedStatement st = null;
boolean callable = isUpdateCallable();
@ -253,9 +252,9 @@ public class OneToManyPersister extends AbstractCollectionPersister {
try {
offset += expectation.prepare( st );
if ( hasIdentifier ) {
offset = writeIdentifier( st, collection.getIdentifier( entry, i ), offset, session );
offset = writeIdentifier( st, collection.getIdentifier( entry, nextIndex ), offset, session );
}
offset = writeIndex( st, collection.getIndex( entry, i, this ), offset, session );
offset = writeIndex( st, collection.getIndex( entry, nextIndex, this ), offset, session );
offset = writeElement( st, collection.getElement( entry ), offset, session );
if ( useBatch ) {
@ -267,7 +266,6 @@ public class OneToManyPersister extends AbstractCollectionPersister {
else {
expectation.verifyOutcome( session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().executeUpdate( st ), st, -1 );
}
count++;
}
catch ( SQLException sqle ) {
if ( useBatch ) {
@ -282,7 +280,7 @@ public class OneToManyPersister extends AbstractCollectionPersister {
}
}
i++;
nextIndex++;
}
}
}

View File

@ -406,8 +406,22 @@ public class OrderByTest extends BaseCoreFunctionalTestCase {
forum = (Forum) s.get( Forum.class, forum.getId() );
assertEquals( 1, forum.getPosts().size() );
final Post post2 = new Post();
post2.setName( "post2" );
post2.setForum( forum );
forum.getPosts().add( post2 );
forum = (Forum) s.merge( forum );
s.flush();
s.clear();
sessionFactory().getCache().evictEntityRegions();
forum = (Forum) s.get( Forum.class, forum.getId() );
assertEquals( 2, forum.getPosts().size() );
assertEquals( "post1", forum.getPosts().get( 0 ).getName() );
assertEquals( "post2", forum.getPosts().get( 1 ).getName() );
assertEquals( 1, forum.getUsers().size() );
assertEquals( "john", forum.getUsers().get( 0 ).getName() );
}