[hibernate-commits] Hibernate SVN: r18428 - in core/trunk/annotations/src: test/java/org/hibernate/test/annotations/collectionelement and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Jan 7 08:56:47 EST 2010


Author: epbernard
Date: 2010-01-07 08:56:47 -0500 (Thu, 07 Jan 2010)
New Revision: 18428

Modified:
   core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.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
   core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java
Log:
HHH-4353 support JPA 2 default column naming for collections of basic types (and still honor the legacy approach)

Modified: core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
--- core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java	2010-01-06 18:49:52 UTC (rev 18427)
+++ core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java	2010-01-07 13:56:47 UTC (rev 18428)
@@ -1552,7 +1552,9 @@
 			collectionBinder.setAccessType( inferredData.getDefaultAccess() );
 
 			Ejb3Column[] elementColumns;
-			PropertyData virtualProperty = new WrappedInferredData( inferredData, "element" );
+			//do not use "element" if you are a JPA 2 @ElementCollection only for legacy Hibernate mappings
+			boolean isJPA2ForValueMapping = property.isAnnotationPresent( ElementCollection.class );
+			PropertyData virtualProperty = isJPA2ForValueMapping ? inferredData : new WrappedInferredData( inferredData, "element" );
 			if ( property.isAnnotationPresent( Column.class ) || property.isAnnotationPresent(
 					Formula.class
 			) ) {

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-06 18:49:52 UTC (rev 18427)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java	2010-01-07 13:56:47 UTC (rev 18428)
@@ -36,7 +36,10 @@
 	private String firstName;
 	private String lastName;
 	private Set<String> nickNames = new HashSet<String>();
+	private Set<String> hatedNames = new HashSet<String>();
+	private Set<String> preferredNames = new HashSet<String>();
 	private Map<String, Integer> scorePerNickName = new HashMap<String, Integer>();
+	private Map<String, Integer> scorePerPreferredName = new HashMap<String, Integer>();
 	private int[] favoriteNumbers;
 	private Set<Toy> favoriteToys = new HashSet<Toy>();
 	private Set<Character> characters = new HashSet<Character>();
@@ -78,6 +81,34 @@
 		this.nickNames = nickName;
 	}
 
+	@ElementCollection //default column names
+	public Set<String> getHatedNames() {
+		return hatedNames;
+	}
+
+	public void setHatedNames(Set<String> hatedNames) {
+		this.hatedNames = hatedNames;
+	}
+
+	@ElementCollection //default column names
+	@Column
+	public Set<String> getPreferredNames() {
+		return preferredNames;
+	}
+
+	public void setPreferredNames(Set<String> preferredNames) {
+		this.preferredNames = preferredNames;
+	}
+
+	@ElementCollection	
+	public Map<String, Integer> getScorePerPreferredName() {
+		return scorePerPreferredName;
+	}
+
+	public void setScorePerPreferredName(Map<String, Integer> scorePerPreferredName) {
+		this.scorePerPreferredName = scorePerPreferredName;
+	}
+
 	@ElementCollection
 	@CollectionTable(name = "ScorePerNickName", joinColumns = @JoinColumn(name = "BoyId"))
 	@Column(name = "score", nullable = false)

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-06 18:49:52 UTC (rev 18427)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java	2010-01-07 13:56:47 UTC (rev 18428)
@@ -2,6 +2,7 @@
 package org.hibernate.test.annotations.collectionelement;
 
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 
@@ -9,8 +10,11 @@
 import org.hibernate.Query;
 import org.hibernate.Session;
 import org.hibernate.Transaction;
+import org.hibernate.mapping.Collection;
+import org.hibernate.mapping.Column;
 import org.hibernate.test.annotations.Country;
 import org.hibernate.test.annotations.TestCase;
+import org.hibernate.util.StringHelper;
 
 /**
  * @author Emmanuel Bernard
@@ -212,6 +216,32 @@
 		s.close();
 	}
 
+	public void testDefaultValueColumnForBasic() throws Exception {
+		isDefaultValueCollectionColumnPresent( Boy.class.getName(), "hatedNames" );
+		isDefaultValueCollectionColumnPresent( Boy.class.getName(), "preferredNames" );
+		isValueCollectionColumnPresent( Boy.class.getName(), "nickNames", "element" );
+		isDefaultValueCollectionColumnPresent( Boy.class.getName(), "scorePerPreferredName");
+	}
+
+	private void isLegacyValueCollectionColumnPresent(String collectionHolder, String propertyName) {
+
+	}
+
+	private void isDefaultValueCollectionColumnPresent(String collectionOwner, String propertyName) {
+		isValueCollectionColumnPresent( collectionOwner, propertyName, propertyName );
+	}
+
+	private void isValueCollectionColumnPresent(String collectionOwner, String propertyName, String columnName) {
+		final Collection collection = getCfg().getCollectionMapping( collectionOwner + "." + propertyName );
+		final Iterator columnIterator = collection.getCollectionTable().getColumnIterator();
+		boolean hasDefault = false;
+		while ( columnIterator.hasNext() ) {
+			Column column = (Column) columnIterator.next();
+			if ( columnName.equals( column.getName() ) ) hasDefault = true;
+		}
+		assertTrue( "Could not find " + columnName, hasDefault );
+	}
+
 	protected Class[] getMappings() {
 		return new Class[] {
 				Boy.class,

Modified: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java	2010-01-06 18:49:52 UTC (rev 18427)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java	2010-01-07 13:56:47 UTC (rev 18428)
@@ -33,13 +33,13 @@
 		isDefaultKeyColumnPresent( Drawer.class.getName(), "dresses", "_ORDER" );
 	}
 
-	private void isDefaultKeyColumnPresent(String collectionRole, String propertyName, String suffix) {
+	private void isDefaultKeyColumnPresent(String collectionOwner, String propertyName, String suffix) {
 		assertTrue( "Could not find " + propertyName + suffix,
-				isDefaultColumnPresent(collectionRole, propertyName, suffix) );
+				isDefaultColumnPresent(collectionOwner, propertyName, suffix) );
 	}
 
-	private boolean isDefaultColumnPresent(String collectionRole, String propertyName, String suffix) {
-		final Collection collection = getCfg().getCollectionMapping( collectionRole + "." + propertyName );
+	private boolean isDefaultColumnPresent(String collectionOwner, String propertyName, String suffix) {
+		final Collection collection = getCfg().getCollectionMapping( collectionOwner + "." + propertyName );
 		final Iterator columnIterator = collection.getCollectionTable().getColumnIterator();
 		boolean hasDefault = false;
 		while ( columnIterator.hasNext() ) {
@@ -49,9 +49,9 @@
 		return hasDefault;
 	}
 
-	private void isNotDefaultKeyColumnPresent(String collectionRole, String propertyName, String suffix) {
+	private void isNotDefaultKeyColumnPresent(String collectionOwner, String propertyName, String suffix) {
 		assertFalse( "Could not find " + propertyName + suffix,
-				isDefaultColumnPresent(collectionRole, propertyName, suffix) );
+				isDefaultColumnPresent(collectionOwner, propertyName, suffix) );
 	}
 
 	public void testFkList() throws Exception {



More information about the hibernate-commits mailing list