diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index 17f55db158..0db8176a2f 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -1242,7 +1242,7 @@ public final class AnnotationBinder { else { if ( clazzToProcess.isAnnotationPresent( PrimaryKeyJoinColumns.class ) || clazzToProcess.isAnnotationPresent( PrimaryKeyJoinColumn.class ) ) { - LOG.invalidPrimaryKeyJoinColumnAnnotation(); + LOG.invalidPrimaryKeyJoinColumnAnnotation( clazzToProcess.getName() ); } } return inheritanceJoinedColumns; diff --git a/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java b/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java index 553f1a5b8e..91dc9d0210 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java @@ -445,8 +445,8 @@ public interface CoreMessageLogger extends BasicLogger { void invalidOnDeleteAnnotation(String entityName); @LogMessage(level = WARN) - @Message(value = "Root entity should not hold an PrimaryKeyJoinColum(s), will be ignored", id = 137) - void invalidPrimaryKeyJoinColumnAnnotation(); + @Message(value = "Root entity should not hold an PrimaryKeyJoinColum(s), will be ignored: %s", id = 137) + void invalidPrimaryKeyJoinColumnAnnotation(String className); @LogMessage(level = WARN) @Message(value = "Mixing inheritance strategy in a entity hierarchy is not allowed, ignoring sub strategy in: %s", diff --git a/hibernate-core/src/test/java/org/hibernate/test/cfg/AnnotationBinderTest.java b/hibernate-core/src/test/java/org/hibernate/test/cfg/AnnotationBinderTest.java new file mode 100644 index 0000000000..471ba21026 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/cfg/AnnotationBinderTest.java @@ -0,0 +1,68 @@ +/* + * 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 . + */ +package org.hibernate.test.cfg; + +import static org.junit.Assert.assertTrue; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.PrimaryKeyJoinColumn; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.AnnotationBinder; +import org.hibernate.internal.CoreMessageLogger; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.logger.LoggerInspectionRule; +import org.hibernate.testing.logger.Triggerable; +import org.jboss.logging.Logger; +import org.junit.Rule; +import org.junit.Test; + +/** + * @author Dominique Toupin + */ +@TestForIssue(jiraKey = "HHH-10456") +public class AnnotationBinderTest { + + @Rule + public LoggerInspectionRule logInspection = new LoggerInspectionRule( + Logger.getMessageLogger( CoreMessageLogger.class, AnnotationBinder.class.getName() ) ); + + @Test + public void testInvalidPrimaryKeyJoinColumnAnnotationMessageContainsClassName() throws Exception { + + Triggerable triggerable = logInspection.watchForLogMessages( "HHH000137" ); + + StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder(); + + Metadata metadata = new MetadataSources( srb.build() ) + .addAnnotatedClass( InvalidPrimaryKeyJoinColumnAnnotationEntity.class ) + .buildMetadata(); + + assertTrue( "Expected warning HHH00137 but it wasn't triggered", triggerable.wasTriggered() ); + assertTrue( "Expected invalid class name in warning HHH00137 message but it does not apper to be present; got " + triggerable.triggerMessage(), + triggerable.triggerMessage().matches( ".*\\b\\Q" + InvalidPrimaryKeyJoinColumnAnnotationEntity.class.getName() + "\\E\\b.*" ) ); + } + + @Entity + @PrimaryKeyJoinColumn + public static class InvalidPrimaryKeyJoinColumnAnnotationEntity { + + private String id; + + @Id + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + } + +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/logger/TriggerOnPrefixLogListener.java b/hibernate-testing/src/main/java/org/hibernate/testing/logger/TriggerOnPrefixLogListener.java index 675d02a2c4..72bdcbe332 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/logger/TriggerOnPrefixLogListener.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/logger/TriggerOnPrefixLogListener.java @@ -7,6 +7,7 @@ package org.hibernate.testing.logger; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; import org.jboss.logging.Logger.Level; @@ -14,6 +15,7 @@ final class TriggerOnPrefixLogListener implements LogListener, Triggerable { private final String expectedPrefix; private final AtomicBoolean triggered = new AtomicBoolean( false ); + private final AtomicReference triggerMessage = new AtomicReference( null ); public TriggerOnPrefixLogListener(String expectedPrefix) { this.expectedPrefix = expectedPrefix; @@ -23,9 +25,15 @@ final class TriggerOnPrefixLogListener implements LogListener, Triggerable { public void loggedEvent(Level level, String renderedMessage, Throwable thrown) { if ( renderedMessage != null && renderedMessage.startsWith( expectedPrefix ) ) { triggered.set( true ); + triggerMessage.set(renderedMessage); } } + @Override + public String triggerMessage() { + return triggerMessage.get(); + } + @Override public boolean wasTriggered() { return triggered.get(); @@ -34,6 +42,6 @@ final class TriggerOnPrefixLogListener implements LogListener, Triggerable { @Override public void reset() { triggered.set( false ); + triggerMessage.set(null); } - } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/logger/Triggerable.java b/hibernate-testing/src/main/java/org/hibernate/testing/logger/Triggerable.java index 4497ff8e85..3cbd0b03e6 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/logger/Triggerable.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/logger/Triggerable.java @@ -8,8 +8,9 @@ package org.hibernate.testing.logger; public interface Triggerable { + String triggerMessage(); + boolean wasTriggered(); void reset(); - }