HHH-4771 @ElementCollection fk column should default to entityName_columnNameOfOwningId

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18455 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2010-01-08 16:58:56 +00:00
parent 1b9394446c
commit e44b5f197d
4 changed files with 22 additions and 4 deletions

View File

@ -62,6 +62,11 @@ public class Ejb3JoinColumn extends Ejb3Column {
//table name on the mapped by side if any //table name on the mapped by side if any
private String mappedByTableName; private String mappedByTableName;
private String mappedByEntityName; private String mappedByEntityName;
private boolean JPA2ElementCollection;
public void setJPA2ElementCollection(boolean JPA2ElementCollection) {
this.JPA2ElementCollection = JPA2ElementCollection;
}
//FIXME hacky solution to get the information at property ref resolution //FIXME hacky solution to get the information at property ref resolution
public String getManyToManyOwnerSideEntityName() { public String getManyToManyOwnerSideEntityName() {
@ -398,10 +403,12 @@ public class Ejb3JoinColumn extends Ejb3Column {
if ( mappedBySide ) { if ( mappedBySide ) {
String unquotedMappedbyTable = StringHelper.unquote( mappedByTableName ); String unquotedMappedbyTable = StringHelper.unquote( mappedByTableName );
final String ownerObjectName = JPA2ElementCollection && mappedByEntityName != null ?
StringHelper.unqualify( mappedByEntityName ) : unquotedMappedbyTable;
columnName = getMappings().getNamingStrategy().foreignKeyColumnName( columnName = getMappings().getNamingStrategy().foreignKeyColumnName(
mappedByPropertyName, mappedByPropertyName,
mappedByEntityName, mappedByEntityName,
unquotedMappedbyTable, ownerObjectName,
unquotedLogicalReferenceColumn unquotedLogicalReferenceColumn
); );
//one element was quoted so we quote //one element was quoted so we quote

View File

@ -36,6 +36,7 @@ import javax.persistence.ElementCollection;
import javax.persistence.Embeddable; import javax.persistence.Embeddable;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.JoinTable; import javax.persistence.JoinTable;
import javax.persistence.ManyToMany; import javax.persistence.ManyToMany;
import javax.persistence.MapKey; import javax.persistence.MapKey;
@ -468,6 +469,7 @@ public abstract class CollectionBinder {
if (isMappedBy if (isMappedBy
&& (property.isAnnotationPresent( JoinColumn.class ) && (property.isAnnotationPresent( JoinColumn.class )
|| property.isAnnotationPresent( JoinColumns.class )
|| property.isAnnotationPresent( JoinTable.class ) ) ) { || property.isAnnotationPresent( JoinTable.class ) ) ) {
String message = "Associations marked as mappedBy must not define database mappings like @JoinTable or @JoinColumn: "; String message = "Associations marked as mappedBy must not define database mappings like @JoinTable or @JoinColumn: ";
message += StringHelper.qualify( propertyHolder.getPath(), propertyName ); message += StringHelper.qualify( propertyHolder.getPath(), propertyName );
@ -1415,6 +1417,9 @@ public abstract class CollectionBinder {
joinColumns, collValue.getOwner(), collectionEntity, collValue, false, mappings joinColumns, collValue.getOwner(), collectionEntity, collValue, false, mappings
); );
SimpleValue key = buildCollectionKey( collValue, joinColumns, cascadeDeleteEnabled, property, mappings ); SimpleValue key = buildCollectionKey( collValue, joinColumns, cascadeDeleteEnabled, property, mappings );
if ( property.isAnnotationPresent( ElementCollection.class ) && joinColumns.length > 0 ) {
joinColumns[0].setJPA2ElementCollection( true );
}
TableBinder.bindFk( collValue.getOwner(), collectionEntity, joinColumns, key, false, mappings ); TableBinder.bindFk( collValue.getOwner(), collectionEntity, joinColumns, key, false, mappings );
} }

View File

@ -18,6 +18,7 @@ import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.OrderColumn; import javax.persistence.OrderColumn;
import javax.persistence.Table;
import org.hibernate.annotations.CollectionOfElements; import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.test.annotations.collectionelement.FavoriteFood; import org.hibernate.test.annotations.collectionelement.FavoriteFood;
@ -31,6 +32,7 @@ import org.hibernate.test.annotations.collectionelement.FavoriteFood;
@AttributeOverride( name="scorePerNickName.element", column = @Column(name="fld_score") ), @AttributeOverride( name="scorePerNickName.element", column = @Column(name="fld_score") ),
@AttributeOverride( name="favoriteToys.element.brand.surname", column = @Column(name = "fld_surname"))} @AttributeOverride( name="favoriteToys.element.brand.surname", column = @Column(name = "fld_surname"))}
) )
@Table(name="tbl_Boys")
public class Boy { public class Boy {
private Integer id; private Integer id;
private String firstName; private String firstName;

View File

@ -218,19 +218,23 @@ public class CollectionElementTest extends TestCase {
public void testDefaultValueColumnForBasic() throws Exception { public void testDefaultValueColumnForBasic() throws Exception {
isDefaultValueCollectionColumnPresent( Boy.class.getName(), "hatedNames" ); isDefaultValueCollectionColumnPresent( Boy.class.getName(), "hatedNames" );
isDefaultValueCollectionColumnPresent( Boy.class.getName(), "preferredNames" ); isDefaultValueCollectionColumnPresent( Boy.class.getName(), "preferredNames" );
isValueCollectionColumnPresent( Boy.class.getName(), "nickNames", "element" ); isCollectionColumnPresent( Boy.class.getName(), "nickNames", "element" );
isDefaultValueCollectionColumnPresent( Boy.class.getName(), "scorePerPreferredName"); isDefaultValueCollectionColumnPresent( Boy.class.getName(), "scorePerPreferredName");
} }
public void testDefaultFKNameForElementCollection() throws Exception {
isCollectionColumnPresent( Boy.class.getName(), "hatedNames", "Boy_id" );
}
private void isLegacyValueCollectionColumnPresent(String collectionHolder, String propertyName) { private void isLegacyValueCollectionColumnPresent(String collectionHolder, String propertyName) {
} }
private void isDefaultValueCollectionColumnPresent(String collectionOwner, String propertyName) { private void isDefaultValueCollectionColumnPresent(String collectionOwner, String propertyName) {
isValueCollectionColumnPresent( collectionOwner, propertyName, propertyName ); isCollectionColumnPresent( collectionOwner, propertyName, propertyName );
} }
private void isValueCollectionColumnPresent(String collectionOwner, String propertyName, String columnName) { private void isCollectionColumnPresent(String collectionOwner, String propertyName, String columnName) {
final Collection collection = getCfg().getCollectionMapping( collectionOwner + "." + propertyName ); final Collection collection = getCfg().getCollectionMapping( collectionOwner + "." + propertyName );
final Iterator columnIterator = collection.getCollectionTable().getColumnIterator(); final Iterator columnIterator = collection.getCollectionTable().getColumnIterator();
boolean hasDefault = false; boolean hasDefault = false;