Author: epbernard
Date: 2008-03-14 11:50:34 -0400 (Fri, 14 Mar 2008)
New Revision: 14425
Added:
annotations/trunk/src/test/org/hibernate/test/annotations/collectionelement/Matrix.java
Modified:
annotations/trunk/src/java/org/hibernate/annotations/MapKey.java
annotations/trunk/src/java/org/hibernate/cfg/annotations/MapBinder.java
annotations/trunk/src/test/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java
Log:
ANN-696 ANN-709 Support @Type on map key
Modified: annotations/trunk/src/java/org/hibernate/annotations/MapKey.java
===================================================================
--- annotations/trunk/src/java/org/hibernate/annotations/MapKey.java 2008-03-14 11:53:11
UTC (rev 14424)
+++ annotations/trunk/src/java/org/hibernate/annotations/MapKey.java 2008-03-14 15:50:34
UTC (rev 14425)
@@ -24,4 +24,9 @@
* Only useful if the collection does not use generics
*/
Class targetElement() default void.class;
+
+ /**
+ * The optional map key type. Guessed if default
+ */
+ Type type() default @Type(type = "");
}
Modified: annotations/trunk/src/java/org/hibernate/cfg/annotations/MapBinder.java
===================================================================
--- annotations/trunk/src/java/org/hibernate/cfg/annotations/MapBinder.java 2008-03-14
11:53:11 UTC (rev 14424)
+++ annotations/trunk/src/java/org/hibernate/cfg/annotations/MapBinder.java 2008-03-14
15:50:34 UTC (rev 14425)
@@ -13,6 +13,7 @@
import org.hibernate.FetchMode;
import org.hibernate.MappingException;
import org.hibernate.annotations.MapKeyManyToMany;
+import org.hibernate.annotations.MapKey;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.cfg.AnnotatedClassType;
@@ -232,7 +233,12 @@
column.setTable( mapValue.getCollectionTable() );
}
elementBinder.setColumns( elementColumns );
- elementBinder.setType( property, elementClass );
+ //do not call setType as it extract the type from @Type
+ //the algorithm generally does not apply for map key anyway
+ MapKey mapKeyAnn = property.getAnnotation( org.hibernate.annotations.MapKey.class
);
+ if (mapKeyAnn != null && ! BinderHelper.isDefault( mapKeyAnn.type().type() )
) {
+ elementBinder.setExplicitType( mapKeyAnn.type() );
+ }
mapValue.setIndex( elementBinder.make() );
}
}
Modified:
annotations/trunk/src/test/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java
===================================================================
---
annotations/trunk/src/test/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java 2008-03-14
11:53:11 UTC (rev 14424)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java 2008-03-14
15:50:34 UTC (rev 14425)
@@ -188,12 +188,27 @@
s.close();
}
+ public void testMapKeyType() throws Exception {
+ Matrix m = new Matrix();
+ m.getValues().put( 1, 1.1f );
+ Session s = openSession();
+ Transaction tx = s.beginTransaction();
+ s.persist( m );
+ s.flush();
+ s.clear();
+ m = (Matrix) s.get( Matrix.class, m.getId() );
+ assertEquals( 1.1f, m.getValues().get( 1 ) );
+ tx.rollback();
+ s.close();
+ }
+
protected Class[] getMappings() {
return new Class[] {
Boy.class,
Country.class,
- TestCourse.class
+ TestCourse.class,
+ Matrix.class
};
}
}
Added:
annotations/trunk/src/test/org/hibernate/test/annotations/collectionelement/Matrix.java
===================================================================
---
annotations/trunk/src/test/org/hibernate/test/annotations/collectionelement/Matrix.java
(rev 0)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/collectionelement/Matrix.java 2008-03-14
15:50:34 UTC (rev 14425)
@@ -0,0 +1,42 @@
+//$
+package org.hibernate.test.annotations.collectionelement;
+
+import java.util.Map;
+import java.util.HashMap;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+
+import org.hibernate.annotations.MapKey;
+import org.hibernate.annotations.CollectionOfElements;
+import org.hibernate.annotations.Type;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+public class Matrix {
+ @Id
+ @GeneratedValue
+ private Integer id;
+ @MapKey(type = @Type(type="integer") )
+ @CollectionOfElements
+ @Type(type = "float")
+ private Map<Integer, Float> values = new HashMap<Integer, Float>();
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public Map<Integer, Float> getValues() {
+ return values;
+ }
+
+ public void setValues(Map<Integer, Float> values) {
+ this.values = values;
+ }
+}