[hibernate-commits] Hibernate SVN: r18455 - in core/trunk/annotations/src: main/java/org/hibernate/cfg/annotations and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Jan 8 11:58:56 EST 2010


Author: epbernard
Date: 2010-01-08 11:58:56 -0500 (Fri, 08 Jan 2010)
New Revision: 18455

Modified:
   core/trunk/annotations/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java
   core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java
   core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java
   core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java
Log:
HHH-4771 @ElementCollection fk column should default to entityName_columnNameOfOwningId

Modified: core/trunk/annotations/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java
===================================================================
--- core/trunk/annotations/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java	2010-01-08 16:39:52 UTC (rev 18454)
+++ core/trunk/annotations/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java	2010-01-08 16:58:56 UTC (rev 18455)
@@ -62,7 +62,12 @@
 	//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() {
 		return manyToManyOwnerSideEntityName;
@@ -398,10 +403,12 @@
 
 		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

Modified: core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java
===================================================================
--- core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java	2010-01-08 16:39:52 UTC (rev 18454)
+++ core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java	2010-01-08 16:58:56 UTC (rev 18455)
@@ -36,6 +36,7 @@
 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 @@
 
 		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 @@
 				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 );
 	}
 

Modified: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java	2010-01-08 16:39:52 UTC (rev 18454)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java	2010-01-08 16:58:56 UTC (rev 18455)
@@ -18,6 +18,7 @@
 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 @@
 		@AttributeOverride( name="scorePerNickName.element", column = @Column(name="fld_score") ),
 		@AttributeOverride( name="favoriteToys.element.brand.surname", column = @Column(name = "fld_surname"))}
 )
+ at Table(name="tbl_Boys")
 public class Boy {
 	private Integer id;
 	private String firstName;

Modified: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java	2010-01-08 16:39:52 UTC (rev 18454)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java	2010-01-08 16:58:56 UTC (rev 18455)
@@ -218,19 +218,23 @@
 	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;



More information about the hibernate-commits mailing list