From a96f7bf7297fc0f7a7f5544db340d8e110d634b0 Mon Sep 17 00:00:00 2001 From: Emmanuel Bernard Date: Thu, 7 Jan 2010 17:33:23 +0000 Subject: [PATCH] HHH-4687 implement @MapKeyTemporal git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18433 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../hibernate/cfg/annotations/MapBinder.java | 4 +++ .../cfg/annotations/SimpleValueBinder.java | 32 +++++++++++++++---- .../CollectionElementTest.java | 5 ++- .../annotations/collectionelement/Matrix.java | 10 +++--- .../annotations/indexcoll/Atmosphere.java | 8 +++++ .../indexcoll/IndexedCollectionTest.java | 20 ++++++++++++ 6 files changed, 65 insertions(+), 14 deletions(-) diff --git a/annotations/src/main/java/org/hibernate/cfg/annotations/MapBinder.java b/annotations/src/main/java/org/hibernate/cfg/annotations/MapBinder.java index 884bdf13d0..11fc19a07a 100644 --- a/annotations/src/main/java/org/hibernate/cfg/annotations/MapBinder.java +++ b/annotations/src/main/java/org/hibernate/cfg/annotations/MapBinder.java @@ -271,9 +271,13 @@ public class MapBinder extends CollectionBinder { //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() ); } } diff --git a/annotations/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java b/annotations/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java index d8df892699..6e0e89c8d8 100644 --- a/annotations/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java +++ b/annotations/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java @@ -30,7 +30,9 @@ import java.util.Date; 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 @@ public class SimpleValueBinder { 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 @@ public class SimpleValueBinder { 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 @@ public class SimpleValueBinder { + 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 @@ public class SimpleValueBinder { 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 @@ public class SimpleValueBinder { } 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 class SimpleValueBinder { } } + + public void setKey(boolean key) { + this.key = key; + } } diff --git a/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java b/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java index 588533e7e8..be83dec7bd 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java @@ -14,7 +14,6 @@ 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 @@ -204,14 +203,14 @@ public class CollectionElementTest extends TestCase { 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(); } diff --git a/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Matrix.java b/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Matrix.java index 3cdf325fde..7ec5f68850 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Matrix.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Matrix.java @@ -27,7 +27,7 @@ public class Matrix { @ElementCollection @Sort(type = SortType.NATURAL) @Type(type = "float") - private SortedMap values = new TreeMap(); + private SortedMap mvalues = new TreeMap(); public Integer getId() { return id; @@ -37,11 +37,11 @@ public class Matrix { this.id = id; } - public Map getValues() { - return values; + public Map getMvalues() { + return mvalues; } - public void setValues(SortedMap values) { - this.values = values; + public void setMvalues(SortedMap mValues) { + this.mvalues = mValues; } } diff --git a/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/Atmosphere.java b/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/Atmosphere.java index ca75572e7f..36dbb0f1b5 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/Atmosphere.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/Atmosphere.java @@ -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.JoinTable; 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 @@ public class Atmosphere { @MapKeyColumn(name="gas_name") public Map gases = new HashMap(); + @MapKeyTemporal(TemporalType.DATE) + @ElementCollection + public Map colorPerDate = new HashMap(); + @ManyToMany(cascade = CascadeType.ALL) @MapKeyJoinColumn(name="gas_id" ) @JoinTable(name = "Gas_per_key") diff --git a/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java b/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java index b8653c4080..6fbbd4b850 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java @@ -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 @@ public class IndexedCollectionTest extends TestCase { 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();