diff --git a/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java b/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java index a4f7769a71..74a6d1ec6a 100644 --- a/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java +++ b/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java @@ -35,6 +35,7 @@ import javax.persistence.AttributeOverrides; import javax.persistence.ElementCollection; import javax.persistence.Embeddable; import javax.persistence.FetchType; +import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.MapKey; @@ -464,6 +465,15 @@ public abstract class CollectionBinder { //work on association boolean isMappedBy = !BinderHelper.isDefault( mappedBy ); + + if (isMappedBy + && (property.isAnnotationPresent( JoinColumn.class ) + || property.isAnnotationPresent( JoinTable.class ) ) ) { + String message = "Associations marked as mappedBy must not define database mappings like @JoinTable or @JoinColumn: "; + message += StringHelper.qualify( propertyHolder.getPath(), propertyName ); + throw new AnnotationException( message ); + } + collection.setInverse( isMappedBy ); //many to many may need some second pass informations @@ -487,7 +497,7 @@ public abstract class CollectionBinder { || property.isAnnotationPresent( CollectionOfElements.class ) //legacy hibernate || property.isAnnotationPresent( ElementCollection.class ) //JPA 2 ) { - // do it right away, otherwise @ManyToon on composite element call addSecondPass + // do it right away, otherwise @ManyToOne on composite element call addSecondPass // and raise a ConcurrentModificationException //sp.doSecondPass( CollectionHelper.EMPTY_MAP ); mappings.addSecondPass( sp, !isMappedBy ); diff --git a/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/AddressBook.java b/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/AddressBook.java index 1f6f0b2e73..975b16effd 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/AddressBook.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/AddressBook.java @@ -42,7 +42,6 @@ public class AddressBook { @MapKey @OneToMany(mappedBy = "book", cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE}) - @JoinTable(name="AddRegEntry") public Map getEntries() { return entries; } @@ -63,7 +62,6 @@ public class AddressBook { @MapKey(name = "directory") @OneToMany(mappedBy = "book") - @JoinTable(name="Dir_Entry") public Map getDirectoryEntries() { return directoryEntries; }