HHH-4529 FK constraint is now generated

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18586 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2010-01-20 11:48:08 +00:00
parent 76f2f88038
commit cd3c152f44
3 changed files with 34 additions and 13 deletions

View File

@ -1414,10 +1414,12 @@ public final class AnnotationBinder {
joinColumn.setSecondaryTableName( join.getTable().getName() );
}
}
//MapsId means the columns belong to the pk => not null
final boolean mandatory = !ann.optional() || property.isAnnotationPresent( MapsId.class );
bindOneToOne(
getCascadeStrategy( ann.cascade(), hibernateCascade, ann.orphanRemoval()),
joinColumns,
ann.optional(),
!mandatory,
getFetchMode( ann.fetch() ),
ignoreNotFound, onDeleteCascade,
mappings.getReflectionManager().toXClass( ann.targetEntity() ),
@ -2088,6 +2090,7 @@ public final class AnnotationBinder {
RootClass rootClass = (RootClass) persistentClass;
String persistentClassName = rootClass.getClassName();
SimpleValue id;
final String propertyName = inferredData.getPropertyName();
if ( isComposite ) {
id = fillComponent(
propertyHolder, inferredData, baseInferredData, propertyAccessor,
@ -2126,7 +2129,7 @@ public final class AnnotationBinder {
column.forceNotNull(); //this is an id
}
SimpleValueBinder value = new SimpleValueBinder();
value.setPropertyName( inferredData.getPropertyName() );
value.setPropertyName( propertyName );
value.setReturnedClassName( inferredData.getTypeName() );
value.setColumns( columns );
value.setPersistentClassName( persistentClassName );
@ -2141,7 +2144,7 @@ public final class AnnotationBinder {
}
else {
PropertyBinder binder = new PropertyBinder();
binder.setName( inferredData.getPropertyName() );
binder.setName( propertyName );
binder.setValue( id );
binder.setAccessType( inferredData.getDefaultAccess() );
binder.setProperty( inferredData.getProperty() );
@ -2220,7 +2223,7 @@ public final class AnnotationBinder {
String path = propertyHolder.getPath() + "." + propertyName;
FkSecondPass secondPass = new ToOneFkSecondPass(
value, columns,
!optional && unique, //cannot have nullabe and unique on certain DBs like Derby
!optional && unique, //cannot have nullable and unique on certain DBs like Derby
propertyHolder.getEntityOwnerClassName(),
path, mappings
);
@ -2328,14 +2331,19 @@ public final class AnnotationBinder {
Iterator idColumns = identifier.getColumnIterator();
List<String> idColumnNames = new ArrayList<String>();
org.hibernate.mapping.Column currentColumn;
while ( idColumns.hasNext() ) {
currentColumn = (org.hibernate.mapping.Column) idColumns.next();
idColumnNames.add( currentColumn.getName() );
if ( identifier.getColumnSpan() != joinColumns.length ) {
mapToPK = false;
}
for (Ejb3JoinColumn col : joinColumns) {
if ( !idColumnNames.contains( col.getMappingColumn().getName() ) ) {
mapToPK = false;
break;
else {
while ( idColumns.hasNext() ) {
currentColumn = (org.hibernate.mapping.Column) idColumns.next();
idColumnNames.add( currentColumn.getName() );
}
for (Ejb3JoinColumn col : joinColumns) {
if ( !idColumnNames.contains( col.getMappingColumn().getName() ) ) {
mapToPK = false;
break;
}
}
}
}

View File

@ -1,5 +1,7 @@
package org.hibernate.test.annotations.derivedidentities.e4.a;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.test.annotations.TestCase;
import org.hibernate.test.util.SchemaUtil;
@ -25,6 +27,11 @@ public class DerivedIdentitySimpleParentSimpleDepTest extends TestCase {
s.clear();
d = (MedicalHistory) s.get( MedicalHistory.class, d.id );
assertEquals( d.id, d.patient.ssn );
d.lastupdate = new Date();
s.flush();
s.clear();
d = (MedicalHistory) s.get( MedicalHistory.class, d.id );
assertNotNull( d.lastupdate );
s.getTransaction().rollback();
s.close();
}

View File

@ -1,18 +1,24 @@
package org.hibernate.test.annotations.derivedidentities.e4.a;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* @author Emmanuel Bernard
*/
@Entity
public class MedicalHistory {
@Id
String id; // overriding not allowed ... // default join column name is overridden @MapsId
@Id String id; // overriding not allowed ... // default join column name is overridden @MapsId
@Temporal(TemporalType.DATE)
Date lastupdate;
@JoinColumn(name = "FK")
@MapsId
@OneToOne