diff --git a/annotations/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java b/annotations/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java index 67fe3da634..c4c666a692 100644 --- a/annotations/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java +++ b/annotations/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java @@ -62,6 +62,11 @@ public class Ejb3JoinColumn extends Ejb3Column { //table name on the mapped by side if any private String mappedByTableName; private String mappedByEntityName; + private boolean JPA2ElementCollection; + + public void setJPA2ElementCollection(boolean JPA2ElementCollection) { + this.JPA2ElementCollection = JPA2ElementCollection; + } //FIXME hacky solution to get the information at property ref resolution public String getManyToManyOwnerSideEntityName() { @@ -398,10 +403,12 @@ public class Ejb3JoinColumn extends Ejb3Column { if ( mappedBySide ) { String unquotedMappedbyTable = StringHelper.unquote( mappedByTableName ); + final String ownerObjectName = JPA2ElementCollection && mappedByEntityName != null ? + StringHelper.unqualify( mappedByEntityName ) : unquotedMappedbyTable; columnName = getMappings().getNamingStrategy().foreignKeyColumnName( mappedByPropertyName, mappedByEntityName, - unquotedMappedbyTable, + ownerObjectName, unquotedLogicalReferenceColumn ); //one element was quoted so we quote 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 1979317334..aeaebf7b80 100644 --- a/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java +++ b/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java @@ -36,6 +36,7 @@ import javax.persistence.ElementCollection; import javax.persistence.Embeddable; import javax.persistence.FetchType; import javax.persistence.JoinColumn; +import javax.persistence.JoinColumns; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.MapKey; @@ -468,6 +469,7 @@ public abstract class CollectionBinder { if (isMappedBy && (property.isAnnotationPresent( JoinColumn.class ) + || property.isAnnotationPresent( JoinColumns.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 ); @@ -1415,6 +1417,9 @@ public abstract class CollectionBinder { joinColumns, collValue.getOwner(), collectionEntity, collValue, false, mappings ); SimpleValue key = buildCollectionKey( collValue, joinColumns, cascadeDeleteEnabled, property, mappings ); + if ( property.isAnnotationPresent( ElementCollection.class ) && joinColumns.length > 0 ) { + joinColumns[0].setJPA2ElementCollection( true ); + } TableBinder.bindFk( collValue.getOwner(), collectionEntity, joinColumns, key, false, mappings ); } diff --git a/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java b/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java index 1211415aee..7268c26f5d 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java @@ -18,6 +18,7 @@ import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OrderColumn; +import javax.persistence.Table; import org.hibernate.annotations.CollectionOfElements; import org.hibernate.test.annotations.collectionelement.FavoriteFood; @@ -31,6 +32,7 @@ import org.hibernate.test.annotations.collectionelement.FavoriteFood; @AttributeOverride( name="scorePerNickName.element", column = @Column(name="fld_score") ), @AttributeOverride( name="favoriteToys.element.brand.surname", column = @Column(name = "fld_surname"))} ) +@Table(name="tbl_Boys") public class Boy { private Integer id; private String firstName; diff --git a/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java b/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java index be83dec7bd..9130302752 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java @@ -218,19 +218,23 @@ public class CollectionElementTest extends TestCase { public void testDefaultValueColumnForBasic() throws Exception { isDefaultValueCollectionColumnPresent( Boy.class.getName(), "hatedNames" ); isDefaultValueCollectionColumnPresent( Boy.class.getName(), "preferredNames" ); - isValueCollectionColumnPresent( Boy.class.getName(), "nickNames", "element" ); + isCollectionColumnPresent( Boy.class.getName(), "nickNames", "element" ); isDefaultValueCollectionColumnPresent( Boy.class.getName(), "scorePerPreferredName"); } + public void testDefaultFKNameForElementCollection() throws Exception { + isCollectionColumnPresent( Boy.class.getName(), "hatedNames", "Boy_id" ); + } + private void isLegacyValueCollectionColumnPresent(String collectionHolder, String propertyName) { } private void isDefaultValueCollectionColumnPresent(String collectionOwner, String propertyName) { - isValueCollectionColumnPresent( collectionOwner, propertyName, propertyName ); + isCollectionColumnPresent( collectionOwner, propertyName, propertyName ); } - private void isValueCollectionColumnPresent(String collectionOwner, String propertyName, String columnName) { + private void isCollectionColumnPresent(String collectionOwner, String propertyName, String columnName) { final Collection collection = getCfg().getCollectionMapping( collectionOwner + "." + propertyName ); final Iterator columnIterator = collection.getCollectionTable().getColumnIterator(); boolean hasDefault = false;