-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Map<String, Entity> fails due to unique constraint
PostPosted: Thu Dec 04, 2008 4:17 pm 
Newbie

Joined: Thu Dec 04, 2008 4:03 pm
Posts: 1
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]


Top
 Profile  
 
 Post subject: Re: Map<String, Entity> fails due to unique constraint
PostPosted: Sun Jul 12, 2009 5:23 pm 
Newbie

Joined: Sun Jul 12, 2009 5:13 pm
Posts: 1
I was running into the same issue.

By using the @ManyToMany annotation instead, I found that the constraints were properly created.

Code:
import org.hibernate.Session;
import org.hibernate.Transaction;


public class Main {

   public static void main(String[] args) {
      TestMap map = new TestMap();
      map.setName("map1");
      
      TestMap map2 = new TestMap();
      map2.setName("map2");

      
      TestItem i1 = new TestItem();
      i1.setName("i1");
      i1.setProp1(1);
      
      TestItem i2 = new TestItem();
      i2.setName("i2");
      i2.setProp1(2);
      
      map.getMap().put("i1-key", i1);
      map.getMap().put("i2-key", i2);
      map.getMap().put("i2-key-copy", i2);
      map2.getMap().put("i2-key", i2);


      Session s = HibernateUtil.getSession();
      Transaction t = s.beginTransaction();
      s.save(i1);
      s.save(i2);
      s.save(map);
      s.save(map2);
      t.commit();
      s.close();

   }
}


Code:
import java.util.HashMap;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

import org.hibernate.annotations.MapKey;


@Entity
public class TestMap {

   @Id
   String name;
   
   @ManyToMany(cascade = CascadeType.ALL)
   @MapKey(columns = {@Column(name="keystring")})
   Map<String,TestItem> map = new HashMap<String,TestItem>();
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public Map<String, TestItem> getMap() {
      return map;
   }
   public void setMap(Map<String, TestItem> map) {
      this.map = map;
   }   
}



Code:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;


@Entity
public class TestItem {

   @Id @GeneratedValue
   long id;
   String name;
   Integer prop1;
   public long getId() {
      return id;
   }
   public void setId(long id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public Integer getProp1() {
      return prop1;
   }
   public void setProp1(Integer prop1) {
      this.prop1 = prop1;
   }   
}




--Jonathan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.