Skip to content

Commit aed7d18

Browse files
cigalygavinking
authored andcommitted
HHH-19134 Fixing problem - find entity by ID when multiple properties are annotated by @id annotation
Set compartor to null when type is null in org.hibernate.type.descriptor.java.AbstractJavaType contructor
1 parent 68e0625 commit aed7d18

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/AbstractJavaType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected AbstractJavaType(Type type) {
4848
protected AbstractJavaType(Type type, MutabilityPlan<T> mutabilityPlan) {
4949
this.type = type;
5050
this.mutabilityPlan = mutabilityPlan;
51-
this.comparator = Comparable.class.isAssignableFrom( getJavaTypeClass() )
51+
this.comparator = type != null && Comparable.class.isAssignableFrom( getJavaTypeClass() )
5252
? (Comparator<T>) ComparableComparator.INSTANCE
5353
: null;
5454
}

tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockEntityPersister.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ public final Type getPropertyType(String propertyPath) {
102102
result = getSubclassPropertyType(propertyPath);
103103
}
104104

105+
if ("id".equals( propertyPath )) {
106+
result = identifierType();
107+
}
108+
105109
if (result!=null) {
106110
propertyTypesByName.put(propertyPath, result);
107111
}

tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/ProcessorSessionFactory.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private static Element dereference(AccessType defaultAccessType, Element symbol,
166166
private Type propertyType(Element member, String entityName, String path, AccessType defaultAccessType) {
167167
final TypeMirror memberType = memberType(member);
168168
if (isEmbeddedProperty(member)) {
169-
return component.make(asElement(memberType), entityName, path, defaultAccessType, this);
169+
return componentType( entityName, path, defaultAccessType, memberType );
170170
}
171171
else if (isToOneAssociation(member)) {
172172
return new ManyToOneType(getTypeConfiguration(), getToOneTargetEntity(member));
@@ -186,6 +186,10 @@ else if (isEnumProperty(member)) {
186186
}
187187
}
188188

189+
private Component componentType(String entityName, String path, AccessType defaultAccessType, TypeMirror memberType) {
190+
return component.make( asElement( memberType ), entityName, path, defaultAccessType, this );
191+
}
192+
189193
@SuppressWarnings({"rawtypes", "unchecked"})
190194
private static BasicType<?> enumType(Element member, TypeMirror memberType) {
191195
final Class<Enum> enumClass = Enum.class; // because we can't load the real enum class!
@@ -414,6 +418,12 @@ public String identifierPropertyName() {
414418

415419
@Override
416420
public Type identifierType() {
421+
if (hasAnnotation( type, "IdClass" )) {
422+
final TypeMirror annotationMember = (TypeMirror)getAnnotationMember( getAnnotation( type, "IdClass" ), "value" );
423+
if (annotationMember != null) {
424+
return factory.componentType( getEntityName(), EntityIdentifierMapping.ID_ROLE_NAME, defaultAccessType, annotationMember );
425+
}
426+
}
417427
for (Element element : type.getEnclosedElements()) {
418428
if ( hasAnnotation(element, "Id")|| hasAnnotation(element, "EmbeddedId") ) {
419429
return factory.propertyType(element, getEntityName(), EntityIdentifierMapping.ID_ROLE_NAME, defaultAccessType);
@@ -490,7 +500,8 @@ String qualifyName(String jpaEntityName) {
490500
boolean isAttributeDefined(String entityName, String fieldName) {
491501
final TypeElement entityClass = findEntityClass(entityName);
492502
return entityClass != null
493-
&& findPropertyByPath(entityClass, fieldName, getDefaultAccessType(entityClass)) != null;
503+
&& (findPropertyByPath(entityClass, fieldName, getDefaultAccessType(entityClass)) != null
504+
|| "id".equals( fieldName ) && hasAnnotation( entityClass, "IdClass" ));
494505
}
495506

496507
public TypeElement findEntityClass(String entityName) {

0 commit comments

Comments
 (0)