Skip to content

Commit deea4ad

Browse files
committed
HHH-10242 - Deal with HCANN poor handling of multiple properties by matching stem name
(cherry picked from commit ae9ff55)
1 parent 3280608 commit deea4ad

File tree

4 files changed

+100
-8
lines changed

4 files changed

+100
-8
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/PropertyContainer.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
import org.hibernate.annotations.Type;
2929
import org.hibernate.annotations.common.reflection.XClass;
3030
import org.hibernate.annotations.common.reflection.XProperty;
31+
import org.hibernate.annotations.common.reflection.java.JavaXMember;
32+
import org.hibernate.boot.jaxb.Origin;
33+
import org.hibernate.boot.jaxb.SourceType;
34+
import org.hibernate.cfg.annotations.HCANNHelper;
3135
import org.hibernate.internal.CoreMessageLogger;
3236
import org.hibernate.internal.util.StringHelper;
3337

@@ -188,7 +192,14 @@ private TreeMap<String, XProperty> initProperties(AccessType access) {
188192
// HHH-10242 detect registration of the same property twice eg boolean isId() + UUID getId()
189193
XProperty oldProperty = propertiesMap.get( property.getName() );
190194
if ( oldProperty != null ) {
191-
throw LOG.throwAmbiguousPropertyException( this.xClass, oldProperty.getName(), oldProperty.getType(), property.getType() );
195+
throw new org.hibernate.boot.MappingException(
196+
LOG.ambiguousPropertyMethods(
197+
xClass.getName(),
198+
HCANNHelper.annotatedElementSignature( oldProperty ),
199+
HCANNHelper.annotatedElementSignature( property )
200+
),
201+
new Origin( SourceType.ANNOTATION, xClass.getName() )
202+
);
192203
}
193204

194205
propertiesMap.put( property.getName(), property );
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.cfg.annotations;
8+
9+
import java.lang.reflect.AnnotatedElement;
10+
import java.lang.reflect.InvocationTargetException;
11+
import java.lang.reflect.Member;
12+
import java.lang.reflect.Method;
13+
14+
import org.hibernate.AssertionFailure;
15+
import org.hibernate.annotations.common.reflection.XProperty;
16+
import org.hibernate.annotations.common.reflection.java.JavaXMember;
17+
18+
/**
19+
* Manage the various fun-ness of dealing with HCANN...
20+
*
21+
* @author Steve Ebersole
22+
*/
23+
public class HCANNHelper {
24+
private static Class javaXMemberClass = JavaXMember.class;
25+
private static Method getMemberMethod;
26+
27+
public static String annotatedElementSignature(XProperty xProperty) {
28+
if ( getMemberMethod == null ) {
29+
resolveGetMemberMethod();
30+
}
31+
32+
return getUnderlyingMember( xProperty ).toString();
33+
}
34+
35+
public static Member getUnderlyingMember(XProperty xProperty) {
36+
if ( getMemberMethod == null ) {
37+
resolveGetMemberMethod();
38+
}
39+
40+
try {
41+
return (Member) getMemberMethod.invoke( xProperty );
42+
}
43+
catch (IllegalAccessException e) {
44+
throw new AssertionFailure(
45+
"Could not resolve member signature from XProperty reference",
46+
e
47+
);
48+
}
49+
catch (InvocationTargetException e) {
50+
throw new AssertionFailure(
51+
"Could not resolve member signature from XProperty reference",
52+
e.getCause()
53+
);
54+
}
55+
}
56+
57+
@SuppressWarnings("unchecked")
58+
private static void resolveGetMemberMethod() {
59+
try {
60+
getMemberMethod = javaXMemberClass.getDeclaredMethod( "getMember" );
61+
getMemberMethod.setAccessible( true );
62+
}
63+
catch (NoSuchMethodException e) {
64+
throw new AssertionFailure(
65+
"Could not resolve JavaXAnnotatedElement#toAnnotatedElement method in order to access XProperty member signature",
66+
e
67+
);
68+
}
69+
}
70+
}

hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,9 @@ void cannotResolveNonNullableTransientDependencies(
17531753
@Message(value = "Omitting cached file [%s] as the mapping file is newer", id = 473)
17541754
void cachedFileObsolete(File cachedFile);
17551755

1756-
@Message(value = "Ambiguous property detected %s.%s (of types %s and %s). Mark one as @Transient.", id = 474)
1757-
HibernateException throwAmbiguousPropertyException(XClass entity, String propertyName, XClass firstPropertyType, XClass secondPropertyType);
1756+
@Message(
1757+
value = "Ambiguous persistent property methods detected on %s; mark one as @Transient : [%s] and [%s]",
1758+
id = 474
1759+
)
1760+
String ambiguousPropertyMethods(String entityName, String oneMethodSig, String secondMethodSig);
17581761
}

hibernate-core/src/test/java/org/hibernate/property/GetAndIsVariantGetterTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.junit.BeforeClass;
2020
import org.junit.Test;
2121

22+
import static org.hamcrest.CoreMatchers.startsWith;
23+
import static org.junit.Assert.assertThat;
2224
import static org.junit.Assert.fail;
2325

2426
/**
@@ -50,17 +52,23 @@ public void testHbmXml() {
5052
.buildMetadata();
5153
fail( "Expecting a failure" );
5254
}
53-
catch (MappingException ignore) {
54-
// expected
55+
catch (MappingException e) {
56+
assertThat( e.getMessage(), startsWith( "In trying to locate getter for property [id]" ) );
5557
}
5658
}
5759

5860
@Test
5961
@TestForIssue( jiraKey = "HHH-10172" )
6062
public void testAnnotations() {
61-
new MetadataSources( ssr )
62-
.addAnnotatedClass( TheEntity.class )
63-
.buildMetadata();
63+
try {
64+
new MetadataSources( ssr )
65+
.addAnnotatedClass( TheEntity.class )
66+
.buildMetadata();
67+
fail( "Expecting a failure" );
68+
}
69+
catch (MappingException e) {
70+
assertThat( e.getMessage(), startsWith( "HHH000474: Ambiguous persistent property methods detected on" ) );
71+
}
6472
}
6573

6674
@Entity

0 commit comments

Comments
 (0)