[hibernate-commits] Hibernate SVN: r18435 - in core/trunk/annotations/src: test/java/org/hibernate/test/annotations/indexcoll and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Jan 7 12:45:29 EST 2010


Author: epbernard
Date: 2010-01-07 12:45:29 -0500 (Thu, 07 Jan 2010)
New Revision: 18435

Modified:
   core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java
   core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/Atmosphere.java
   core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java
Log:
HHH-4686 Implement @MapKeyEnumerated

Modified: core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java
===================================================================
--- core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java	2010-01-07 17:41:05 UTC (rev 18434)
+++ core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java	2010-01-07 17:45:29 UTC (rev 18435)
@@ -30,6 +30,7 @@
 import java.util.Properties;
 import javax.persistence.Enumerated;
 import javax.persistence.Lob;
+import javax.persistence.MapKeyEnumerated;
 import javax.persistence.MapKeyTemporal;
 import javax.persistence.Temporal;
 import javax.persistence.TemporalType;
@@ -207,9 +208,8 @@
 				typeParameters.setProperty( EnumType.CATALOG, catalog );
 				typeParameters.setProperty( EnumType.TABLE, columns[0].getTable().getName() );
 				typeParameters.setProperty( EnumType.COLUMN, columns[0].getName() );
-				Enumerated enumAnn = property.getAnnotation( Enumerated.class );
-				if ( enumAnn != null ) {
-					javax.persistence.EnumType enumType = enumAnn.value();
+				javax.persistence.EnumType enumType = getEnumType( property );
+				if ( enumType != null ) {
 					if ( javax.persistence.EnumType.ORDINAL.equals( enumType ) ) {
 						typeParameters.setProperty( EnumType.TYPE, String.valueOf( Types.INTEGER ) );
 					}
@@ -228,6 +228,23 @@
 		setExplicitType( annType );
 	}
 
+	private javax.persistence.EnumType getEnumType(XProperty property) {
+		javax.persistence.EnumType enumType = null;
+		if (key) {
+			MapKeyEnumerated enumAnn = property.getAnnotation( MapKeyEnumerated.class );
+			if ( enumAnn != null ) {
+				enumType = enumAnn.value();
+			}
+		}
+		else {
+			Enumerated enumAnn = property.getAnnotation( Enumerated.class );
+			if ( enumAnn != null ) {
+				enumType = enumAnn.value();
+			}
+		}
+		return enumType;
+	}
+
 	private TemporalType getTemporalType(XProperty property) {
 		if (key) {
 			MapKeyTemporal ann = property.getAnnotation( MapKeyTemporal.class );

Modified: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/Atmosphere.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/Atmosphere.java	2010-01-07 17:41:05 UTC (rev 18434)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/Atmosphere.java	2010-01-07 17:45:29 UTC (rev 18435)
@@ -7,6 +7,7 @@
 import javax.persistence.CascadeType;
 import javax.persistence.ElementCollection;
 import javax.persistence.Entity;
+import javax.persistence.EnumType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
 import javax.persistence.ManyToMany;
@@ -14,6 +15,7 @@
 import javax.persistence.JoinTable;
 import javax.persistence.JoinColumn;
 import javax.persistence.MapKeyColumn;
+import javax.persistence.MapKeyEnumerated;
 import javax.persistence.MapKeyJoinColumn;
 import javax.persistence.MapKeyTemporal;
 import javax.persistence.TemporalType;
@@ -26,6 +28,12 @@
  */
 @Entity
 public class Atmosphere {
+
+	public static enum Level {
+		LOW,
+		HIGH
+	}
+
 	@Id
 	@GeneratedValue
 	public Integer id;
@@ -38,6 +46,10 @@
 	@ElementCollection
 	public Map<Date, String> colorPerDate = new HashMap<Date,String>();
 
+	@ElementCollection
+	@MapKeyEnumerated(EnumType.STRING)
+	public Map<Level, String> colorPerLevel = new HashMap<Level,String>();
+
 	@ManyToMany(cascade = CascadeType.ALL)
 	@MapKeyJoinColumn(name="gas_id" )
 	@JoinTable(name = "Gas_per_key")

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-07 17:41:05 UTC (rev 18434)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java	2010-01-07 17:45:29 UTC (rev 18435)
@@ -462,6 +462,23 @@
 		s.close();
 	}
 
+	public void testEnumKeyType() throws Exception {
+		Session s = openSession();
+		Transaction tx = s.beginTransaction();
+		Atmosphere atm = new Atmosphere();
+		atm.colorPerLevel.put( Atmosphere.Level.HIGH, "red" );
+		s.persist( atm );
+
+		s.flush();
+		s.clear();
+
+		atm = (Atmosphere) s.get( Atmosphere.class, atm.id );
+		assertEquals( 1, atm.colorPerLevel.size() );
+		assertEquals( "red", atm.colorPerLevel.get(Atmosphere.Level.HIGH) );
+		tx.rollback();
+		s.close();
+	}
+
 	public void testMapKeyEntityEntity() throws Exception {
 		Session s = openSession();
 		Transaction tx = s.beginTransaction();



More information about the hibernate-commits mailing list