HHH-4353 support JPA 2 default column naming for collections of basic types (and still honor the legacy approach)

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18428 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2010-01-07 13:56:47 +00:00
parent b24ea51873
commit 8b061f9933
4 changed files with 70 additions and 7 deletions

View File

@ -1552,7 +1552,9 @@ public final class AnnotationBinder {
collectionBinder.setAccessType( inferredData.getDefaultAccess() );
Ejb3Column[] elementColumns;
PropertyData virtualProperty = new WrappedInferredData( inferredData, "element" );
//do not use "element" if you are a JPA 2 @ElementCollection only for legacy Hibernate mappings
boolean isJPA2ForValueMapping = property.isAnnotationPresent( ElementCollection.class );
PropertyData virtualProperty = isJPA2ForValueMapping ? inferredData : new WrappedInferredData( inferredData, "element" );
if ( property.isAnnotationPresent( Column.class ) || property.isAnnotationPresent(
Formula.class
) ) {

View File

@ -36,7 +36,10 @@ public class Boy {
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<Toy> favoriteToys = new HashSet<Toy>();
private Set<Character> characters = new HashSet<Character>();
@ -78,6 +81,34 @@ public class Boy {
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
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)

View File

@ -2,6 +2,7 @@
package org.hibernate.test.annotations.collectionelement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
@ -9,8 +10,11 @@ import org.hibernate.Filter;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.Column;
import org.hibernate.test.annotations.Country;
import org.hibernate.test.annotations.TestCase;
import org.hibernate.util.StringHelper;
/**
* @author Emmanuel Bernard
@ -212,6 +216,32 @@ public class CollectionElementTest extends TestCase {
s.close();
}
public void testDefaultValueColumnForBasic() throws Exception {
isDefaultValueCollectionColumnPresent( Boy.class.getName(), "hatedNames" );
isDefaultValueCollectionColumnPresent( Boy.class.getName(), "preferredNames" );
isValueCollectionColumnPresent( Boy.class.getName(), "nickNames", "element" );
isDefaultValueCollectionColumnPresent( Boy.class.getName(), "scorePerPreferredName");
}
private void isLegacyValueCollectionColumnPresent(String collectionHolder, String propertyName) {
}
private void isDefaultValueCollectionColumnPresent(String collectionOwner, String propertyName) {
isValueCollectionColumnPresent( collectionOwner, propertyName, propertyName );
}
private void isValueCollectionColumnPresent(String collectionOwner, String propertyName, String columnName) {
final Collection collection = getCfg().getCollectionMapping( collectionOwner + "." + propertyName );
final Iterator columnIterator = collection.getCollectionTable().getColumnIterator();
boolean hasDefault = false;
while ( columnIterator.hasNext() ) {
Column column = (Column) columnIterator.next();
if ( columnName.equals( column.getName() ) ) hasDefault = true;
}
assertTrue( "Could not find " + columnName, hasDefault );
}
protected Class[] getMappings() {
return new Class[] {
Boy.class,

View File

@ -33,13 +33,13 @@ public class IndexedCollectionTest extends TestCase {
isDefaultKeyColumnPresent( Drawer.class.getName(), "dresses", "_ORDER" );
}
private void isDefaultKeyColumnPresent(String collectionRole, String propertyName, String suffix) {
private void isDefaultKeyColumnPresent(String collectionOwner, String propertyName, String suffix) {
assertTrue( "Could not find " + propertyName + suffix,
isDefaultColumnPresent(collectionRole, propertyName, suffix) );
isDefaultColumnPresent(collectionOwner, propertyName, suffix) );
}
private boolean isDefaultColumnPresent(String collectionRole, String propertyName, String suffix) {
final Collection collection = getCfg().getCollectionMapping( collectionRole + "." + propertyName );
private boolean isDefaultColumnPresent(String collectionOwner, String propertyName, String suffix) {
final Collection collection = getCfg().getCollectionMapping( collectionOwner + "." + propertyName );
final Iterator columnIterator = collection.getCollectionTable().getColumnIterator();
boolean hasDefault = false;
while ( columnIterator.hasNext() ) {
@ -49,9 +49,9 @@ public class IndexedCollectionTest extends TestCase {
return hasDefault;
}
private void isNotDefaultKeyColumnPresent(String collectionRole, String propertyName, String suffix) {
private void isNotDefaultKeyColumnPresent(String collectionOwner, String propertyName, String suffix) {
assertFalse( "Could not find " + propertyName + suffix,
isDefaultColumnPresent(collectionRole, propertyName, suffix) );
isDefaultColumnPresent(collectionOwner, propertyName, suffix) );
}
public void testFkList() throws Exception {