Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into wip/6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 committed Mar 16, 2021
2 parents d28b719 + 2c39bc0 commit 6c3d0d8
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .github/hibernate-github-bot.yml
@@ -0,0 +1,3 @@
---
jira:
projectKey: "HHH"
Expand Up @@ -252,7 +252,7 @@ public void processEntityHierarchies(Set<String> processedEntityNames) {
}

private List<XClass> orderAndFillHierarchy(List<XClass> original) {
List<XClass> copy = new ArrayList<>( original );
List<XClass> copy = new ArrayList<>( original.size() );
insertMappedSuperclasses( original, copy );

// order the hierarchy
Expand All @@ -266,16 +266,28 @@ private List<XClass> orderAndFillHierarchy(List<XClass> original) {
}

private void insertMappedSuperclasses(List<XClass> original, List<XClass> copy) {
final boolean debug = log.isDebugEnabled();
for ( XClass clazz : original ) {
XClass superClass = clazz.getSuperclass();
while ( superClass != null
&& !reflectionManager.equals( superClass, Object.class )
&& !copy.contains( superClass ) ) {
if ( superClass.isAnnotationPresent( Entity.class )
|| superClass.isAnnotationPresent( javax.persistence.MappedSuperclass.class ) ) {
copy.add( superClass );
if ( clazz.isAnnotationPresent( javax.persistence.MappedSuperclass.class ) ) {
if ( debug ) {
log.debugf(
"Skipping explicit MappedSuperclass %s, the class will be discovered analyzing the implementing class",
clazz
);
}
}
else {
copy.add( clazz );
XClass superClass = clazz.getSuperclass();
while ( superClass != null
&& !reflectionManager.equals( superClass, Object.class )
&& !copy.contains( superClass ) ) {
if ( superClass.isAnnotationPresent( Entity.class )
|| superClass.isAnnotationPresent( javax.persistence.MappedSuperclass.class ) ) {
copy.add( superClass );
}
superClass = superClass.getSuperclass();
}
superClass = superClass.getSuperclass();
}
}
}
Expand Down
Expand Up @@ -1584,8 +1584,7 @@ static int addElementsOfClass(
MetadataBuildingContext context) {
int idPropertyCounter = 0;

Collection<XProperty> properties = propertyContainer.getProperties();
for ( XProperty p : properties ) {
for ( XProperty p : propertyContainer.propertyIterator() ) {
final int currentIdPropertyCounter = addProperty(
propertyContainer,
p,
Expand Down
Expand Up @@ -9,6 +9,7 @@

package org.hibernate.cfg;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -35,6 +36,7 @@
import org.hibernate.cfg.annotations.HCANNHelper;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;

import org.jboss.logging.Logger;

Expand Down Expand Up @@ -63,7 +65,7 @@ class PropertyContainer {
*/
private final AccessType classLevelAccessType;

private final TreeMap<String, XProperty> persistentAttributeMap;
private final List<XProperty> persistentAttributes;

PropertyContainer(XClass clazz, XClass entityAtStake, AccessType defaultClassLevelAccessType) {
this.xClass = clazz;
Expand All @@ -83,7 +85,6 @@ class PropertyContainer {
: defaultClassLevelAccessType;
assert classLevelAccessType == AccessType.FIELD || classLevelAccessType == AccessType.PROPERTY;

this.persistentAttributeMap = new TreeMap<>();

final List<XProperty> fields = xClass.getDeclaredProperties( AccessType.FIELD.getType() );
final List<XProperty> getters = xClass.getDeclaredProperties( AccessType.PROPERTY.getType() );
Expand All @@ -92,18 +93,23 @@ class PropertyContainer {

final Map<String,XProperty> persistentAttributesFromGetters = new HashMap<>();

final TreeMap<String, XProperty> localAttributeMap = new TreeMap<>();
collectPersistentAttributesUsingLocalAccessType(
persistentAttributeMap,
xClass,
localAttributeMap,
persistentAttributesFromGetters,
fields,
getters
);
collectPersistentAttributesUsingClassLevelAccessType(
persistentAttributeMap,
xClass,
classLevelAccessType,
localAttributeMap,
persistentAttributesFromGetters,
fields,
getters
);
this.persistentAttributes = verifyAndInitializePersistentAttributes( xClass, localAttributeMap );
}

private void preFilter(List<XProperty> fields, List<XProperty> getters) {
Expand All @@ -124,7 +130,8 @@ private void preFilter(List<XProperty> fields, List<XProperty> getters) {
}
}

private void collectPersistentAttributesUsingLocalAccessType(
private static void collectPersistentAttributesUsingLocalAccessType(
XClass xClass,
TreeMap<String, XProperty> persistentAttributeMap,
Map<String,XProperty> persistentAttributesFromGetters,
List<XProperty> fields,
Expand Down Expand Up @@ -176,7 +183,9 @@ private void collectPersistentAttributesUsingLocalAccessType(
}
}

private void collectPersistentAttributesUsingClassLevelAccessType(
private static void collectPersistentAttributesUsingClassLevelAccessType(
XClass xClass,
AccessType classLevelAccessType,
TreeMap<String, XProperty> persistentAttributeMap,
Map<String,XProperty> persistentAttributesFromGetters,
List<XProperty> fields,
Expand Down Expand Up @@ -229,20 +238,30 @@ public AccessType getClassLevelAccessType() {
return classLevelAccessType;
}

/**
* @deprecated Use the {@link #propertyIterator()} method instead.
*/
@Deprecated
public Collection<XProperty> getProperties() {
assertTypesAreResolvable();
return Collections.unmodifiableCollection( persistentAttributeMap.values() );
return Collections.unmodifiableCollection( this.persistentAttributes );
}

public Iterable<XProperty> propertyIterator() {
return persistentAttributes;
}

private void assertTypesAreResolvable() {
for ( XProperty xProperty : persistentAttributeMap.values() ) {
private static List<XProperty> verifyAndInitializePersistentAttributes(XClass xClass, Map<String, XProperty> localAttributeMap) {
ArrayList<XProperty> output = new ArrayList( localAttributeMap.size() );
for ( XProperty xProperty : localAttributeMap.values() ) {
if ( !xProperty.isTypeResolved() && !discoverTypeWithoutReflection( xProperty ) ) {
String msg = "Property " + StringHelper.qualify( xClass.getName(), xProperty.getName() ) +
" has an unbound type and no explicit target entity. Resolve this Generic usage issue" +
" or set an explicit target attribute (eg @OneToMany(target=) or use an explicit @Type";
throw new AnnotationException( msg );
}
output.add( xProperty );
}
return CollectionHelper.toSmallList( output );
}
//
// private void considerExplicitFieldAndPropertyAccess() {
Expand Down
@@ -0,0 +1,90 @@
/*
* 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.mapping;

import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.MappedSuperclass;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;

@TestForIssue(jiraKey = "HHH-14499")
public class MappedSuperclassWithGenericsTest extends BaseCoreFunctionalTestCase {

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
IntermediateAbstractMapped.class,
BaseEntity.class,
AbstractGenericMappedSuperType.class,
};
}

@Test
public void testIt() {

}

@MappedSuperclass
public static abstract class AbstractGenericMappedSuperType<T> {

private T whateverType;

}

@MappedSuperclass
@IdClass(PK.class)
public static abstract class IntermediateAbstractMapped<T> extends AbstractGenericMappedSuperType<T> {

@Id
private String keyOne;
@Id
private String keyTwo;
@Id
private String keyThree;
}

@SuppressWarnings("UnusedDeclaration")
public static class PK implements Serializable {

private String keyOne;
private String keyTwo;
private String keyThree;

@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
PK pk = (PK) o;
return Objects.equals( keyOne, pk.keyOne ) &&
Objects.equals( keyTwo, pk.keyTwo ) &&
Objects.equals( keyThree, pk.keyThree );
}

@Override
public int hashCode() {
return Objects.hash( keyOne, keyTwo, keyThree );
}
}

@Entity(name = "BaseEntity")
public static class BaseEntity<T> extends IntermediateAbstractMapped<byte[]> {

String aString;

}

}

0 comments on commit 6c3d0d8

Please sign in to comment.