HH-13720: Implement mapping model support for plural attributes - sorted set

This commit is contained in:
Steve Ebersole 2019-11-27 07:22:49 -06:00
parent 6a403fa5d9
commit 7b489b180c
9 changed files with 111 additions and 25 deletions

View File

@ -59,10 +59,12 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
private transient SharedSessionContractImplementor session;
private boolean isTempSession = false;
private boolean initialized;
private transient boolean initializing;
private transient List<DelayedOperation> operationQueue;
private transient boolean directlyAccessible;
private transient boolean initializing;
private Object owner;
private int cachedSize = -1;
@ -615,7 +617,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
this.initialized = true;
}
protected boolean isInitializing() {
@Override
public boolean isInitializing() {
return initializing;
}

View File

@ -342,6 +342,11 @@ public interface PersistentCollection {
*/
boolean isWrapper(Object collection);
/**
* Is this PersistentCollection in the process of being initialized?
*/
boolean isInitializing();
/**
* Is this instance initialized?
*

View File

@ -300,13 +300,16 @@ public abstract class AbstractImmediateCollectionInitializer extends AbstractCol
// the LoadingCollectionEntry won't finalize this for us without at least one row.
final PersistenceContext persistenceContext = context.getSession().getPersistenceContext();
final PersistentCollection collection = persistenceContext.getCollection( collectionKey );
collection.beforeInitialize( getCollectionAttributeMapping().getCollectionDescriptor(), 0 );
collection.beginRead();
collection.endRead();
final CollectionEntry entry = persistenceContext.getCollectionEntry( collection );
if ( entry != null ) {
entry.postInitialize( collection );
if ( ! collection.isInitializing() ) {
collection.beforeInitialize( getCollectionAttributeMapping().getCollectionDescriptor(), 0 );
collection.beginRead();
collection.endRead();
final CollectionEntry entry = persistenceContext.getCollectionEntry( collection );
if ( entry != null ) {
entry.postInitialize( collection );
}
}
}
}

View File

@ -8,7 +8,10 @@ package org.hibernate.orm.test.metamodel.mapping.collections;
import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Convert;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
@ -19,6 +22,8 @@ import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.SortNatural;
/**
* @author Steve Ebersole
*/
@ -34,6 +39,8 @@ public class EntityContainingSets {
private Set<SomeStuff> setOfComponents;
private Set<SimpleEntity> setOfEntities;
private SortedSet<String> sortedSetOfBasics;
public EntityContainingSets() {
}
@ -141,4 +148,22 @@ public class EntityContainingSets {
}
setOfEntities.add( value );
}
@ElementCollection()
@CollectionTable( name = "EntityOfSet_sortedBasics")
@SortNatural
public SortedSet<String> getSortedSetOfBasics() {
return sortedSetOfBasics;
}
public void setSortedSetOfBasics(SortedSet<String> sortedSetOfBasics) {
this.sortedSetOfBasics = sortedSetOfBasics;
}
public void addSortedBasic(String value) {
if ( sortedSetOfBasics == null ) {
sortedSetOfBasics = new TreeSet<>();
}
sortedSetOfBasics.add( value );
}
}

View File

@ -58,16 +58,16 @@ public class MapOperationTests {
// there is a problem with deleting entities which have basic collections. for some reason those
// do not register as cascadable, so we do not delete the collection rows first
// scope.inTransaction(
// session -> {
// final EntityContainingMaps entity = session.load( EntityContainingMaps.class, 1 );
// session.delete( entity );
// }
// );
scope.inTransaction(
session -> {
final EntityContainingMaps entity = session.load( EntityContainingMaps.class, 1 );
session.delete( entity );
}
);
// uber hacky temp way:
TempDropDataHelper.cleanDatabaseSchema( scope, domainModelScope );
// TempDropDataHelper.cleanDatabaseSchema( scope, domainModelScope );
}
@Test
@ -98,5 +98,8 @@ public class MapOperationTests {
session.delete( entity );
}
);
// re-create it so the drop-data can succeed
createData( scope );
}
}

View File

@ -67,7 +67,7 @@ public class PluralAttributeMappingTests {
final DomainMetamodel domainModel = scope.getSessionFactory().getDomainModel();
final EntityMappingType containerEntityDescriptor = domainModel.getEntityDescriptor( EntityContainingSets.class );
assertThat( containerEntityDescriptor.getNumberOfAttributeMappings(), is( 6 ) );
assertThat( containerEntityDescriptor.getNumberOfAttributeMappings(), is( 7 ) );
final AttributeMapping setOfBasics = containerEntityDescriptor.findAttributeMapping( "setOfBasics" );
assertThat( setOfBasics, notNullValue() );
@ -84,6 +84,9 @@ public class PluralAttributeMappingTests {
final AttributeMapping setOfEntities = containerEntityDescriptor.findAttributeMapping( "setOfEntities" );
assertThat( setOfEntities, notNullValue() );
final AttributeMapping sortedSetOfBasics = containerEntityDescriptor.findAttributeMapping( "sortedSetOfBasics" );
assertThat( sortedSetOfBasics, notNullValue() );
}
@Test

View File

@ -6,6 +6,8 @@
*/
package org.hibernate.orm.test.metamodel.mapping.collections;
import java.util.Iterator;
import org.hibernate.Hibernate;
import org.hibernate.testing.orm.junit.DomainModel;
@ -44,6 +46,9 @@ public class SetOperationTests {
entity.addBasic( "a value" );
entity.addBasic( "another value" );
entity.addSortedBasic( "def" );
entity.addSortedBasic( "abc" );
entity.addEnum( EnumValue.ONE );
entity.addEnum( EnumValue.TWO );
@ -60,14 +65,12 @@ public class SetOperationTests {
@AfterEach
public void dropData(SessionFactoryScope scope, DomainModelScope domainModelScope) {
// scope.inTransaction(
// session -> {
// final EntityContainingSets entity = session.load( EntityContainingSets.class, 1 );
// session.delete( entity );
// }
// );
TempDropDataHelper.cleanDatabaseSchema( scope, domainModelScope );
scope.inTransaction(
session -> {
final EntityContainingSets entity = session.load( EntityContainingSets.class, 1 );
session.delete( entity );
}
);
}
@Test
@ -117,10 +120,12 @@ public class SetOperationTests {
session.delete( entity );
}
);
// re-create it so the drop-data can succeed
createData( scope );
}
@Test
@FailureExpected( reason = "not sure" )
public void testTriggerFetch(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
@ -135,4 +140,24 @@ public class SetOperationTests {
}
);
}
@Test
public void testSortedSetAccess(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
final EntityContainingSets entity = session.get( EntityContainingSets.class, 1 );
assert ! Hibernate.isInitialized( entity.getSortedSetOfBasics() );
Hibernate.initialize( entity.getSortedSetOfBasics() );
assertThat( entity.getSortedSetOfBasics().size(), is( 2 ) );
final Iterator<String> iterator = entity.getSortedSetOfBasics().iterator();
final String first = iterator.next();
final String second = iterator.next();
assertThat( first, is( "abc" ) );
assertThat( second, is( "def" ) );
}
);
}
}

View File

@ -246,6 +246,11 @@ public class SelectClauseTests extends BaseSqmUnitTest {
EntityOfSets.class.getName() + ".setOfOneToMany",
"b"
);
collectionValueFunctionAssertions(
interpretSelect( "select value(b) from EntityOfSets e join e.sortedSetOfBasics b" ),
EntityOfSets.class.getName() + ".sortedSetOfBasics",
"b"
);
// todo : ManyToMany not properly handled atm
}

View File

@ -8,6 +8,7 @@ package org.hibernate.testing.orm.domain.gambit;
import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
@ -17,6 +18,7 @@ import javax.persistence.OneToMany;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.SortNatural;
/**
* @author Steve Ebersole
@ -31,6 +33,8 @@ public class EntityOfSets {
private Set<EntityOfSets> setOfOneToMany;
private Set<EntityOfSets> setOfManyToMany;
private SortedSet<String> sortedSetOfBasics;
public EntityOfSets() {
}
@ -103,4 +107,14 @@ public class EntityOfSets {
this.setOfManyToMany = setOfManyToMany;
}
@ElementCollection()
@CollectionTable( name = "EntityOfSet_sortedBasics")
@SortNatural
public SortedSet<String> getSortedSetOfBasics() {
return sortedSetOfBasics;
}
public void setSortedSetOfBasics(SortedSet<String> sortedSetOfBasics) {
this.sortedSetOfBasics = sortedSetOfBasics;
}
}