initial work on @Version support

- by side effect fixed batch collection loading
This commit is contained in:
Steve Ebersole 2020-02-19 12:10:16 -06:00
parent 1503f09a41
commit ab6d1953d6
30 changed files with 171 additions and 71 deletions

View File

@ -38,7 +38,7 @@ public class InferredBasicValueResolution<J> implements BasicValue.Resolution<J>
this.relationalJtd = relationalJtd;
this.relationalStd = relationalStd;
this.valueConverter = valueConverter;
this.mutabilityPlan = mutabilityPlan;
this.mutabilityPlan = mutabilityPlan == null ? domainJtd.getMutabilityPlan() : mutabilityPlan;
}
@Override

View File

@ -17,12 +17,11 @@ import java.util.ListIterator;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.collection.spi.CollectionSemantics;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.sql.results.graph.DomainResultAssembler;
import org.hibernate.sql.results.jdbc.spi.RowProcessingState;
import org.hibernate.type.Type;
/**
@ -128,11 +127,17 @@ public class PersistentBag extends AbstractPersistentCollection implements List
assert bag == null;
final CollectionPersister collectionDescriptor = attributeMapping.getCollectionDescriptor();
this.bag = (List) collectionDescriptor.getCollectionSemantics().instantiateRaw( loadingState.size(), collectionDescriptor );
final CollectionSemantics collectionSemantics = collectionDescriptor.getCollectionSemantics();
for ( int i = 0; i < loadingState.size(); i++ ) {
//noinspection unchecked,UseBulkOperation
bag.add( loadingState.get( i ) );
final int elementCount = loadingState == null ? 0 : loadingState.size();
this.bag = (List) collectionSemantics.instantiateRaw( elementCount, collectionDescriptor );
if ( loadingState != null ) {
for ( int i = 0; i < elementCount; i++ ) {
//noinspection unchecked
bag.add( loadingState.get( i ) );
}
}
}

View File

@ -89,36 +89,38 @@ public class SingleIdLoadPlan<T> implements SingleEntityLoadPlan {
final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator( sessionFactory ).translate( sqlAst );
final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl(
restrictivePart.getJdbcTypeCount( sessionFactory.getTypeConfiguration() )
);
final int jdbcTypeCount = restrictivePart.getJdbcTypeCount( sessionFactory.getTypeConfiguration() );
assert jdbcParameters.size() % jdbcTypeCount == 0;
final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl( jdbcTypeCount );
final Iterator<JdbcParameter> paramItr = jdbcParameters.iterator();
restrictivePart.visitJdbcValues(
restrictedValue,
Clause.WHERE,
(value, type) -> {
assert paramItr.hasNext();
final JdbcParameter parameter = paramItr.next();
jdbcParameterBindings.addBinding(
parameter,
new JdbcParameterBinding() {
@Override
public JdbcMapping getBindType() {
return type;
}
while ( paramItr.hasNext() ) {
restrictivePart.visitJdbcValues(
restrictedValue,
Clause.WHERE,
(value, type) -> {
assert paramItr.hasNext();
final JdbcParameter parameter = paramItr.next();
jdbcParameterBindings.addBinding(
parameter,
new JdbcParameterBinding() {
@Override
public JdbcMapping getBindType() {
return type;
}
@Override
public Object getBindValue() {
return value;
@Override
public Object getBindValue() {
return value;
}
}
}
);
},
session
);
assert !paramItr.hasNext();
);
},
session
);
}
final List list = JdbcSelectExecutorStandardImpl.INSTANCE.list(
jdbcSelect,

View File

@ -19,7 +19,6 @@ import org.hibernate.type.BasicType;
public class EntityVersionMappingImpl extends BasicValuedSingularAttributeMapping implements EntityVersionMapping {
public EntityVersionMappingImpl(
String attributeName,
int stateArrayPosition,
StateArrayContributorMetadataAccess attributeMetadataAccess,
String containingTableExpression,
String mappedColumnExpression,
@ -28,7 +27,7 @@ public class EntityVersionMappingImpl extends BasicValuedSingularAttributeMappin
PropertyAccess propertyAccess) {
super(
attributeName,
stateArrayPosition,
0,
attributeMetadataAccess,
FetchStrategy.IMMEDIATE_JOIN,
containingTableExpression,

View File

@ -116,6 +116,7 @@ import org.hibernate.loader.ast.spi.MultiIdEntityLoader;
import org.hibernate.loader.ast.spi.NaturalIdLoader;
import org.hibernate.loader.ast.spi.SingleIdEntityLoader;
import org.hibernate.loader.ast.spi.SingleUniqueKeyEntityLoader;
import org.hibernate.mapping.BasicValue;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.Formula;
@ -141,8 +142,10 @@ import org.hibernate.metamodel.mapping.Queryable;
import org.hibernate.metamodel.mapping.SingularAttributeMapping;
import org.hibernate.metamodel.mapping.StateArrayContributorMapping;
import org.hibernate.metamodel.mapping.StateArrayContributorMetadata;
import org.hibernate.metamodel.mapping.StateArrayContributorMetadataAccess;
import org.hibernate.metamodel.mapping.internal.BasicEntityIdentifierMappingImpl;
import org.hibernate.metamodel.mapping.internal.EntityDiscriminatorMappingImpl;
import org.hibernate.metamodel.mapping.internal.EntityVersionMappingImpl;
import org.hibernate.metamodel.mapping.internal.InFlightEntityMappingType;
import org.hibernate.metamodel.mapping.internal.MappingModelCreationHelper;
import org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess;
@ -213,6 +216,7 @@ import org.hibernate.type.Type;
import org.hibernate.type.TypeHelper;
import org.hibernate.type.VersionType;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.MutabilityPlan;
/**
* Basic functionality for persisting an entity via JDBC
@ -5764,7 +5768,11 @@ public abstract class AbstractEntityPersister
versionMapping = creationProcess.processSubPart(
versionPropertyName,
(role, creationProcess1) -> generateVersionMapping( this, creationProcess )
(role, creationProcess1) -> generateVersionMapping(
this,
(RootClass) bootEntityDescriptor,
creationProcess
)
);
}
@ -5881,7 +5889,7 @@ public abstract class AbstractEntityPersister
discriminatorMapping = new EntityDiscriminatorMappingImpl(
this,
getRootTableName(),
getDiscriminatorColumnName(),
getDiscriminatorColumnReaders(),
(BasicType) getDiscriminatorType()
);
}
@ -6079,10 +6087,81 @@ public abstract class AbstractEntityPersister
);
}
/**
* @param entityPersister The AbstractEntityPersister being constructed - still initializing
* @param bootModelRootEntityDescriptor The boot-time entity descriptor for the "root entity" in the hierarchy
* @param creationProcess The SF creation process - access to useful things
*/
private static EntityVersionMapping generateVersionMapping(
EntityPersister entityPersister,
AbstractEntityPersister entityPersister,
RootClass bootModelRootEntityDescriptor,
MappingModelCreationProcess creationProcess) {
throw new NotYetImplementedFor6Exception( AbstractEntityPersister.class );
final PropertyAccess propertyAccess = entityPersister.representationStrategy.resolvePropertyAccess(
bootModelRootEntityDescriptor.getVersion()
);
final BasicValue.Resolution<?> basicTypeResolution = ( (BasicValue) bootModelRootEntityDescriptor.getVersion().getValue() ).resolve();
final StateArrayContributorMetadataAccess attributeMetadataAccess = entityMappingType -> new StateArrayContributorMetadata() {
private final MutabilityPlan mutabilityPlan = basicTypeResolution.getMutabilityPlan();
@Override
public PropertyAccess getPropertyAccess() {
return propertyAccess;
}
@Override
public MutabilityPlan getMutabilityPlan() {
return mutabilityPlan;
}
@Override
public boolean isNullable() {
return false;
}
@Override
public boolean isInsertable() {
return true;
}
@Override
public boolean isUpdatable() {
return true;
}
@Override
public boolean isIncludedInDirtyChecking() {
return false;
}
@Override
public boolean isIncludedInOptimisticLocking() {
return false;
}
@Override
public CascadeStyle getCascadeStyle() {
return CascadeStyles.NONE;
}
};
final Iterator versionColumnIterator = bootModelRootEntityDescriptor.getVersion().getColumnIterator();
assert versionColumnIterator.hasNext();
final Dialect dialect = creationProcess.getCreationContext().getSessionFactory().getJdbcServices().getDialect();
final String versionColumnName = ( (Column) versionColumnIterator.next() ).getQuotedName( dialect );
assert !versionColumnIterator.hasNext();
return new EntityVersionMappingImpl(
bootModelRootEntityDescriptor.getVersion().getName(),
attributeMetadataAccess,
entityPersister.getRootTableName(),
versionColumnName,
basicTypeResolution.getResolvedBasicType(),
entityPersister,
propertyAccess
);
}
private AttributeMapping generateNonIdAttributeMapping(

View File

@ -949,6 +949,9 @@ public final class StandardBasicTypes {
"url", java.net.URL.class.getName()
);
// Specialized version handlers
handle(
ROW_VERSION,
null,
@ -956,6 +959,13 @@ public final class StandardBasicTypes {
"row_version"
);
handle(
DbTimestampType.INSTANCE,
null,
basicTypeRegistry,
DbTimestampType.INSTANCE.getName()
);
// todo (6.0) - ? how to handle DbTimestampType?
// DbTimestampType was really just a variant of TimestampType with overridden
// version (opt lock) support

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.version;
package org.hibernate.orm.test.version;
import java.time.Instant;
import javax.persistence.Entity;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.version;
package org.hibernate.orm.test.version;
import java.time.LocalDateTime;
import javax.persistence.Entity;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.version;
package org.hibernate.orm.test.version;
import java.time.OffsetDateTime;
import javax.persistence.Entity;

View File

@ -6,7 +6,7 @@
*/
//$Id: Person.java 7676 2005-07-29 06:27:10Z oneovthafew $
package org.hibernate.test.version;
package org.hibernate.orm.test.version;
import java.util.ArrayList;
import java.util.List;

View File

@ -15,7 +15,7 @@
a version increment on the owning instance)
-->
<hibernate-mapping
package="org.hibernate.test.version">
package="org.hibernate.orm.test.version">
<class name="Person">
<id name="name"/>

View File

@ -6,7 +6,7 @@
*/
//$Id: Task.java 7676 2005-07-29 06:27:10Z oneovthafew $
package org.hibernate.test.version;
package org.hibernate.orm.test.version;
public class Task {

View File

@ -6,7 +6,7 @@
*/
//$Id: Thing.java 7715 2005-08-01 16:46:57Z oneovthafew $
package org.hibernate.test.version;
package org.hibernate.orm.test.version;
public class Thing {

View File

@ -4,10 +4,11 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.version;
package org.hibernate.orm.test.version;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.junit.Test;
@ -28,7 +29,12 @@ import static org.junit.Assert.assertTrue;
public class VersionTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "version/PersonThing.hbm.xml" };
return new String[] { "org/hibernate/orm/test/version/PersonThing.hbm.xml" };
}
@Override
protected String getBaseForMappings() {
return "";
}
@Test
@ -148,9 +154,6 @@ public class VersionTest extends BaseCoreFunctionalTestCase {
}
private Person getPerson(Session s) {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
criteria.from( Person.class );
return s.createQuery( criteria ).uniqueResult();
return session.createQuery( "select p from Person p", Person.class ).uniqueResult();
}
}

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.version;
package org.hibernate.orm.test.version;
import java.time.ZonedDateTime;
import javax.persistence.Entity;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.version.db;
package org.hibernate.orm.test.version.db;
import java.sql.Timestamp;
@ -27,6 +27,11 @@ public class DbVersionTest extends BaseCoreFunctionalTestCase {
return new String[] { "version/db/User.hbm.xml" };
}
@Override
protected String getBaseForMappings() {
return "org/hibernate/orm/test/";
}
@Test
public void testCollectionVersion() throws Exception {
Session s = openSession();

View File

@ -6,7 +6,7 @@
*/
// $Id: Group.java 7736 2005-08-03 20:03:34Z steveebersole $
package org.hibernate.test.version.db;
package org.hibernate.orm.test.version.db;
import java.util.Date;
import java.util.Set;

View File

@ -6,7 +6,7 @@
*/
// $Id: Permission.java 7736 2005-08-03 20:03:34Z steveebersole $
package org.hibernate.test.version.db;
package org.hibernate.orm.test.version.db;
import java.util.Date;
/**

View File

@ -15,7 +15,7 @@
a version increment on the owning instance)
-->
<hibernate-mapping
package="org.hibernate.test.version.db">
package="org.hibernate.orm.test.version.db">
<class name="User" table="db_vers_user">
<id name="id" column="user_id" type="long">

View File

@ -6,7 +6,7 @@
*/
// $Id: User.java 7736 2005-08-03 20:03:34Z steveebersole $
package org.hibernate.test.version.db;
package org.hibernate.orm.test.version.db;
import java.sql.Timestamp;
import java.util.Set;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.version.mappedsuperclass;
package org.hibernate.orm.test.version.mappedsuperclass;
import javax.persistence.MappedSuperclass;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.version.mappedsuperclass;
package org.hibernate.orm.test.version.mappedsuperclass;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
@ -27,7 +27,7 @@ import static org.junit.Assert.assertThat;
public class HbmMappingMappedSuperclassWithVersionTest extends BaseEntityManagerFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] {"org/hibernate/test/version/mappedsuperclass/TestEntity.hbm.xml"};
return new String[] {"org/hibernate/orm/test/version/mappedsuperclass/TestEntity.hbm.xml"};
}
@Override

View File

@ -15,7 +15,7 @@
a version increment on the owning instance)
-->
<hibernate-mapping
package="org.hibernate.test.version.mappedsuperclass">
package="org.hibernate.orm.test.version.mappedsuperclass">
<class name="TestEntity">
<id name="id">

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.version.mappedsuperclass;
package org.hibernate.orm.test.version.mappedsuperclass;
/**
* @author Andrea Boriero

View File

@ -6,7 +6,7 @@
*/
// $Id: Group.java 7805 2005-08-10 16:25:11Z steveebersole $
package org.hibernate.test.version.sybase;
package org.hibernate.orm.test.version.sybase;
import java.util.Date;
import java.util.Set;

View File

@ -6,7 +6,7 @@
*/
// $Id: Permission.java 7805 2005-08-10 16:25:11Z steveebersole $
package org.hibernate.test.version.sybase;
package org.hibernate.orm.test.version.sybase;
import java.util.Date;
/**

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.version.sybase;
package org.hibernate.orm.test.version.sybase;
import javax.persistence.Column;
import javax.persistence.Entity;
@ -17,9 +17,6 @@ import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.annotations.Source;
import org.hibernate.annotations.SourceType;
import org.hibernate.annotations.Type;
import org.hibernate.dialect.SybaseASE15Dialect;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.version.sybase;
package org.hibernate.orm.test.version.sybase;
import javax.persistence.OptimisticLockException;

View File

@ -13,7 +13,7 @@
Demonstrates use of the TIMESTAMP datatype available in Sybase
and SQL Server for optimistic locking value.
-->
<hibernate-mapping package="org.hibernate.test.version.sybase">
<hibernate-mapping package="org.hibernate.orm.test.version.sybase">
<class name="User" table="syb_ts_user">
<id name="id" column="user_id" type="long">

View File

@ -6,7 +6,7 @@
*/
// $Id: User.java 7805 2005-08-10 16:25:11Z steveebersole $
package org.hibernate.test.version.sybase;
package org.hibernate.orm.test.version.sybase;
import java.util.Set;
/**