From 45d0e696b43ba95aed2f3f083874ae53521959fc Mon Sep 17 00:00:00 2001 From: Scott Marlow Date: Tue, 12 Jan 2010 01:16:55 +0000 Subject: [PATCH] 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 --- .../annotations/embedded/EmbeddedTest.java | 68 ++++++++++++++++++- .../annotations/embedded/FavoriteThings.java | 34 ++++++++++ .../embedded/InternetFavorites.java | 32 +++++++++ .../annotations/embedded/URLFavorite.java | 17 +++++ 4 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/embedded/FavoriteThings.java create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/embedded/InternetFavorites.java create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/embedded/URLFavorite.java diff --git a/annotations/src/test/java/org/hibernate/test/annotations/embedded/EmbeddedTest.java b/annotations/src/test/java/org/hibernate/test/annotations/embedded/EmbeddedTest.java index 97006c49fe..e98ef78e17 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/embedded/EmbeddedTest.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/embedded/EmbeddedTest.java @@ -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 urls = new ArrayList(); + 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 ); + + Collectionideas = new ArrayList(); + 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 }; } } diff --git a/annotations/src/test/java/org/hibernate/test/annotations/embedded/FavoriteThings.java b/annotations/src/test/java/org/hibernate/test/annotations/embedded/FavoriteThings.java new file mode 100644 index 0000000000..bd6db126c0 --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/embedded/FavoriteThings.java @@ -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; + } +} diff --git a/annotations/src/test/java/org/hibernate/test/annotations/embedded/InternetFavorites.java b/annotations/src/test/java/org/hibernate/test/annotations/embedded/InternetFavorites.java new file mode 100644 index 0000000000..0aec43ad9e --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/embedded/InternetFavorites.java @@ -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 links; + + @Embedded + Collection ideas; + + public Collection getIdeas() { + return ideas; + } + + public void setIdeas(Collection ideas) { + this.ideas = ideas; + } + + public Collection getLinks() { + + return links; + } + + public void setLinks(Collection links) { + this.links = links; + } +} diff --git a/annotations/src/test/java/org/hibernate/test/annotations/embedded/URLFavorite.java b/annotations/src/test/java/org/hibernate/test/annotations/embedded/URLFavorite.java new file mode 100644 index 0000000000..5e71cb4e62 --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/embedded/URLFavorite.java @@ -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; + } +}