HHH-7702 Add support for collections of (aggregated) composite elements

This commit is contained in:
Brett Meyer 2012-11-19 19:22:33 -05:00
parent 1c992b1faf
commit 037f081793
6 changed files with 334 additions and 335 deletions

View File

@ -52,6 +52,8 @@ public class CompositePluralAttributeElementSourceImpl implements CompositePlura
private List<AttributeSource> attributeSources
= new ArrayList<AttributeSource>();
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;

View File

@ -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() {

View File

@ -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<String> nickNames = new HashSet<String>();
// private Set<String> hatedNames = new HashSet<String>();
// private Set<String> preferredNames = new HashSet<String>();
// private Map<String, Integer> scorePerNickName = new HashMap<String, Integer>();
// private Map<String, Integer> scorePerPreferredName = new HashMap<String, Integer>();
// private int[] favoriteNumbers;
private Set<String> nickNames = new HashSet<String>();
private Set<String> hatedNames = new HashSet<String>();
private Set<String> preferredNames = new HashSet<String>();
private Map<String, Integer> scorePerNickName = new HashMap<String, Integer>();
private Map<String, Integer> scorePerPreferredName = new HashMap<String, Integer>();
private int[] favoriteNumbers;
private Set<Toy> favoriteToys = new HashSet<Toy>();
// private Set<Character> characters = new HashSet<Character>();
// private Map<String, FavoriteFood> foods = new HashMap<String,FavoriteFood>();
// private Set<CountryAttitude> countryAttitudes = new HashSet<CountryAttitude>();
private Set<Character> characters = new HashSet<Character>();
private Map<String, FavoriteFood> foods = new HashMap<String,FavoriteFood>();
private Set<CountryAttitude> countryAttitudes = new HashSet<CountryAttitude>();
@Id
@GeneratedValue
@ -71,70 +72,70 @@ public class Boy {
this.lastName = lastName;
}
// @ElementCollection
// public Set<String> getNickNames() {
// return nickNames;
// }
//
// public void setNickNames(Set<String> nickName) {
// this.nickNames = nickName;
// }
//
// @ElementCollection //default column names
// public Set<String> getHatedNames() {
// return hatedNames;
// }
//
// public void setHatedNames(Set<String> hatedNames) {
// this.hatedNames = hatedNames;
// }
//
// @ElementCollection //default column names
// @Column
// public Set<String> getPreferredNames() {
// return preferredNames;
// }
//
// public void setPreferredNames(Set<String> preferredNames) {
// this.preferredNames = preferredNames;
// }
//
// @ElementCollection
// @MapKeyColumn(nullable=false)
// public Map<String, Integer> getScorePerPreferredName() {
// return scorePerPreferredName;
// }
//
// public void setScorePerPreferredName(Map<String, Integer> scorePerPreferredName) {
// this.scorePerPreferredName = scorePerPreferredName;
// }
//
// @ElementCollection
// @CollectionTable(name = "ScorePerNickName", joinColumns = @JoinColumn(name = "BoyId"))
// @Column(name = "score", nullable = false)
// @MapKeyColumn(nullable=false)
// public Map<String, Integer> getScorePerNickName() {
// return scorePerNickName;
// }
//
// public void setScorePerNickName(Map<String, Integer> 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<String> getNickNames() {
return nickNames;
}
public void setNickNames(Set<String> nickName) {
this.nickNames = nickName;
}
@ElementCollection //default column names
public Set<String> getHatedNames() {
return hatedNames;
}
public void setHatedNames(Set<String> hatedNames) {
this.hatedNames = hatedNames;
}
@ElementCollection //default column names
@Column
public Set<String> getPreferredNames() {
return preferredNames;
}
public void setPreferredNames(Set<String> preferredNames) {
this.preferredNames = preferredNames;
}
@ElementCollection
@MapKeyColumn(nullable=false)
public Map<String, Integer> getScorePerPreferredName() {
return scorePerPreferredName;
}
public void setScorePerPreferredName(Map<String, Integer> scorePerPreferredName) {
this.scorePerPreferredName = scorePerPreferredName;
}
@ElementCollection
@CollectionTable(name = "ScorePerNickName", joinColumns = @JoinColumn(name = "BoyId"))
@Column(name = "score", nullable = false)
@MapKeyColumn(nullable=false)
public Map<String, Integer> getScorePerNickName() {
return scorePerNickName;
}
public void setScorePerNickName(Map<String, Integer> 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<Toy> getFavoriteToys() {
@ -145,36 +146,36 @@ public class Boy {
this.favoriteToys = favoriteToys;
}
// @ElementCollection
// @Enumerated(EnumType.STRING)
// @Column(name = "`characters`")
// public Set<Character> getCharacters() {
// return characters;
// }
//
// public void setCharacters(Set<Character> characters) {
// this.characters = characters;
// }
//
// @ElementCollection
// @Enumerated(EnumType.STRING)
// @MapKeyColumn(nullable=false)
// public Map<String, FavoriteFood> getFavoriteFood() {
// return foods;
// }
//
// public void setFavoriteFood(Map<String, FavoriteFood>foods) {
// this.foods = foods;
// }
//
// @ElementCollection(fetch = FetchType.EAGER)
// //@Where(clause = "b_likes=false")
// public Set<CountryAttitude> getCountryAttitudes() {
// return countryAttitudes;
// }
//
// public void setCountryAttitudes(Set<CountryAttitude> countryAttitudes) {
// this.countryAttitudes = countryAttitudes;
// }
@ElementCollection
@Enumerated(EnumType.STRING)
@Column(name = "`characters`")
public Set<Character> getCharacters() {
return characters;
}
public void setCharacters(Set<Character> characters) {
this.characters = characters;
}
@ElementCollection
@Enumerated(EnumType.STRING)
@MapKeyColumn(nullable=false)
public Map<String, FavoriteFood> getFavoriteFood() {
return foods;
}
public void setFavoriteFood(Map<String, FavoriteFood>foods) {
this.foods = foods;
}
@ElementCollection(fetch = FetchType.EAGER)
//@Where(clause = "b_likes=false")
public Set<CountryAttitude> getCountryAttitudes() {
return countryAttitudes;
}
public void setCountryAttitudes(Set<CountryAttitude> countryAttitudes) {
this.countryAttitudes = countryAttitudes;
}
}

View File

@ -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<String,FavoriteFood> foods = new HashMap<String,FavoriteFood>();
// 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<String,FavoriteFood> foods = new HashMap<String,FavoriteFood>();
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<PluralAttributeBinding> 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<PluralAttributeBinding> 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
};
}
}

View File

@ -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;

View File

@ -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() {