HHH-4598 An embeddable class may contains collection of basic types or embeddable objects

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18510 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Scott Marlow 2010-01-12 01:16:55 +00:00
parent bec0f8046e
commit 45d0e696b4
4 changed files with 150 additions and 1 deletions

View File

@ -2,6 +2,9 @@
package org.hibernate.test.annotations.embedded;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
@ -415,6 +418,68 @@ public class EmbeddedTest extends TestCase {
s.close();
}
// make sure we support collection of embeddable objects inside embeddable objects
public void testEmbeddableInsideEmbeddable() throws Exception {
Session s;
Transaction tx;
Collection<URLFavorite> urls = new ArrayList<URLFavorite>();
URLFavorite urlFavorite = new URLFavorite();
urlFavorite.setUrl( "http://highscalability.com/" );
urls.add(urlFavorite);
urlFavorite = new URLFavorite();
urlFavorite.setUrl( "http://www.jboss.org/" );
urls.add(urlFavorite);
urlFavorite = new URLFavorite();
urlFavorite.setUrl( "http://www.hibernate.org/" );
urls.add(urlFavorite);
urlFavorite = new URLFavorite();
urlFavorite.setUrl( "http://www.jgroups.org/" );
urls.add( urlFavorite );
Collection<String>ideas = new ArrayList<String>();
ideas.add( "lionheart" );
ideas.add( "xforms" );
ideas.add( "dynamic content" );
ideas.add( "http" );
InternetFavorites internetFavorites = new InternetFavorites();
internetFavorites.setLinks( urls );
internetFavorites.setIdeas( ideas );
FavoriteThings favoriteThings = new FavoriteThings();
favoriteThings.setWeb( internetFavorites );
s = openSession();
tx = s.beginTransaction();
s.persist(favoriteThings);
tx.commit();
tx = s.beginTransaction();
s.flush();
favoriteThings = (FavoriteThings) s.get( FavoriteThings.class, favoriteThings.getId() );
assertTrue( "has web", favoriteThings.getWeb() != null );
assertTrue( "has ideas", favoriteThings.getWeb().getIdeas() != null );
assertTrue( "has favorite idea 'http'",favoriteThings.getWeb().getIdeas().contains("http") );
assertTrue( "has favorite idea 'http'",favoriteThings.getWeb().getIdeas().contains("dynamic content") );
urls = favoriteThings.getWeb().getLinks();
assertTrue( "has urls", urls != null);
URLFavorite[] favs = new URLFavorite[4];
urls.toArray(favs);
assertTrue( "has http://www.hibernate.org url favorite link",
"http://www.hibernate.org/".equals( favs[0].getUrl() ) ||
"http://www.hibernate.org/".equals( favs[1].getUrl() ) ||
"http://www.hibernate.org/".equals( favs[2].getUrl() ) ||
"http://www.hibernate.org/".equals( favs[3].getUrl() ));
tx.commit();
s.close();
}
public EmbeddedTest(String x) {
super( x );
}
@ -431,7 +496,8 @@ public class EmbeddedTest extends TestCase {
InternetProvider.class,
CorpType.class,
Nationality.class,
Manager.class
Manager.class,
FavoriteThings.class
};
}
}

View File

@ -0,0 +1,34 @@
package org.hibernate.test.annotations.embedded;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* Has collection of embeddable objects inside embeddable objects for testing HHH-4598
*/
@Entity
public class FavoriteThings {
@Id
int id;
@Embedded
InternetFavorites web;
public InternetFavorites getWeb() {
return web;
}
public void setWeb(InternetFavorites web) {
this.web = web;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}

View File

@ -0,0 +1,32 @@
package org.hibernate.test.annotations.embedded;
import javax.persistence.Embeddable;
import javax.persistence.Embedded;
import java.util.Collection;
@Embeddable
public class InternetFavorites {
@Embedded
Collection<URLFavorite> links;
@Embedded
Collection<String> ideas;
public Collection<String> getIdeas() {
return ideas;
}
public void setIdeas(Collection<String> ideas) {
this.ideas = ideas;
}
public Collection<URLFavorite> getLinks() {
return links;
}
public void setLinks(Collection<URLFavorite> links) {
this.links = links;
}
}

View File

@ -0,0 +1,17 @@
package org.hibernate.test.annotations.embedded;
import javax.persistence.Embeddable;
@Embeddable
public class URLFavorite {
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}