diff --git a/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java index 665ef33cd2..2f702d241d 100644 --- a/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -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 idColumnNames = new ArrayList(); 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; + } } } } diff --git a/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e4/a/DerivedIdentitySimpleParentSimpleDepTest.java b/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e4/a/DerivedIdentitySimpleParentSimpleDepTest.java index 4fd416aab8..641e7846e8 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e4/a/DerivedIdentitySimpleParentSimpleDepTest.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e4/a/DerivedIdentitySimpleParentSimpleDepTest.java @@ -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(); } diff --git a/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e4/a/MedicalHistory.java b/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e4/a/MedicalHistory.java index f4c008a85f..7a695e2755 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e4/a/MedicalHistory.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/derivedidentities/e4/a/MedicalHistory.java @@ -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