[hibernate-issues] [Hibernate-JIRA] Commented: (ANN-491) Collection mapped with javax.persistence.MapKey isn't loaded properly.

Magnus Heino (JIRA) noreply at atlassian.com
Tue Nov 14 04:05:05 EST 2006


    [ http://opensource.atlassian.com/projects/hibernate/browse/ANN-491?page=comments#action_25271 ] 

Magnus Heino commented on ANN-491:
----------------------------------


Here we go: (Uncomment the assert after s.get to trigger the load. As the code is right now, it breaks)

	public void testMapKeyLoad() throws Exception {
		Session s;
		Transaction tx;
		s = openSession();
		tx = s.beginTransaction();
		Software hibernate = new Software();
		hibernate.setName( "Hibernate" );
		Version v1 = new Version();
		v1.setCodeName( "HumbaHumba" );
		v1.setNumber( "1.0" );
		v1.setSoftware( hibernate );
		hibernate.addVersion(v1);
		s.persist(hibernate);
		s.persist(v1);
		tx.commit();
		s.clear();
		tx = s.beginTransaction();
		hibernate = (Software) s.get( Software.class, "Hibernate" );
		//assertEquals(1, hibernate.getVersions().size() );
		Version v2 = new Version();
		v2.setCodeName( "HumbaHumba2" );
		v2.setNumber( "2.0" );
		v2.setSoftware( hibernate );
		hibernate.addVersion(v2);		
		assertEquals("One loaded persisted version, and one just added", 2, hibernate.getVersions().size() );
		tx.commit();
		s.clear();
		tx = s.beginTransaction();
		hibernate = (Software) s.get( Software.class, "Hibernate" );
		for ( Version v : hibernate.getVersions().values() ) {
			s.delete( v );
		}
		s.delete( hibernate );
		tx.commit();
		s.close();	
	}

> Collection mapped with javax.persistence.MapKey isn't loaded properly.
> ----------------------------------------------------------------------
>
>          Key: ANN-491
>          URL: http://opensource.atlassian.com/projects/hibernate/browse/ANN-491
>      Project: Hibernate Annotations
>         Type: Bug

>     Versions: 3.2.0.ga
>     Reporter: Magnus Heino

>
>
> http://forum.hibernate.org/viewtopic.php?t=966948
> Add this test to IndexedCollectionTest.java:
> 	public void testMapKeyLoad() throws Exception {
> 		Session s;
> 		Transaction tx;
> 		s = openSession();
> 		tx = s.beginTransaction();
> 		Software hibernate = new Software();
> 		hibernate.setName( "Hibernate" );
> 		Version v1 = new Version();
> 		v1.setCodeName( "HumbaHumba" );
> 		v1.setNumber( "1.0" );
> 		v1.setSoftware( hibernate );
> 		hibernate.addVersion(v1);
> 		s.persist(hibernate);
> 		s.persist(v1);
> 		tx.commit();
> 		s.clear();
> 		tx = s.beginTransaction();
> 		hibernate = (Software) s.get( Software.class, "Hibernate" );
> 		Version v2 = new Version();
> 		v2.setCodeName( "HumbaHumba" );
> 		v2.setNumber( "2.0" );
> 		v2.setSoftware( hibernate );
> 		hibernate.addVersion(v2);
> 		assertEquals("One loaded persisted version, and one just added", 2, hibernate.getVersions().size() );
> 		tx.commit();
> 		s.clear();
> 		tx = s.beginTransaction();
> 		hibernate = (Software) s.get( Software.class, "Hibernate" );
> 		for ( Version v : hibernate.getVersions().values() ) {
> 			s.delete( v );
> 		}
> 		s.delete( hibernate );
> 		tx.commit();
> 		s.close();	
> 	}
> And modify Software.java to look like this:
> //$Id: Software.java 9795 2006-04-26 06:41:18Z epbernard $
> package org.hibernate.test.annotations.indexcoll;
> import java.util.HashMap;
> import java.util.Map;
> import javax.persistence.Entity;
> import javax.persistence.Id;
> import javax.persistence.MapKey;
> import javax.persistence.OneToMany;
> /**
>  * @author Emmanuel Bernard
>  */
> @Entity
> public class Software {
> 	private String name;
> 	private Map<String, Version> versions;
> 	public Software() {
> 		this.versions = new HashMap<String, Version>();
> 	}
> 	
> 	@Id
> 	public String getName() {
> 		return name;
> 	}
> 	public void setName(String name) {
> 		this.name = name;
> 	}
> 	@OneToMany(mappedBy = "software")
> 	@MapKey(name = "codeName")
> 	public Map<String, Version> getVersions() {
> 		return versions;
> 	}
> 	public void setVersions(Map<String, Version> versions) {
> 		this.versions = versions;
> 	}
> 	
> 	public void addVersion(Version version) {
> 		this.getVersions().put(version.getCodeName(), version);
> 	}
> }
> AFAIK addVersion should be able to do this.version.put too if you read the spec, but it doesn't work with the getter as it is now either.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira




More information about the hibernate-issues mailing list