HHH-6813 @Id @OneToOne cause NullPointerException during query

This commit is contained in:
Brett Meyer 2013-05-10 12:53:28 -04:00
parent 5373be01df
commit cf91fd7a49
2 changed files with 45 additions and 5 deletions

View File

@ -32,6 +32,7 @@ import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.cfg.annotations.PropertyBinder; import org.hibernate.cfg.annotations.PropertyBinder;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.mapping.Column; import org.hibernate.mapping.Column;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.DependantValue; import org.hibernate.mapping.DependantValue;
import org.hibernate.mapping.Join; import org.hibernate.mapping.Join;
import org.hibernate.mapping.ManyToOne; import org.hibernate.mapping.ManyToOne;
@ -212,7 +213,23 @@ public class OneToOneSecondPass implements SecondPass {
propertyHolder.addProperty( prop, inferredData.getDeclaringClass() ); propertyHolder.addProperty( prop, inferredData.getDeclaringClass() );
} }
value.setReferencedPropertyName( mappedBy ); // HHH-6813
// If otherSide's id is derived, do not set EntityType#uniqueKeyPropertyName.
// EntityType#isReferenceToPrimaryKey() assumes that, if it's set,
// a PK is not referenced. Example:
//
// Foo: @Id long id, @OneToOne(mappedBy="foo") Bar bar
// Bar: @Id @OneToOne Foo foo
boolean referencesDerivedId = false;
try {
referencesDerivedId = otherSide.getIdentifier() instanceof Component
&& ( (Component) otherSide.getIdentifier() ).getProperty( mappedBy ) != null;
}
catch ( MappingException e ) {
// ignore
}
String referencedPropertyName = referencesDerivedId ? null : mappedBy;
value.setReferencedPropertyName( referencedPropertyName );
String propertyRef = value.getReferencedPropertyName(); String propertyRef = value.getReferencedPropertyName();
if ( propertyRef != null ) { if ( propertyRef != null ) {

View File

@ -23,14 +23,14 @@
*/ */
package org.hibernate.test.annotations.derivedidentities.bidirectional; package org.hibernate.test.annotations.derivedidentities.bidirectional;
import org.junit.Test; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.testing.FailureExpected; import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class OneToOneWithDerivedIdentityTest extends BaseCoreFunctionalTestCase { public class OneToOneWithDerivedIdentityTest extends BaseCoreFunctionalTestCase {
@Test @Test
@ -57,6 +57,29 @@ public class OneToOneWithDerivedIdentityTest extends BaseCoreFunctionalTestCase
s.getTransaction().rollback(); s.getTransaction().rollback();
s.close(); s.close();
} }
@Test
@TestForIssue(jiraKey = "HHH-6813")
public void testSelectWithDerivedId() {
Session s = openSession();
s.beginTransaction();
Bar bar = new Bar();
bar.setDetails( "Some details" );
Foo foo = new Foo();
foo.setBar( bar );
bar.setFoo( foo );
s.persist( foo );
s.flush();
assertNotNull( foo.getId() );
assertEquals( foo.getId(), bar.getFoo().getId() );
s.clear();
Foo newFoo = (Foo) s.createQuery( "SELECT f FROM Foo f" ).uniqueResult();
assertNotNull( newFoo );
assertEquals( "Some details", newFoo.getBar().getDetails() );
s.getTransaction().rollback();
s.close();
}
@Override @Override
protected Class<?>[] getAnnotatedClasses() { protected Class<?>[] getAnnotatedClasses() {