[hibernate-commits] Hibernate SVN: r18433 - 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 12:33:24 EST 2010
Author: epbernard
Date: 2010-01-07 12:33:23 -0500 (Thu, 07 Jan 2010)
New Revision: 18433
Modified:
core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/MapBinder.java
core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Matrix.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-4687 implement @MapKeyTemporal
Modified: core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/MapBinder.java
===================================================================
--- core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/MapBinder.java 2010-01-07 14:51:20 UTC (rev 18432)
+++ core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/MapBinder.java 2010-01-07 17:33:23 UTC (rev 18433)
@@ -271,9 +271,13 @@
//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 );
+ elementBinder.setKey(true);
if (mapKeyAnn != null && ! BinderHelper.isDefault( mapKeyAnn.type().type() ) ) {
elementBinder.setExplicitType( mapKeyAnn.type() );
}
+ else {
+ elementBinder.setType( property, elementClass );
+ }
mapValue.setIndex( elementBinder.make() );
}
}
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 14:51:20 UTC (rev 18432)
+++ core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java 2010-01-07 17:33:23 UTC (rev 18433)
@@ -30,7 +30,9 @@
import java.util.Properties;
import javax.persistence.Enumerated;
import javax.persistence.Lob;
+import javax.persistence.MapKeyTemporal;
import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
@@ -70,6 +72,8 @@
private Table table;
private SimpleValue simpleValue;
private boolean isVersion;
+ //is a Map key
+ private boolean key;
public boolean isVersion() {
return isVersion;
@@ -112,8 +116,9 @@
Properties typeParameters = this.typeParameters;
typeParameters.clear();
String type = BinderHelper.ANNOTATION_STRING_DEFAULT;
- if ( property.isAnnotationPresent( Temporal.class ) ) {
- Temporal ann = property.getAnnotation( Temporal.class );
+ if ( (!key && property.isAnnotationPresent( Temporal.class ) )
+ || (key && property.isAnnotationPresent( MapKeyTemporal.class ) )) {
+
boolean isDate;
if ( mappings.getReflectionManager().equals( returnedClassOrElement, Date.class ) ) {
isDate = true;
@@ -127,8 +132,8 @@
+ StringHelper.qualify( persistentClassName, propertyName )
);
}
-
- switch ( ann.value() ) {
+ final TemporalType temporalType = getTemporalType( property );
+ switch ( temporalType ) {
case DATE:
type = isDate ? "date" : "calendar_date";
break;
@@ -145,7 +150,7 @@
type = isDate ? "timestamp" : "calendar";
break;
default:
- throw new AssertionFailure( "Unknown temporal type: " + ann.value() );
+ throw new AssertionFailure( "Unknown temporal type: " + temporalType );
}
}
else if ( property.isAnnotationPresent( Lob.class ) ) {
@@ -219,10 +224,21 @@
}
explicitType = type;
this.typeParameters = typeParameters;
- Type annType = (Type) property.getAnnotation( Type.class );
+ Type annType = property.getAnnotation( Type.class );
setExplicitType( annType );
}
+ private TemporalType getTemporalType(XProperty property) {
+ if (key) {
+ MapKeyTemporal ann = property.getAnnotation( MapKeyTemporal.class );
+ return ann.value();
+ }
+ else {
+ Temporal ann = property.getAnnotation( Temporal.class );
+ return ann.value();
+ }
+ }
+
public void setExplicitType(String explicitType) {
this.explicitType = explicitType;
}
@@ -297,4 +313,8 @@
}
}
+
+ public void setKey(boolean key) {
+ this.key = key;
+ }
}
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-07 14:51:20 UTC (rev 18432)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java 2010-01-07 17:33:23 UTC (rev 18433)
@@ -14,7 +14,6 @@
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
@@ -204,14 +203,14 @@
public void testMapKeyType() throws Exception {
Matrix m = new Matrix();
- m.getValues().put( 1, 1.1f );
+ m.getMvalues().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 ) );
+ assertEquals( 1.1f, m.getMvalues().get( 1 ) );
tx.rollback();
s.close();
}
Modified: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Matrix.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Matrix.java 2010-01-07 14:51:20 UTC (rev 18432)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Matrix.java 2010-01-07 17:33:23 UTC (rev 18433)
@@ -27,7 +27,7 @@
@ElementCollection
@Sort(type = SortType.NATURAL)
@Type(type = "float")
- private SortedMap<Integer, Float> values = new TreeMap<Integer, Float>();
+ private SortedMap<Integer, Float> mvalues = new TreeMap<Integer, Float>();
public Integer getId() {
return id;
@@ -37,11 +37,11 @@
this.id = id;
}
- public Map<Integer, Float> getValues() {
- return values;
+ public Map<Integer, Float> getMvalues() {
+ return mvalues;
}
- public void setValues(SortedMap<Integer, Float> values) {
- this.values = values;
+ public void setMvalues(SortedMap<Integer, Float> mValues) {
+ this.mvalues = mValues;
}
}
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 14:51:20 UTC (rev 18432)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/Atmosphere.java 2010-01-07 17:33:23 UTC (rev 18433)
@@ -1,9 +1,11 @@
//$Id$
package org.hibernate.test.annotations.indexcoll;
+import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.CascadeType;
+import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@@ -13,6 +15,8 @@
import javax.persistence.JoinColumn;
import javax.persistence.MapKeyColumn;
import javax.persistence.MapKeyJoinColumn;
+import javax.persistence.MapKeyTemporal;
+import javax.persistence.TemporalType;
import org.hibernate.annotations.MapKey;
import org.hibernate.annotations.CollectionOfElements;
@@ -30,6 +34,10 @@
@MapKeyColumn(name="gas_name")
public Map<String, Gas> gases = new HashMap<String, Gas>();
+ @MapKeyTemporal(TemporalType.DATE)
+ @ElementCollection
+ public Map<Date, String> colorPerDate = new HashMap<Date,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 14:51:20 UTC (rev 18432)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java 2010-01-07 17:33:23 UTC (rev 18433)
@@ -2,6 +2,7 @@
package org.hibernate.test.annotations.indexcoll;
import java.util.ArrayList;
+import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -442,6 +443,25 @@
s.close();
}
+ public void testTemporalKeyMap() throws Exception {
+ Session s = openSession();
+ Transaction tx = s.beginTransaction();
+ Atmosphere atm = new Atmosphere();
+ atm.colorPerDate.put( new Date(1234567000), "red" );
+ s.persist( atm );
+
+ s.flush();
+ s.clear();
+
+ atm = (Atmosphere) s.get( Atmosphere.class, atm.id );
+ assertEquals( 1, atm.colorPerDate.size() );
+ final Date date = atm.colorPerDate.keySet().iterator().next();
+ final long diff = new Date( 1234567000 ).getTime() - date.getTime();
+ assertTrue( "24h diff max", diff > 0 && diff < 24*60*60*1000 );
+ tx.rollback();
+ s.close();
+ }
+
public void testMapKeyEntityEntity() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
More information about the hibernate-commits
mailing list