HHH-10664 - Prep 6.0 feature branch - merge hibernate-entitymanager into hibernate-core (fix test failures)

This commit is contained in:
Steve Ebersole 2016-05-02 23:19:01 -05:00
parent 1c1783e90f
commit 9ecb2a3deb
9 changed files with 100 additions and 56 deletions

View File

@ -61,11 +61,13 @@ public void testSessionInterceptor() {
.withOptions() .withOptions()
.interceptor(new LoggingInterceptor() ) .interceptor(new LoggingInterceptor() )
.openSession(); .openSession();
session.getTransaction().begin();
Customer customer = session.get( Customer.class, customerId ); Customer customer = session.get( Customer.class, customerId );
customer.setName( "Mr. John Doe" ); customer.setName( "Mr. John Doe" );
//Entity Customer#1 changed from [John Doe, 0] to [Mr. John Doe, 0] //Entity Customer#1 changed from [John Doe, 0] to [Mr. John Doe, 0]
session.flush();
session.getTransaction().commit();
//end::events-interceptors-session-scope-example[] //end::events-interceptors-session-scope-example[]
session.close(); session.close();
} }
@ -84,11 +86,12 @@ public void testSessionFactoryInterceptor() {
.build(); .build();
//end::events-interceptors-session-factory-scope-example[] //end::events-interceptors-session-factory-scope-example[]
Session session = sessionFactory.openSession(); Session session = sessionFactory.openSession();
session.getTransaction().begin();
Customer customer = session.get( Customer.class, customerId ); Customer customer = session.get( Customer.class, customerId );
customer.setName( "Mr. John Doe" ); customer.setName( "Mr. John Doe" );
//Entity Customer#1 changed from [John Doe, 0] to [Mr. John Doe, 0] //Entity Customer#1 changed from [John Doe, 0] to [Mr. John Doe, 0]
session.flush(); session.getTransaction().commit();
session.close(); session.close();
sessionFactory.close(); sessionFactory.close();
} }

View File

@ -129,7 +129,7 @@ public void testFlushAutoSQLNativeSession() {
assertTrue(((Number) session assertTrue(((Number) session
.createSQLQuery( "select count(*) from Person") .createSQLQuery( "select count(*) from Person")
.uniqueResult()).intValue() == 0 ); .uniqueResult()).intValue() == 0 );
//end::flushing-auto-flush-sql-native-example[] //end::flushing-auto-flush-sql-native-example[\]
} ); } );
} }

View File

@ -35,9 +35,6 @@
import javax.persistence.criteria.SetJoin; import javax.persistence.criteria.SetJoin;
import javax.persistence.criteria.Subquery; import javax.persistence.criteria.Subquery;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.Session;
import org.hibernate.internal.SessionFactoryImpl; import org.hibernate.internal.SessionFactoryImpl;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.internal.util.collections.CollectionHelper;
@ -84,8 +81,6 @@
import org.hibernate.query.criteria.internal.predicate.LikePredicate; import org.hibernate.query.criteria.internal.predicate.LikePredicate;
import org.hibernate.query.criteria.internal.predicate.MemberOfPredicate; import org.hibernate.query.criteria.internal.predicate.MemberOfPredicate;
import org.hibernate.query.criteria.internal.predicate.NullnessPredicate; import org.hibernate.query.criteria.internal.predicate.NullnessPredicate;
import org.hibernate.type.SerializableType;
import org.hibernate.type.Type;
/** /**
* Hibernate implementation of the JPA {@link CriteriaBuilder} contract. * Hibernate implementation of the JPA {@link CriteriaBuilder} contract.

View File

@ -28,6 +28,7 @@
import org.hibernate.jpa.spi.HibernateEntityManagerImplementor; import org.hibernate.jpa.spi.HibernateEntityManagerImplementor;
import org.hibernate.query.criteria.internal.compile.CompilableCriteria; import org.hibernate.query.criteria.internal.compile.CompilableCriteria;
import org.hibernate.query.criteria.internal.compile.CriteriaInterpretation; import org.hibernate.query.criteria.internal.compile.CriteriaInterpretation;
import org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter;
import org.hibernate.query.criteria.internal.compile.ImplicitParameterBinding; import org.hibernate.query.criteria.internal.compile.ImplicitParameterBinding;
import org.hibernate.query.criteria.internal.compile.InterpretedParameterMetadata; import org.hibernate.query.criteria.internal.compile.InterpretedParameterMetadata;
import org.hibernate.query.criteria.internal.compile.RenderingContext; import org.hibernate.query.criteria.internal.compile.RenderingContext;
@ -370,13 +371,12 @@ public void validate(Type[] returnTypes) {
implicitParameterBinding.bind( jpaqlQuery ); implicitParameterBinding.bind( jpaqlQuery );
} }
// return new CriteriaQueryTypeQueryAdapter( return new CriteriaQueryTypeQueryAdapter(
// entityManager, entityManager,
// jpaqlQuery, jpaqlQuery,
// parameterMetadata.explicitParameterInfoMap() parameterMetadata.explicitParameterInfoMap()
// ); );
return jpaqlQuery;
} }
private Map<String, Class> extractTypeMap(List<ImplicitParameterBinding> implicitParameterBindings) { private Map<String, Class> extractTypeMap(List<ImplicitParameterBinding> implicitParameterBindings) {

View File

@ -16,17 +16,27 @@
import javax.persistence.LockModeType; import javax.persistence.LockModeType;
import javax.persistence.Parameter; import javax.persistence.Parameter;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.ParameterExpression; import javax.persistence.criteria.ParameterExpression;
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.jpa.HibernateQuery; import org.hibernate.query.Query;
import org.hibernate.query.internal.AbstractProducedQuery;
import org.hibernate.query.spi.QueryImplementor; import org.hibernate.query.spi.QueryImplementor;
import org.hibernate.type.Type;
/** /**
* <strong>Make this go away in 6.0</strong> :)
* <p/>
* Needed because atm we render a JPA Criteria query into a HQL/JPQL query String and some metadata, and then
* compile into a Query. This class wraps the compiled HQL/JPQL query and adds an extra layer of metadata.
* <p/>
* But the move to SQM in 6.0 allows us to do away with the "wrapping".
*
* Essentially
*
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class CriteriaQueryTypeQueryAdapter<X> implements TypedQuery<X>, HibernateQuery { public class CriteriaQueryTypeQueryAdapter<X> extends AbstractProducedQuery<X> implements QueryImplementor<X> {
private final SessionImplementor entityManager; private final SessionImplementor entityManager;
private final QueryImplementor<X> jpqlQuery; private final QueryImplementor<X> jpqlQuery;
private final Map<ParameterExpression<?>, ExplicitParameterInfo<?>> explicitParameterInfoMap; private final Map<ParameterExpression<?>, ExplicitParameterInfo<?>> explicitParameterInfoMap;
@ -35,16 +45,12 @@ public CriteriaQueryTypeQueryAdapter(
SessionImplementor entityManager, SessionImplementor entityManager,
QueryImplementor<X> jpqlQuery, QueryImplementor<X> jpqlQuery,
Map<ParameterExpression<?>, ExplicitParameterInfo<?>> explicitParameterInfoMap) { Map<ParameterExpression<?>, ExplicitParameterInfo<?>> explicitParameterInfoMap) {
super( entityManager, jpqlQuery.getParameterMetadata() );
this.entityManager = entityManager; this.entityManager = entityManager;
this.jpqlQuery = jpqlQuery; this.jpqlQuery = jpqlQuery;
this.explicitParameterInfoMap = explicitParameterInfoMap; this.explicitParameterInfoMap = explicitParameterInfoMap;
} }
@Override
public QueryImplementor getHibernateQuery() {
return jpqlQuery;
}
public List<X> getResultList() { public List<X> getResultList() {
return jpqlQuery.getResultList(); return jpqlQuery.getResultList();
} }
@ -57,7 +63,7 @@ public int getMaxResults() {
return jpqlQuery.getMaxResults(); return jpqlQuery.getMaxResults();
} }
public TypedQuery<X> setMaxResults(int i) { public QueryImplementor<X> setMaxResults(int i) {
jpqlQuery.setMaxResults( i ); jpqlQuery.setMaxResults( i );
return this; return this;
} }
@ -66,7 +72,7 @@ public int getFirstResult() {
return jpqlQuery.getFirstResult(); return jpqlQuery.getFirstResult();
} }
public TypedQuery<X> setFirstResult(int i) { public QueryImplementor<X> setFirstResult(int i) {
jpqlQuery.setFirstResult( i ); jpqlQuery.setFirstResult( i );
return this; return this;
} }
@ -75,16 +81,31 @@ public Map<String, Object> getHints() {
return jpqlQuery.getHints(); return jpqlQuery.getHints();
} }
public TypedQuery<X> setHint(String name, Object value) { public QueryImplementor<X> setHint(String name, Object value) {
jpqlQuery.setHint( name, value ); jpqlQuery.setHint( name, value );
return this; return this;
} }
@Override
protected boolean isNativeQuery() {
return false;
}
@Override
public String getQueryString() {
return jpqlQuery.getQueryString();
}
public FlushModeType getFlushMode() { public FlushModeType getFlushMode() {
return jpqlQuery.getFlushMode(); return jpqlQuery.getFlushMode();
} }
public TypedQuery<X> setFlushMode(FlushModeType flushModeType) { @Override
public Type[] getReturnTypes() {
return jpqlQuery.getReturnTypes();
}
public QueryImplementor<X> setFlushMode(FlushModeType flushModeType) {
jpqlQuery.setFlushMode( flushModeType ); jpqlQuery.setFlushMode( flushModeType );
return this; return this;
} }
@ -93,11 +114,26 @@ public LockModeType getLockMode() {
return jpqlQuery.getLockMode(); return jpqlQuery.getLockMode();
} }
public TypedQuery<X> setLockMode(LockModeType lockModeType) { public QueryImplementor<X> setLockMode(LockModeType lockModeType) {
jpqlQuery.setLockMode( lockModeType ); jpqlQuery.setLockMode( lockModeType );
return this; return this;
} }
@Override
public Query<X> setEntity(int position, Object val) {
return null;
}
@Override
public Query<X> setEntity(String name, Object val) {
return null;
}
@Override
public String[] getReturnAliases() {
return new String[0];
}
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public Set<Parameter<?>> getParameters() { public Set<Parameter<?>> getParameters() {
entityManager.checkOpen( false ); entityManager.checkOpen( false );
@ -142,7 +178,7 @@ else if ( param.getPosition() != null && param.getPosition().equals( parameterIn
} }
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public <T> TypedQuery<X> setParameter(Parameter<T> param, T t) { public <T> QueryImplementor<X> setParameter(Parameter<T> param, T t) {
entityManager.checkOpen( false ); entityManager.checkOpen( false );
final ExplicitParameterInfo parameterInfo = resolveParameterInfo( param ); final ExplicitParameterInfo parameterInfo = resolveParameterInfo( param );
if ( parameterInfo.isNamed() ) { if ( parameterInfo.isNamed() ) {
@ -155,7 +191,7 @@ public <T> TypedQuery<X> setParameter(Parameter<T> param, T t) {
} }
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public TypedQuery<X> setParameter(Parameter<Calendar> param, Calendar calendar, TemporalType temporalType) { public QueryImplementor<X> setParameter(Parameter<Calendar> param, Calendar calendar, TemporalType temporalType) {
entityManager.checkOpen( false ); entityManager.checkOpen( false );
final ExplicitParameterInfo parameterInfo = resolveParameterInfo( param ); final ExplicitParameterInfo parameterInfo = resolveParameterInfo( param );
if ( parameterInfo.isNamed() ) { if ( parameterInfo.isNamed() ) {
@ -168,7 +204,7 @@ public TypedQuery<X> setParameter(Parameter<Calendar> param, Calendar calendar,
} }
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public TypedQuery<X> setParameter(Parameter<Date> param, Date date, TemporalType temporalType) { public QueryImplementor<X> setParameter(Parameter<Date> param, Date date, TemporalType temporalType) {
entityManager.checkOpen( false ); entityManager.checkOpen( false );
final ExplicitParameterInfo parameterInfo = resolveParameterInfo( param ); final ExplicitParameterInfo parameterInfo = resolveParameterInfo( param );
if ( parameterInfo.isNamed() ) { if ( parameterInfo.isNamed() ) {
@ -219,7 +255,7 @@ public <T> Parameter<T> getParameter(String name, Class<T> type) {
} }
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public TypedQuery<X> setParameter(String name, Object value) { public QueryImplementor<X> setParameter(String name, Object value) {
entityManager.checkOpen( true ); entityManager.checkOpen( true );
ExplicitParameterInfo parameterInfo = locateParameterByName( name ); ExplicitParameterInfo parameterInfo = locateParameterByName( name );
parameterInfo.validateBindValue( value ); parameterInfo.validateBindValue( value );
@ -228,7 +264,7 @@ public TypedQuery<X> setParameter(String name, Object value) {
} }
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public TypedQuery<X> setParameter(String name, Calendar calendar, TemporalType temporalType) { public QueryImplementor<X> setParameter(String name, Calendar calendar, TemporalType temporalType) {
entityManager.checkOpen( true ); entityManager.checkOpen( true );
ExplicitParameterInfo parameterInfo = locateParameterByName( name ); ExplicitParameterInfo parameterInfo = locateParameterByName( name );
parameterInfo.validateCalendarBind(); parameterInfo.validateCalendarBind();
@ -237,7 +273,7 @@ public TypedQuery<X> setParameter(String name, Calendar calendar, TemporalType t
} }
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public TypedQuery<X> setParameter(String name, Date date, TemporalType temporalType) { public QueryImplementor<X> setParameter(String name, Date date, TemporalType temporalType) {
entityManager.checkOpen( true ); entityManager.checkOpen( true );
ExplicitParameterInfo parameterInfo = locateParameterByName( name ); ExplicitParameterInfo parameterInfo = locateParameterByName( name );
parameterInfo.validateDateBind(); parameterInfo.validateDateBind();
@ -252,15 +288,15 @@ public int executeUpdate() {
throw new IllegalStateException( "Typed criteria queries do not support executeUpdate" ); throw new IllegalStateException( "Typed criteria queries do not support executeUpdate" );
} }
public TypedQuery<X> setParameter(int i, Object o) { public QueryImplementor<X> setParameter(int i, Object o) {
throw new IllegalArgumentException( "Criteria queries do not support positioned parameters" ); throw new IllegalArgumentException( "Criteria queries do not support positioned parameters" );
} }
public TypedQuery<X> setParameter(int i, Calendar calendar, TemporalType temporalType) { public QueryImplementor<X> setParameter(int i, Calendar calendar, TemporalType temporalType) {
throw new IllegalArgumentException( "Criteria queries do not support positioned parameters" ); throw new IllegalArgumentException( "Criteria queries do not support positioned parameters" );
} }
public TypedQuery<X> setParameter(int i, Date date, TemporalType temporalType) { public QueryImplementor<X> setParameter(int i, Date date, TemporalType temporalType) {
throw new IllegalArgumentException( "Criteria queries do not support positioned parameters" ); throw new IllegalArgumentException( "Criteria queries do not support positioned parameters" );
} }

View File

@ -103,7 +103,8 @@ public abstract class AbstractProducedQuery<R> implements QueryImplementor<R> {
private ResultTransformer resultTransformer; private ResultTransformer resultTransformer;
private RowSelection queryOptions = new RowSelection(); private RowSelection queryOptions = new RowSelection();
private HQLQueryPlan entityGraphHintedQueryPlan;
private EntityGraphQueryHint entityGraphQueryHint;
private Object optionalObject; private Object optionalObject;
private Serializable optionalId; private Serializable optionalId;
@ -487,14 +488,14 @@ public QueryImplementor setParameterList(String name, Object[] values) {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public QueryImplementor setParameter(Parameter param, Calendar value, TemporalType temporalType) { public QueryImplementor setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType) {
queryParameterBindings.getBinding( (QueryParameter) param ).setBindValue( value, temporalType ); queryParameterBindings.getBinding( (QueryParameter) param ).setBindValue( value, temporalType );
return this; return this;
} }
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public QueryImplementor setParameter(Parameter param, Date value, TemporalType temporalType) { public QueryImplementor setParameter(Parameter<Date> param, Date value, TemporalType temporalType) {
queryParameterBindings.getBinding( (QueryParameter) param ).setBindValue( value, temporalType ); queryParameterBindings.getBinding( (QueryParameter) param ).setBindValue( value, temporalType );
return this; return this;
} }
@ -1009,19 +1010,8 @@ protected void applyAliasSpecificLockModeHint(String alias, LockMode lockMode) {
* *
* @param hint The entity graph hint object * @param hint The entity graph hint object
*/ */
public void applyEntityGraphQueryHint(EntityGraphQueryHint hint) { protected void applyEntityGraphQueryHint(EntityGraphQueryHint hint) {
queryParameterBindings.verifyParametersBound( false ); this.entityGraphQueryHint = hint;
// todo : ideally we'd update the instance state related to queryString but that is final atm
final String expandedQuery = queryParameterBindings.expandListValuedParameters( getQueryString(), getProducer() );
this.entityGraphHintedQueryPlan = new HQLQueryPlan(
expandedQuery,
false,
getProducer().getLoadQueryInfluencers().getEnabledFilters(),
getProducer().getFactory(),
hint
);
} }
/** /**
@ -1057,6 +1047,25 @@ public <T> T unwrap(Class<T> cls) {
} }
public QueryParameters getQueryParameters() { public QueryParameters getQueryParameters() {
final HQLQueryPlan entityGraphHintedQueryPlan;
if ( entityGraphQueryHint == null) {
entityGraphHintedQueryPlan = null;
}
else {
queryParameterBindings.verifyParametersBound( false );
// todo : ideally we'd update the instance state related to queryString but that is final atm
final String expandedQuery = queryParameterBindings.expandListValuedParameters( getQueryString(), getProducer() );
entityGraphHintedQueryPlan = new HQLQueryPlan(
expandedQuery,
false,
getProducer().getLoadQueryInfluencers().getEnabledFilters(),
getProducer().getFactory(),
entityGraphQueryHint
);
}
QueryParameters queryParameters = new QueryParameters( QueryParameters queryParameters = new QueryParameters(
getPositionalParameterTypes(), getPositionalParameterTypes(),
getPositionalParameterValues(), getPositionalParameterValues(),

View File

@ -213,6 +213,7 @@ else if ( NativeSQLQueryConstructorReturn.class.isInstance( queryReturn ) ) {
super.beforeQuery(); super.beforeQuery();
if ( getSynchronizedQuerySpaces() != null && !getSynchronizedQuerySpaces().isEmpty() ) { if ( getSynchronizedQuerySpaces() != null && !getSynchronizedQuerySpaces().isEmpty() ) {
// The application defined query spaces on the Hibernate native SQLQuery which means the query will already // The application defined query spaces on the Hibernate native SQLQuery which means the query will already
// perform a partial flush according to the defined query spaces, no need to do a full flush. // perform a partial flush according to the defined query spaces, no need to do a full flush.
@ -237,9 +238,11 @@ private boolean shouldFlush() {
} }
if ( effectiveFlushMode != FlushMode.MANUAL ) { if ( effectiveFlushMode != FlushMode.MANUAL ) {
if ( getProducer().getFactory().getSessionFactoryOptions().isJpaBootstrap() ) {
return true; return true;
} }
} }
}
return false; return false;
} }

View File

@ -9,7 +9,6 @@
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import org.hibernate.ResourceClosedException;
import org.hibernate.TransactionException; import org.hibernate.TransactionException;
import org.hibernate.resource.jdbc.ResourceRegistry; import org.hibernate.resource.jdbc.ResourceRegistry;
import org.hibernate.resource.jdbc.spi.LogicalConnectionImplementor; import org.hibernate.resource.jdbc.spi.LogicalConnectionImplementor;
@ -35,7 +34,7 @@ public PhysicalJdbcTransaction getPhysicalJdbcTransaction() {
protected void errorIfClosed() { protected void errorIfClosed() {
if ( !isOpen() ) { if ( !isOpen() ) {
throw new ResourceClosedException( this.toString() + " is closed" ); throw new IllegalStateException( this.toString() + " is closed" );
} }
} }

View File

@ -161,7 +161,6 @@ public void testCloseAndTransaction() throws Exception {
} }
catch ( IllegalStateException e ) { catch ( IllegalStateException e ) {
//success //success
em.getTransaction().rollback();
} }
} }