Hello. I am trying to create a CollectionOfElements where the key is a String, the entity is an Entity, where it is possible for many keys to map to the same Entity (e.g. "keya" -> EntityX, "keyb" -> EntityX). It seems that no matter what I have tried, a unique constraint is always placed on the Entity. I need to know if this is correct behavior or not. I have not been able to find this in any documentation (perhaps I missed something?).
I applied the following patch (see end of post) to the annotations checkout in order to reproduce (ant clean junit).
I have one line commented out that, if uncommented, will fail (because the column is set to unique). You can verify the column is set to unique in build/test-reports/hsqldb/TEST-org.hibernate.test.annotations.collectionelement.CollectionElementTest.txt
It looks like AnnotationBinder may be the cause... around 1566 (?) :
'else if ( collectionOfElementsAnn != null )'
collectionsOfElementsAnn is not null if @CollectionOfElements is set. In the else if block, setOneToMany is set on the CollectionBinder. This becomes the 'unique' boolean value later on down the road.
Thanks!
jason
[code]
Index: tags/3.4.0.GA/src/test/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java
===================================================================
--- tags/3.4.0.GA/src/test/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java (revision 15666)
+++ tags/3.4.0.GA/src/test/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java (working copy)
@@ -18,6 +18,33 @@
@SuppressWarnings("unchecked")
public class CollectionElementTest extends TestCase {
+ public void testEntityMapProblem() throws Exception {
+ // Save an entity instance
+ ValueEntity v = new ValueEntity();
+ Session s = openSession();
+ Transaction tx = s.beginTransaction();
+ s.persist( v );
+ s.getTransaction().commit();
+ s.clear();
+ v = (ValueEntity) s.get( ValueEntity.class, v.getId() );
+
+ // Try and use two keys for same Entity (fail)
+ s.getTransaction().begin();
+ Boy boy = new Boy();
+ boy.setFirstName( "John" );
+ boy.setLastName( "Doe" );
+ boy.getEntities().put( "keyA", v );
+ //boy.getEntities().put( "keyB", v );
+ s.persist( boy );
+ s.getTransaction().commit();
+ s.clear();
+ tx = s.beginTransaction();
+ boy = (Boy) s.get( Boy.class, boy.getId() );
+ s.delete( boy );
+ tx.commit();
+ s.close();
+ }
+
public void testSimpleElement() throws Exception {
assertEquals(
"BoyFavoriteNumbers",
@@ -207,7 +234,8 @@
Boy.class,
Country.class,
TestCourse.class,
- Matrix.class
+ Matrix.class,
+ ValueEntity.class
};
}
}
Index: tags/3.4.0.GA/src/test/org/hibernate/test/annotations/collectionelement/Boy.java
===================================================================
--- tags/3.4.0.GA/src/test/org/hibernate/test/annotations/collectionelement/Boy.java (revision 15666)
+++ tags/3.4.0.GA/src/test/org/hibernate/test/annotations/collectionelement/Boy.java (working copy)
@@ -19,6 +19,7 @@
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.IndexColumn;
+import org.hibernate.annotations.MapKey;
/**
* @author Emmanuel Bernard
@@ -35,6 +36,7 @@
private String lastName;
private Set<String> nickNames = new HashSet<String>();
private Map<String, Integer> scorePerNickName = new HashMap<String, Integer>();
+ private Map<String, ValueEntity> entity = new HashMap<String, ValueEntity>();
private int[] favoriteNumbers;
private Set<Toy> favoriteToys = new HashSet<Toy>();
private Set<Character> characters = new HashSet<Character>();
@@ -86,6 +88,16 @@
this.scorePerNickName = scorePerNickName;
}
+ @CollectionOfElements (targetElement=ValueEntity.class, fetch=FetchType.LAZY)
+ @MapKey (columns = @Column (name = "ident", nullable = false, length = 64),
+ targetElement = String.class)
+ public Map<String, ValueEntity> getEntities() {
+ return entity;
+ }
+ public void setEntities(Map<String, ValueEntity> entity) {
+ this.entity = entity;
+ }
+
@CollectionOfElements
@JoinTable(
name = "BoyFavoriteNumbers",
Index: tags/3.4.0.GA/src/test/org/hibernate/test/annotations/collectionelement/ValueEntity.java
===================================================================
--- tags/3.4.0.GA/src/test/org/hibernate/test/annotations/collectionelement/ValueEntity.java (revision 0)
+++ tags/3.4.0.GA/src/test/org/hibernate/test/annotations/collectionelement/ValueEntity.java (revision 0)
@@ -0,0 +1,37 @@
+//$
+package org.hibernate.test.annotations.collectionelement;
+
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+import org.hibernate.annotations.CollectionOfElements;
+import org.hibernate.annotations.MapKey;
+import org.hibernate.annotations.Sort;
+import org.hibernate.annotations.SortType;
+import org.hibernate.annotations.Type;
+
+@Entity
+public class ValueEntity {
+ @Id
+ @GeneratedValue
+ private Integer id;
+
+ @MapKey(type = @Type(type="integer") )
+ @CollectionOfElements
+ @Sort(type = SortType.NATURAL)
+ @Type(type = "float")
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+}
[/code]
|