Merge remote-tracking branch 'upstream/master' into wip/6.0

This commit is contained in:
Andrea Boriero 2020-10-30 10:21:28 +00:00
commit a5bb92f0d5
10 changed files with 176 additions and 14 deletions

View File

@ -36,6 +36,13 @@ public interface SynchronizeableQuery<T> {
*/
SynchronizeableQuery<T> addSynchronizedQuerySpace(String querySpace);
/**
* Adds a table expression as a query space.
*/
default SynchronizeableQuery<T> addSynchronizedTable(String tableExpression) {
return addSynchronizedQuerySpace( tableExpression );
}
/**
* Adds an entity name for (a) auto-flush checking and (b) query result cache invalidation checking. Same as
* {@link #addSynchronizedQuerySpace} for all tables associated with the given entity.

View File

@ -189,9 +189,8 @@ public void doAfterTransactionCompletion(boolean success, SharedSessionContractI
for ( NaturalIdCleanup cleanup : naturalIdCleanups ) {
cleanup.release();
}
entityCleanups.clear();
naturalIdCleanups.clear();
for ( CollectionCleanup cleanup : collectionCleanups ) {
cleanup.release();

View File

@ -40,9 +40,9 @@
* @author Emmanuel Bernard
*/
@SuppressWarnings("unchecked")
public class JPAMetadataProvider implements MetadataProvider {
public final class JPAMetadataProvider implements MetadataProvider {
private final MetadataProvider delegate = new JavaMetadataProvider();
private static final MetadataProvider STATELESS_BASE_DELEGATE = new JavaMetadataProvider();
private final ClassLoaderAccess classLoaderAccess;
private final XMLContext xmlContext;
@ -54,7 +54,7 @@ public class JPAMetadataProvider implements MetadataProvider {
private final boolean xmlMappingEnabled;
private Map<Object, Object> defaults;
private Map<AnnotatedElement, AnnotationReader> cache = CollectionHelper.mapOfSize( 100 );
private Map<AnnotatedElement, AnnotationReader> cache;
/**
* @deprecated Use {@link JPAMetadataProvider#JPAMetadataProvider(BootstrapContext)} instead.
@ -92,19 +92,29 @@ public JPAMetadataProvider(BootstrapContext bootstrapContext) {
//all of the above can be safely rebuilt from XMLContext: only XMLContext this object is serialized
@Override
public AnnotationReader getAnnotationReader(AnnotatedElement annotatedElement) {
if ( cache == null ) {
cache = new HashMap<>(50 );
}
AnnotationReader reader = cache.get( annotatedElement );
if (reader == null) {
if ( xmlContext.hasContext() ) {
reader = new JPAOverriddenAnnotationReader( annotatedElement, xmlContext, classLoaderAccess );
}
else {
reader = delegate.getAnnotationReader( annotatedElement );
reader = STATELESS_BASE_DELEGATE.getAnnotationReader( annotatedElement );
}
cache.put(annotatedElement, reader);
cache.put( annotatedElement, reader );
}
return reader;
}
@Override
public void reset() {
//It's better to remove the HashMap, as it could grow rather large:
//when doing a clear() the internal buckets array is not scaled down.
this.cache = null;
}
@Override
public Map<Object, Object> getDefaults() {
if ( !xmlMappingEnabled ) {

View File

@ -7,9 +7,9 @@
package org.hibernate.engine.jdbc.env.internal;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
import org.hibernate.AssertionFailure;
import org.hibernate.boot.model.naming.DatabaseIdentifier;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.engine.jdbc.env.spi.IdentifierCaseStrategy;
@ -31,7 +31,7 @@ public class NormalizingIdentifierHelperImpl implements IdentifierHelper {
private final boolean globallyQuoteIdentifiers;
private final boolean globallyQuoteIdentifiersSkipColumnDefinitions;
private final boolean autoQuoteKeywords;
private final Set<String> reservedWords = new TreeSet<>( String.CASE_INSENSITIVE_ORDER );
private final TreeSet<String> reservedWords;
private final IdentifierCaseStrategy unquotedCaseStrategy;
private final IdentifierCaseStrategy quotedCaseStrategy;
@ -41,7 +41,7 @@ public NormalizingIdentifierHelperImpl(
boolean globallyQuoteIdentifiers,
boolean globallyQuoteIdentifiersSkipColumnDefinitions,
boolean autoQuoteKeywords,
Set<String> reservedWords,
TreeSet<String> reservedWords, //careful, we intentionally omit making a defensive copy to not waste memory
IdentifierCaseStrategy unquotedCaseStrategy,
IdentifierCaseStrategy quotedCaseStrategy) {
this.jdbcEnvironment = jdbcEnvironment;
@ -49,9 +49,7 @@ public NormalizingIdentifierHelperImpl(
this.globallyQuoteIdentifiers = globallyQuoteIdentifiers;
this.globallyQuoteIdentifiersSkipColumnDefinitions = globallyQuoteIdentifiersSkipColumnDefinitions;
this.autoQuoteKeywords = autoQuoteKeywords;
if ( reservedWords != null ) {
this.reservedWords.addAll( reservedWords );
}
this.reservedWords = reservedWords;
this.unquotedCaseStrategy = unquotedCaseStrategy == null ? IdentifierCaseStrategy.UPPER : unquotedCaseStrategy;
this.quotedCaseStrategy = quotedCaseStrategy == null ? IdentifierCaseStrategy.MIXED : quotedCaseStrategy;
}
@ -98,6 +96,9 @@ public Identifier applyGlobalQuoting(String text) {
@Override
public boolean isReservedWord(String word) {
if ( autoQuoteKeywords == false ) {
throw new AssertionFailure( "The reserved keywords map is only initialized if autoQuoteKeywords is true" );
}
return reservedWords.contains( word );
}

View File

@ -31,7 +31,10 @@ public class IdentifierHelperBuilder {
private NameQualifierSupport nameQualifierSupport = NameQualifierSupport.BOTH;
private Set<String> reservedWords = new TreeSet<>( String.CASE_INSENSITIVE_ORDER );
//TODO interesting computer science puzzle: find a more compact representation?
// we only need "contains" on this set, and it has to be case sensitive and efficient.
private final TreeSet<String> reservedWords = new TreeSet<>( String.CASE_INSENSITIVE_ORDER );
private boolean globallyQuoteIdentifiers = false;
private boolean skipGlobalQuotingForColumnDefinitions = false;
private boolean autoQuoteKeywords = true;

View File

@ -377,6 +377,10 @@ public void sessionFactoryClosed(SessionFactory factory) {
this,
serviceRegistry.getService( JndiService.class )
);
//As last operation, delete all caches from ReflectionManager
//(not modelled as a listener as we want this to be last)
metadata.getMetadataBuildingOptions().getReflectionManager().reset();
}
catch (Exception e) {
for ( Integrator integrator : serviceRegistry.getService( IntegratorService.class ).getIntegrators() ) {

View File

@ -0,0 +1,28 @@
package org.hibernate.test.jpa;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class MapContent {
@Id
private Long id;
@ManyToOne(optional = false)
private Relationship relationship;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Relationship getRelationship() {
return relationship;
}
public void setRelationship(Relationship relationship) {
this.relationship = relationship;
}
}

View File

@ -0,0 +1,35 @@
package org.hibernate.test.jpa;
import java.util.Map;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
@Entity
public class MapOwner {
@Id
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@MapKey(name = "relationship")
private Map<Relationship, MapContent> contents;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Map<Relationship, MapContent> getContents() {
return contents;
}
public void setContents(Map<Relationship, MapContent> contents) {
this.contents = contents;
}
}

View File

@ -0,0 +1,26 @@
package org.hibernate.test.jpa;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Relationship {
@Id
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,49 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* 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.jpa.ql;
import org.hibernate.Session;
import org.hibernate.dialect.PostgreSQL81Dialect;
import org.hibernate.test.jpa.AbstractJPATest;
import org.hibernate.test.jpa.MapContent;
import org.hibernate.test.jpa.MapOwner;
import org.hibernate.test.jpa.Relationship;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
@TestForIssue(jiraKey = "HHH-14279")
public class MapIssueTest extends AbstractJPATest {
@Override
public String[] getMappings() {
return new String[] {};
}
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { MapOwner.class, MapContent.class, Relationship.class};
}
@Test
@RequiresDialect(value = PostgreSQL81Dialect.class, comment = "Requires support for using a correlated column in a join condition which H2 apparently does not support.")
public void testWhereSubqueryMapKeyIsEntityWhereWithKey() {
Session s = openSession();
s.beginTransaction();
s.createQuery( "select r from Relationship r where exists (select 1 from MapOwner as o left join o.contents c with key(c) = r)" ).list();
s.getTransaction().commit();
s.close();
}
@Test
public void testMapKeyIsEntityWhereWithKey() {
Session s = openSession();
s.beginTransaction();
s.createQuery( "select 1 from MapOwner as o left join o.contents c where c.id is not null" ).list();
s.getTransaction().commit();
s.close();
}
}