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

Conflicts:
	hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java
This commit is contained in:
Brett Meyer 2014-03-24 13:26:48 -04:00
parent ad69571543
commit 3127bb8b18
4 changed files with 32 additions and 18 deletions

View File

@ -1635,12 +1635,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)
throws HibernateException;
protected abstract void doProcessQueuedOps(PersistentCollection collection, Serializable key,
int nextIndex, SessionImplementor session) throws HibernateException;
public CollectionMetadata getCollectionMetadata() {
return this;

View File

@ -154,7 +154,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

@ -181,34 +181,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();
@ -237,9 +236,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 ) {
@ -251,7 +250,6 @@ public class OneToManyPersister extends AbstractCollectionPersister {
else {
expectation.verifyOutcome( session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().executeUpdate( st ), st, -1 );
}
count++;
}
catch ( SQLException sqle ) {
if ( useBatch ) {
@ -266,7 +264,7 @@ public class OneToManyPersister extends AbstractCollectionPersister {
}
}
i++;
nextIndex++;
}
}
}

View File

@ -404,8 +404,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() );
}