From 037f081793ea0caa404a1a63d5c0577880dc067c Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Mon, 19 Nov 2012 19:22:33 -0500 Subject: [PATCH] HHH-7702 Add support for collections of (aggregated) composite elements --- ...ositePluralAttributeElementSourceImpl.java | 8 +- .../HibernateTraversableResolverTest.java | 12 +- .../annotations/collectionelement/Boy.java | 209 ++++----- .../CollectionElementTest.java | 415 +++++++++--------- .../annotations/collectionelement/Toy.java | 18 +- .../withcustomenumdef/TestBasicOps.java | 7 +- 6 files changed, 334 insertions(+), 335 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/CompositePluralAttributeElementSourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/CompositePluralAttributeElementSourceImpl.java index fbfa3e3ce2..679b9738d9 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/CompositePluralAttributeElementSourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/CompositePluralAttributeElementSourceImpl.java @@ -52,6 +52,8 @@ public class CompositePluralAttributeElementSourceImpl implements CompositePlura private List attributeSources = new ArrayList(); + private String parentReferenceAttributeName; + public CompositePluralAttributeElementSourceImpl( AssociationAttribute associationAttribute, RootEntityClass rootEntityClass ) { @@ -111,8 +113,7 @@ public class CompositePluralAttributeElementSourceImpl implements CompositePlura @Override public String getParentReferenceAttributeName() { - // TODO: Is this correct? - return associationAttribute.getName(); + return parentReferenceAttributeName; } @Override @@ -125,6 +126,9 @@ public class CompositePluralAttributeElementSourceImpl implements CompositePlura EmbeddableClass embeddableClass = rootEntityClass .getCollectionEmbeddedClasses() .get( associationAttribute.getName() ); + + parentReferenceAttributeName = embeddableClass.getParentReferencingAttributeName(); + // TODO: Duplicates code in ComponentAttributeSourceImpl. for ( BasicAttribute attribute : embeddableClass.getSimpleAttributes() ) { AttributeOverride attributeOverride = null; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/beanvalidation/HibernateTraversableResolverTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/beanvalidation/HibernateTraversableResolverTest.java index 255961cacc..c5b3e21ae2 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/beanvalidation/HibernateTraversableResolverTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/beanvalidation/HibernateTraversableResolverTest.java @@ -23,25 +23,23 @@ */ package org.hibernate.test.annotations.beanvalidation; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + import java.math.BigDecimal; + import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; -import org.junit.Test; - import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; -import org.hibernate.testing.FailureExpectedWithNewMetamodel; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import org.junit.Test; /** * @author Emmanuel Bernard */ -@FailureExpectedWithNewMetamodel public class HibernateTraversableResolverTest extends BaseCoreFunctionalTestCase { @Test public void testNonLazyAssocFieldWithConstraintsFailureExpected() { diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java index 7fba52cbc9..06e9c24f13 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; + import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.CollectionTable; @@ -34,16 +35,16 @@ public class Boy { private Integer id; private String firstName; private String lastName; -// private Set nickNames = new HashSet(); -// private Set hatedNames = new HashSet(); -// private Set preferredNames = new HashSet(); -// private Map scorePerNickName = new HashMap(); -// private Map scorePerPreferredName = new HashMap(); -// private int[] favoriteNumbers; + private Set nickNames = new HashSet(); + private Set hatedNames = new HashSet(); + private Set preferredNames = new HashSet(); + private Map scorePerNickName = new HashMap(); + private Map scorePerPreferredName = new HashMap(); + private int[] favoriteNumbers; private Set favoriteToys = new HashSet(); -// private Set characters = new HashSet(); -// private Map foods = new HashMap(); -// private Set countryAttitudes = new HashSet(); + private Set characters = new HashSet(); + private Map foods = new HashMap(); + private Set countryAttitudes = new HashSet(); @Id @GeneratedValue @@ -71,70 +72,70 @@ public class Boy { this.lastName = lastName; } -// @ElementCollection -// public Set getNickNames() { -// return nickNames; -// } -// -// public void setNickNames(Set nickName) { -// this.nickNames = nickName; -// } -// -// @ElementCollection //default column names -// public Set getHatedNames() { -// return hatedNames; -// } -// -// public void setHatedNames(Set hatedNames) { -// this.hatedNames = hatedNames; -// } -// -// @ElementCollection //default column names -// @Column -// public Set getPreferredNames() { -// return preferredNames; -// } -// -// public void setPreferredNames(Set preferredNames) { -// this.preferredNames = preferredNames; -// } -// -// @ElementCollection -// @MapKeyColumn(nullable=false) -// public Map getScorePerPreferredName() { -// return scorePerPreferredName; -// } -// -// public void setScorePerPreferredName(Map scorePerPreferredName) { -// this.scorePerPreferredName = scorePerPreferredName; -// } -// -// @ElementCollection -// @CollectionTable(name = "ScorePerNickName", joinColumns = @JoinColumn(name = "BoyId")) -// @Column(name = "score", nullable = false) -// @MapKeyColumn(nullable=false) -// public Map getScorePerNickName() { -// return scorePerNickName; -// } -// -// public void setScorePerNickName(Map scorePerNickName) { -// this.scorePerNickName = scorePerNickName; -// } -// -// @ElementCollection -// @CollectionTable( -// name = "BoyFavoriteNumbers", -// joinColumns = @JoinColumn(name = "BoyId") -// ) -// @Column(name = "favoriteNumber", nullable = false) -// @OrderColumn(name = "nbr_index") -// public int[] getFavoriteNumbers() { -// return favoriteNumbers; -// } -// -// public void setFavoriteNumbers(int[] favoriteNumbers) { -// this.favoriteNumbers = favoriteNumbers; -// } + @ElementCollection + public Set getNickNames() { + return nickNames; + } + + public void setNickNames(Set nickName) { + this.nickNames = nickName; + } + + @ElementCollection //default column names + public Set getHatedNames() { + return hatedNames; + } + + public void setHatedNames(Set hatedNames) { + this.hatedNames = hatedNames; + } + + @ElementCollection //default column names + @Column + public Set getPreferredNames() { + return preferredNames; + } + + public void setPreferredNames(Set preferredNames) { + this.preferredNames = preferredNames; + } + + @ElementCollection + @MapKeyColumn(nullable=false) + public Map getScorePerPreferredName() { + return scorePerPreferredName; + } + + public void setScorePerPreferredName(Map scorePerPreferredName) { + this.scorePerPreferredName = scorePerPreferredName; + } + + @ElementCollection + @CollectionTable(name = "ScorePerNickName", joinColumns = @JoinColumn(name = "BoyId")) + @Column(name = "score", nullable = false) + @MapKeyColumn(nullable=false) + public Map getScorePerNickName() { + return scorePerNickName; + } + + public void setScorePerNickName(Map scorePerNickName) { + this.scorePerNickName = scorePerNickName; + } + + @ElementCollection + @CollectionTable( + name = "BoyFavoriteNumbers", + joinColumns = @JoinColumn(name = "BoyId") + ) + @Column(name = "favoriteNumber", nullable = false) + @OrderColumn(name = "nbr_index") + public int[] getFavoriteNumbers() { + return favoriteNumbers; + } + + public void setFavoriteNumbers(int[] favoriteNumbers) { + this.favoriteNumbers = favoriteNumbers; + } @ElementCollection @AttributeOverride(name = "element.serial", column = @Column(name = "serial_nbr")) public Set getFavoriteToys() { @@ -145,36 +146,36 @@ public class Boy { this.favoriteToys = favoriteToys; } -// @ElementCollection -// @Enumerated(EnumType.STRING) -// @Column(name = "`characters`") -// public Set getCharacters() { -// return characters; -// } -// -// public void setCharacters(Set characters) { -// this.characters = characters; -// } -// -// @ElementCollection -// @Enumerated(EnumType.STRING) -// @MapKeyColumn(nullable=false) -// public Map getFavoriteFood() { -// return foods; -// } -// -// public void setFavoriteFood(Mapfoods) { -// this.foods = foods; -// } -// -// @ElementCollection(fetch = FetchType.EAGER) -// //@Where(clause = "b_likes=false") -// public Set getCountryAttitudes() { -// return countryAttitudes; -// } -// -// public void setCountryAttitudes(Set countryAttitudes) { -// this.countryAttitudes = countryAttitudes; -// } + @ElementCollection + @Enumerated(EnumType.STRING) + @Column(name = "`characters`") + public Set getCharacters() { + return characters; + } + + public void setCharacters(Set characters) { + this.characters = characters; + } + + @ElementCollection + @Enumerated(EnumType.STRING) + @MapKeyColumn(nullable=false) + public Map getFavoriteFood() { + return foods; + } + + public void setFavoriteFood(Mapfoods) { + this.foods = foods; + } + + @ElementCollection(fetch = FetchType.EAGER) + //@Where(clause = "b_likes=false") + public Set getCountryAttitudes() { + return countryAttitudes; + } + + public void setCountryAttitudes(Set countryAttitudes) { + this.countryAttitudes = countryAttitudes; + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java index 3e406727ba..e71a68bbe0 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/CollectionElementTest.java @@ -23,13 +23,15 @@ */ package org.hibernate.test.annotations.collectionelement; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Locale; -import org.junit.Test; - import org.hibernate.Filter; import org.hibernate.Query; import org.hibernate.Session; @@ -38,70 +40,67 @@ import org.hibernate.metamodel.spi.binding.PluralAttributeBinding; import org.hibernate.test.annotations.Country; import org.hibernate.testing.FailureExpectedWithNewMetamodel; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import org.junit.Test; /** * @author Emmanuel Bernard * @author Hardy Ferentschik */ @SuppressWarnings("unchecked") -//@FailureExpectedWithNewMetamodel +@FailureExpectedWithNewMetamodel public class CollectionElementTest extends BaseCoreFunctionalTestCase { -// @Test -// public void testSimpleElement() throws Exception { -// assertEquals( -// "BoyFavoriteNumbers", -// configuration().getCollectionMapping( Boy.class.getName() + '.' + "favoriteNumbers" ) -// .getCollectionTable().getName() -// ); -// Session s = openSession(); -// s.getTransaction().begin(); -// Boy boy = new Boy(); -// boy.setFirstName( "John" ); -// boy.setLastName( "Doe" ); -// boy.getNickNames().add( "Johnny" ); -// boy.getNickNames().add( "Thing" ); -// boy.getScorePerNickName().put( "Johnny", new Integer( 3 ) ); -// boy.getScorePerNickName().put( "Thing", new Integer( 5 ) ); -// int[] favNbrs = new int[4]; -// for (int index = 0; index < favNbrs.length - 1; index++) { -// favNbrs[index] = index * 3; -// } -// boy.setFavoriteNumbers( favNbrs ); -// boy.getCharacters().add( Character.GENTLE ); -// boy.getCharacters().add( Character.CRAFTY ); -// -// HashMap foods = new HashMap(); -// foods.put( "breakfast", FavoriteFood.PIZZA); -// foods.put( "lunch", FavoriteFood.KUNGPAOCHICKEN); -// foods.put( "dinner", FavoriteFood.SUSHI); -// boy.setFavoriteFood(foods); -// s.persist( boy ); -// s.getTransaction().commit(); -// s.clear(); -// Transaction tx = s.beginTransaction(); -// boy = (Boy) s.get( Boy.class, boy.getId() ); -// assertNotNull( boy.getNickNames() ); -// assertTrue( boy.getNickNames().contains( "Thing" ) ); -// assertNotNull( boy.getScorePerNickName() ); -// assertTrue( boy.getScorePerNickName().containsKey( "Thing" ) ); -// assertEquals( new Integer( 5 ), boy.getScorePerNickName().get( "Thing" ) ); -// assertNotNull( boy.getFavoriteNumbers() ); -// assertEquals( 3, boy.getFavoriteNumbers()[1] ); -// assertTrue( boy.getCharacters().contains( Character.CRAFTY ) ); -// assertTrue( boy.getFavoriteFood().get("dinner").equals(FavoriteFood.SUSHI)); -// assertTrue( boy.getFavoriteFood().get("lunch").equals(FavoriteFood.KUNGPAOCHICKEN)); -// assertTrue( boy.getFavoriteFood().get("breakfast").equals(FavoriteFood.PIZZA)); -// List result = s.createQuery( "select boy from Boy boy join boy.nickNames names where names = :name" ) -// .setParameter( "name", "Thing" ).list(); -// assertEquals( 1, result.size() ); -// s.delete( boy ); -// tx.commit(); -// s.close(); -// } + @Test + public void testSimpleElement() throws Exception { + assertEquals( + "BoyFavoriteNumbers", + configuration().getCollectionMapping( Boy.class.getName() + '.' + "favoriteNumbers" ) + .getCollectionTable().getName() + ); + Session s = openSession(); + s.getTransaction().begin(); + Boy boy = new Boy(); + boy.setFirstName( "John" ); + boy.setLastName( "Doe" ); + boy.getNickNames().add( "Johnny" ); + boy.getNickNames().add( "Thing" ); + boy.getScorePerNickName().put( "Johnny", new Integer( 3 ) ); + boy.getScorePerNickName().put( "Thing", new Integer( 5 ) ); + int[] favNbrs = new int[4]; + for (int index = 0; index < favNbrs.length - 1; index++) { + favNbrs[index] = index * 3; + } + boy.setFavoriteNumbers( favNbrs ); + boy.getCharacters().add( Character.GENTLE ); + boy.getCharacters().add( Character.CRAFTY ); + + HashMap foods = new HashMap(); + foods.put( "breakfast", FavoriteFood.PIZZA); + foods.put( "lunch", FavoriteFood.KUNGPAOCHICKEN); + foods.put( "dinner", FavoriteFood.SUSHI); + boy.setFavoriteFood(foods); + s.persist( boy ); + s.getTransaction().commit(); + s.clear(); + Transaction tx = s.beginTransaction(); + boy = (Boy) s.get( Boy.class, boy.getId() ); + assertNotNull( boy.getNickNames() ); + assertTrue( boy.getNickNames().contains( "Thing" ) ); + assertNotNull( boy.getScorePerNickName() ); + assertTrue( boy.getScorePerNickName().containsKey( "Thing" ) ); + assertEquals( new Integer( 5 ), boy.getScorePerNickName().get( "Thing" ) ); + assertNotNull( boy.getFavoriteNumbers() ); + assertEquals( 3, boy.getFavoriteNumbers()[1] ); + assertTrue( boy.getCharacters().contains( Character.CRAFTY ) ); + assertTrue( boy.getFavoriteFood().get("dinner").equals(FavoriteFood.SUSHI)); + assertTrue( boy.getFavoriteFood().get("lunch").equals(FavoriteFood.KUNGPAOCHICKEN)); + assertTrue( boy.getFavoriteFood().get("breakfast").equals(FavoriteFood.PIZZA)); + List result = s.createQuery( "select boy from Boy boy join boy.nickNames names where names = :name" ) + .setParameter( "name", "Thing" ).list(); + assertEquals( 1, result.size() ); + s.delete( boy ); + tx.commit(); + s.close(); + } @Test public void testCompositeElement() throws Exception { @@ -123,166 +122,166 @@ public class CollectionElementTest extends BaseCoreFunctionalTestCase { boy = (Boy) s.get( Boy.class, boy.getId() ); assertNotNull( boy.getFavoriteToys() ); assertTrue( boy.getFavoriteToys().contains( toy ) ); -// assertEquals( "@Parent is failing", boy, boy.getFavoriteToys().iterator().next().getOwner() ); + assertEquals( "@Parent is failing", boy, boy.getFavoriteToys().iterator().next().getOwner() ); s.delete( boy ); tx.commit(); s.close(); } -// @Test -// public void testAttributedJoin() throws Exception { -// Session s = openSession(); -// s.getTransaction().begin(); -// Country country = new Country(); -// country.setName( "Australia" ); -// s.persist( country ); -// -// Boy boy = new Boy(); -// boy.setFirstName( "John" ); -// boy.setLastName( "Doe" ); -// CountryAttitude attitude = new CountryAttitude(); -// // TODO: doesn't work -// attitude.setBoy( boy ); -// attitude.setCountry( country ); -// attitude.setLikes( true ); -// boy.getCountryAttitudes().add( attitude ); -// s.persist( boy ); -// s.getTransaction().commit(); -// s.clear(); -// -// Transaction tx = s.beginTransaction(); -// boy = (Boy) s.get( Boy.class, boy.getId() ); -// assertTrue( boy.getCountryAttitudes().contains( attitude ) ); -// s.delete( boy ); -// s.delete( s.get( Country.class, country.getId() ) ); -// tx.commit(); -// s.close(); -// } -// -// @Test -// public void testLazyCollectionofElements() throws Exception { -// assertEquals( -// "BoyFavoriteNumbers", -// configuration().getCollectionMapping( Boy.class.getName() + '.' + "favoriteNumbers" ) -// .getCollectionTable().getName() -// ); -// Session s = openSession(); -// s.getTransaction().begin(); -// Boy boy = new Boy(); -// boy.setFirstName( "John" ); -// boy.setLastName( "Doe" ); -// boy.getNickNames().add( "Johnny" ); -// boy.getNickNames().add( "Thing" ); -// boy.getScorePerNickName().put( "Johnny", new Integer( 3 ) ); -// boy.getScorePerNickName().put( "Thing", new Integer( 5 ) ); -// int[] favNbrs = new int[4]; -// for (int index = 0; index < favNbrs.length - 1; index++) { -// favNbrs[index] = index * 3; -// } -// boy.setFavoriteNumbers( favNbrs ); -// boy.getCharacters().add( Character.GENTLE ); -// boy.getCharacters().add( Character.CRAFTY ); -// s.persist( boy ); -// s.getTransaction().commit(); -// s.clear(); -// Transaction tx = s.beginTransaction(); -// boy = (Boy) s.get( Boy.class, boy.getId() ); -// assertNotNull( boy.getNickNames() ); -// assertTrue( boy.getNickNames().contains( "Thing" ) ); -// assertNotNull( boy.getScorePerNickName() ); -// assertTrue( boy.getScorePerNickName().containsKey( "Thing" ) ); -// assertEquals( new Integer( 5 ), boy.getScorePerNickName().get( "Thing" ) ); -// assertNotNull( boy.getFavoriteNumbers() ); -// assertEquals( 3, boy.getFavoriteNumbers()[1] ); -// assertTrue( boy.getCharacters().contains( Character.CRAFTY ) ); -// List result = s.createQuery( "select boy from Boy boy join boy.nickNames names where names = :name" ) -// .setParameter( "name", "Thing" ).list(); -// assertEquals( 1, result.size() ); -// s.delete( boy ); -// tx.commit(); -// s.close(); -// } -// -// @Test -// public void testFetchEagerAndFilter() throws Exception { -// Session s = openSession(); -// Transaction tx = s.beginTransaction(); -// -// TestCourse test = new TestCourse(); -// -// LocalizedString title = new LocalizedString( "title in english" ); -// title.getVariations().put( Locale.FRENCH.getLanguage(), "title en francais" ); -// test.setTitle( title ); -// s.save( test ); -// -// s.flush(); -// s.clear(); -// -// Filter filter = s.enableFilter( "selectedLocale" ); -// filter.setParameter( "param", "fr" ); -// -// Query q = s.createQuery( "from TestCourse t" ); -// List l = q.list(); -// assertEquals( 1, l.size() ); -// -// TestCourse t = (TestCourse) s.get( TestCourse.class, test.getTestCourseId() ); -// assertEquals( 1, t.getTitle().getVariations().size() ); -// -// tx.rollback(); -// -// s.close(); -// } -// -// @Test -// public void testMapKeyType() throws Exception { -// Matrix m = new Matrix(); -// m.getMvalues().put( 1, 1.1f ); -// Session s = openSession(); -// Transaction tx = s.beginTransaction(); -// s.persist( m ); -// s.flush(); -// s.clear(); -// m = (Matrix) s.get( Matrix.class, m.getId() ); -// assertEquals( 1.1f, m.getMvalues().get( 1 ), 0.01f ); -// tx.rollback(); -// s.close(); -// } -// -// @Test -// public void testDefaultValueColumnForBasic() throws Exception { -// isCollectionColumnPresent( Boy.class.getName(), "hatedNames" ); -// isCollectionColumnPresent( Boy.class.getName(), "preferredNames" ); -// isCollectionColumnPresent( Boy.class.getName(), "nickNames" ); -// isCollectionColumnPresent( Boy.class.getName(), "scorePerPreferredName"); -// } -// -// @Test -// public void testDefaultFKNameForElementCollection() throws Exception { -// isCollectionColumnPresent( Boy.class.getName(), "Boy_id" ); -// } -// -// private void isCollectionColumnPresent(String collectionOwner, String columnName) { -// // TODO: Is this correct? Cannot test due to ManyToOne issues. -// Iterator bindings = getCollectionBindings(); -// boolean hasDefault = false; -// while ( bindings.hasNext() ) { -// PluralAttributeBinding binding = bindings.next(); -// if ( binding.getAttribute().getName().equals( columnName ) -// && binding.getAttribute().getAttributeContainer().getClassName().equals( collectionOwner ) ) { -// hasDefault = true; -// break; -// } -// } -// assertTrue( "Could not find " + columnName, hasDefault ); -// } + @Test + public void testAttributedJoin() throws Exception { + Session s = openSession(); + s.getTransaction().begin(); + Country country = new Country(); + country.setName( "Australia" ); + s.persist( country ); + + Boy boy = new Boy(); + boy.setFirstName( "John" ); + boy.setLastName( "Doe" ); + CountryAttitude attitude = new CountryAttitude(); + // TODO: doesn't work + attitude.setBoy( boy ); + attitude.setCountry( country ); + attitude.setLikes( true ); + boy.getCountryAttitudes().add( attitude ); + s.persist( boy ); + s.getTransaction().commit(); + s.clear(); + + Transaction tx = s.beginTransaction(); + boy = (Boy) s.get( Boy.class, boy.getId() ); + assertTrue( boy.getCountryAttitudes().contains( attitude ) ); + s.delete( boy ); + s.delete( s.get( Country.class, country.getId() ) ); + tx.commit(); + s.close(); + } + + @Test + public void testLazyCollectionofElements() throws Exception { + assertEquals( + "BoyFavoriteNumbers", + configuration().getCollectionMapping( Boy.class.getName() + '.' + "favoriteNumbers" ) + .getCollectionTable().getName() + ); + Session s = openSession(); + s.getTransaction().begin(); + Boy boy = new Boy(); + boy.setFirstName( "John" ); + boy.setLastName( "Doe" ); + boy.getNickNames().add( "Johnny" ); + boy.getNickNames().add( "Thing" ); + boy.getScorePerNickName().put( "Johnny", new Integer( 3 ) ); + boy.getScorePerNickName().put( "Thing", new Integer( 5 ) ); + int[] favNbrs = new int[4]; + for (int index = 0; index < favNbrs.length - 1; index++) { + favNbrs[index] = index * 3; + } + boy.setFavoriteNumbers( favNbrs ); + boy.getCharacters().add( Character.GENTLE ); + boy.getCharacters().add( Character.CRAFTY ); + s.persist( boy ); + s.getTransaction().commit(); + s.clear(); + Transaction tx = s.beginTransaction(); + boy = (Boy) s.get( Boy.class, boy.getId() ); + assertNotNull( boy.getNickNames() ); + assertTrue( boy.getNickNames().contains( "Thing" ) ); + assertNotNull( boy.getScorePerNickName() ); + assertTrue( boy.getScorePerNickName().containsKey( "Thing" ) ); + assertEquals( new Integer( 5 ), boy.getScorePerNickName().get( "Thing" ) ); + assertNotNull( boy.getFavoriteNumbers() ); + assertEquals( 3, boy.getFavoriteNumbers()[1] ); + assertTrue( boy.getCharacters().contains( Character.CRAFTY ) ); + List result = s.createQuery( "select boy from Boy boy join boy.nickNames names where names = :name" ) + .setParameter( "name", "Thing" ).list(); + assertEquals( 1, result.size() ); + s.delete( boy ); + tx.commit(); + s.close(); + } + + @Test + public void testFetchEagerAndFilter() throws Exception { + Session s = openSession(); + Transaction tx = s.beginTransaction(); + + TestCourse test = new TestCourse(); + + LocalizedString title = new LocalizedString( "title in english" ); + title.getVariations().put( Locale.FRENCH.getLanguage(), "title en francais" ); + test.setTitle( title ); + s.save( test ); + + s.flush(); + s.clear(); + + Filter filter = s.enableFilter( "selectedLocale" ); + filter.setParameter( "param", "fr" ); + + Query q = s.createQuery( "from TestCourse t" ); + List l = q.list(); + assertEquals( 1, l.size() ); + + TestCourse t = (TestCourse) s.get( TestCourse.class, test.getTestCourseId() ); + assertEquals( 1, t.getTitle().getVariations().size() ); + + tx.rollback(); + + s.close(); + } + + @Test + public void testMapKeyType() throws Exception { + Matrix m = new Matrix(); + m.getMvalues().put( 1, 1.1f ); + Session s = openSession(); + Transaction tx = s.beginTransaction(); + s.persist( m ); + s.flush(); + s.clear(); + m = (Matrix) s.get( Matrix.class, m.getId() ); + assertEquals( 1.1f, m.getMvalues().get( 1 ), 0.01f ); + tx.rollback(); + s.close(); + } + + @Test + public void testDefaultValueColumnForBasic() throws Exception { + isCollectionColumnPresent( Boy.class.getName(), "hatedNames" ); + isCollectionColumnPresent( Boy.class.getName(), "preferredNames" ); + isCollectionColumnPresent( Boy.class.getName(), "nickNames" ); + isCollectionColumnPresent( Boy.class.getName(), "scorePerPreferredName"); + } + + @Test + public void testDefaultFKNameForElementCollection() throws Exception { + isCollectionColumnPresent( Boy.class.getName(), "Boy_id" ); + } + + private void isCollectionColumnPresent(String collectionOwner, String columnName) { + // TODO: Is this correct? Cannot test due to ManyToOne issues. + Iterator bindings = getCollectionBindings(); + boolean hasDefault = false; + while ( bindings.hasNext() ) { + PluralAttributeBinding binding = bindings.next(); + if ( binding.getAttribute().getName().equals( columnName ) + && binding.getAttribute().getAttributeContainer().getClassName().equals( collectionOwner ) ) { + hasDefault = true; + break; + } + } + assertTrue( "Could not find " + columnName, hasDefault ); + } @Override protected Class[] getAnnotatedClasses() { return new Class[] { - Boy.class/*, + Boy.class, Country.class, TestCourse.class, - Matrix.class*/ + Matrix.class }; } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/Toy.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/Toy.java index 3c5fba873e..d4f0655c4f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/Toy.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/Toy.java @@ -14,7 +14,7 @@ public class Toy { private String name; private Brand brand; private String serial; -// private Boy owner; + private Boy owner; @AttributeOverride(name = "name", column = @Column(name = "brand_name")) public Brand getBrand() { @@ -41,14 +41,14 @@ public class Toy { this.serial = serial; } -// @Parent -// public Boy getOwner() { -// return owner; -// } -// -// public void setOwner(Boy owner) { -// this.owner = owner; -// } + @Parent + public Boy getOwner() { + return owner; + } + + public void setOwner(Boy owner) { + this.owner = owner; + } public boolean equals(Object o) { if ( this == o ) return true; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/embeddables/withcustomenumdef/TestBasicOps.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/embeddables/withcustomenumdef/TestBasicOps.java index bd6bfc0a4c..d7c7009c26 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/embeddables/withcustomenumdef/TestBasicOps.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/embeddables/withcustomenumdef/TestBasicOps.java @@ -23,18 +23,15 @@ */ package org.hibernate.test.annotations.collectionelement.embeddables.withcustomenumdef; -import org.junit.Test; +import static junit.framework.Assert.assertEquals; import org.hibernate.Session; -import org.hibernate.testing.FailureExpectedWithNewMetamodel; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -import static junit.framework.Assert.assertEquals; +import org.junit.Test; /** * @author Steve Ebersole */ -@FailureExpectedWithNewMetamodel public class TestBasicOps extends BaseCoreFunctionalTestCase { @Override protected Class[] getAnnotatedClasses() {