HHH-4849 honor default join column names for @MapsId properties

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18655 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2010-01-27 18:46:51 +00:00
parent fde085a09a
commit 66072429f4
9 changed files with 75 additions and 10 deletions

View File

@ -1758,6 +1758,12 @@ public final class
propertyBinder.setLazy( lazy );
propertyBinder.setColumns( columns );
if (isOverridden) {
final PropertyData mapsIdProperty = BinderHelper.getPropertyAnnotatedWithMapsId(
isId, propertyHolder, property.getName(), mappings
);
propertyBinder.setReferencedEntityName( mapsIdProperty.getClassOrElementName() );
}
propertyBinder.makePropertyValueAndBind();

View File

@ -365,6 +365,7 @@ public class AnnotationConfiguration extends Configuration {
caches.clear();
try {
inSecondPass = true;
processSecondPassesOfType( PkDrivenByDefaultMapsIdSecondPass.class );
processSecondPassesOfType( SetSimpleValueTypeSecondPass.class );
processSecondPassesOfType( CopyIdentifierComponentSecondPass.class );
processFkSecondPassInOrder();

View File

@ -189,9 +189,9 @@ class ColumnsBuilder {
if ( annotatedWithMapsId != null ) {
result = buildExplicitJoinColumns( annotatedWithMapsId.getProperty(), annotatedWithMapsId );
if (result == null) {
//result = buildDefaultJoinColumnsForXToOne( annotatedWithMapsId.getProperty(), annotatedWithMapsId);
throw new UnsupportedOperationException( "Implicit @JoinColumn is not supported on @MapsId properties: "
+ annotatedWithMapsId.getDeclaringClass() + " " + annotatedWithMapsId.getPropertyName() );
result = buildDefaultJoinColumnsForXToOne( annotatedWithMapsId.getProperty(), annotatedWithMapsId);
// throw new UnsupportedOperationException( "Implicit @JoinColumn is not supported on @MapsId properties: "
// + annotatedWithMapsId.getDeclaringClass() + " " + annotatedWithMapsId.getPropertyName() );
}
}
return result;

View File

@ -0,0 +1,38 @@
package org.hibernate.cfg;
import java.util.Map;
import org.hibernate.AnnotationException;
import org.hibernate.MappingException;
import org.hibernate.cfg.annotations.TableBinder;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.SimpleValue;
/**
* @author Emmanuel Bernard
*/
public class PkDrivenByDefaultMapsIdSecondPass implements SecondPass {
private final String referencedEntityName;
private final Ejb3JoinColumn[] columns;
private final SimpleValue value;
public PkDrivenByDefaultMapsIdSecondPass(String referencedEntityName, Ejb3JoinColumn[] columns, SimpleValue value) {
this.referencedEntityName = referencedEntityName;
this.columns = columns;
this.value = value;
}
public void doSecondPass(Map persistentClasses) throws MappingException {
PersistentClass referencedEntity = (PersistentClass) persistentClasses.get( referencedEntityName );
if ( referencedEntity == null ) {
throw new AnnotationException(
"Unknown entity name: " + referencedEntityName
);
};
TableBinder.linkJoinColumnWithValueOverridingNameIfImplicit(
referencedEntity,
referencedEntity.getKey().getColumnIterator(),
columns,
value);
}
}

View File

@ -78,6 +78,11 @@ public class PropertyBinder {
private boolean embedded;
private EntityBinder entityBinder;
private boolean isXToMany;
private String referencedEntityName;
public void setReferencedEntityName(String referencedEntityName) {
this.referencedEntityName = referencedEntityName;
}
public void setEmbedded(boolean embedded) {
this.embedded = embedded;
@ -179,6 +184,7 @@ public class PropertyBinder {
simpleValueBinder.setPersistentClassName( containerClassName );
simpleValueBinder.setType( property, returnedClass );
simpleValueBinder.setMappings( mappings );
simpleValueBinder.setReferencedEntityName( referencedEntityName );
SimpleValue propertyValue = simpleValueBinder.make();
setValue( propertyValue );
return makeProperty();

View File

@ -45,8 +45,10 @@ import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.cfg.BinderHelper;
import org.hibernate.cfg.Ejb3Column;
import org.hibernate.cfg.Ejb3JoinColumn;
import org.hibernate.cfg.ExtendedMappings;
import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.cfg.PkDrivenByDefaultMapsIdSecondPass;
import org.hibernate.cfg.SecondPass;
import org.hibernate.cfg.SetSimpleValueTypeSecondPass;
import org.hibernate.mapping.SimpleValue;
@ -77,6 +79,11 @@ public class SimpleValueBinder {
private boolean isVersion;
//is a Map key
private boolean key;
private String referencedEntityName;
public void setReferencedEntityName(String referencedEntityName) {
this.referencedEntityName = referencedEntityName;
}
public boolean isVersion() {
return isVersion;
@ -307,8 +314,15 @@ public class SimpleValueBinder {
}
public void linkWithValue() {
for ( Ejb3Column column : columns) {
column.linkWithValue( simpleValue );
if ( columns[0].isNameDeferred() && ! mappings.isInSecondPass() && referencedEntityName != null) {
mappings.addSecondPass(
new PkDrivenByDefaultMapsIdSecondPass( referencedEntityName, ( Ejb3JoinColumn[]) columns, simpleValue)
);
}
else {
for ( Ejb3Column column : columns) {
column.linkWithValue( simpleValue );
}
}
}

View File

@ -474,7 +474,7 @@ public class TableBinder {
}
}
private static void linkJoinColumnWithValueOverridingNameIfImplicit(
public static void linkJoinColumnWithValueOverridingNameIfImplicit(
PersistentClass referencedEntity, Iterator columnIterator, Ejb3JoinColumn[] columns, SimpleValue value
) {
for (Ejb3JoinColumn joinCol : columns) {

View File

@ -14,7 +14,7 @@ public class Dependent {
@EmbeddedId
DependentId id;
@JoinColumn(name="FK") // id attribute mapped by join column default
//@JoinColumn(name="FK") // id attribute mapped by join column default
@MapsId("empPK") // maps empPK attribute of embedded id
@ManyToOne
Employee emp;

View File

@ -11,19 +11,19 @@ public class
DerivedIdentitySimpleParentEmbeddedIdDepTest extends TestCase {
public void testManyToOne() throws Exception {
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "FK", getCfg() ) );
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "emp_empId", getCfg() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "Dependent", "empPK", getCfg() ) );
Employee e = new Employee();
e.empId = 1;
e.empName = "Emmanuel";
Session s = openSession( );
s.getTransaction().begin();
s.persist( e );
Dependent d = new Dependent();
d.emp = e;
d.id = new DependentId();
d.id.name = "Doggy";
//d.id.empPK = e.empId; //FIXME not needed when foreign is enabled
s.persist( e );
s.persist( d );
s.flush();
s.clear();