diff --git a/hibernate-core/src/main/java/org/hibernate/type/EntityType.java b/hibernate-core/src/main/java/org/hibernate/type/EntityType.java index 7924c203bc40..18d2e1198819 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/EntityType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/EntityType.java @@ -201,7 +201,9 @@ public boolean isReferenceToPrimaryKey() { } public String getRHSUniqueKeyPropertyName() { - return uniqueKeyPropertyName; + // Return null if this type references a PK. This is important for + // associations' use of mappedBy referring to a derived ID. + return referenceToPrimaryKey ? null : uniqueKeyPropertyName; } public String getLHSPropertyName() { diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/derivedidentities/bidirectional/OneToOneWithDerivedIdentityTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/derivedidentities/bidirectional/OneToOneWithDerivedIdentityTest.java index a63ca1e92e3b..1b35c15bc88e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/derivedidentities/bidirectional/OneToOneWithDerivedIdentityTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/derivedidentities/bidirectional/OneToOneWithDerivedIdentityTest.java @@ -26,6 +26,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import java.util.List; + +import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; @@ -80,11 +83,50 @@ public void testSelectWithDerivedId() { s.close(); } + @Test + @TestForIssue(jiraKey = "HHH-6813") + // Regression test utilizing multiple types of queries. + public void testCase() { + Session s = openSession(); + s.getTransaction().begin(); + + Person p = new Person(); + p.setName( "Alfio" ); + PersonInfo pi = new PersonInfo(); + pi.setId( p ); + pi.setInfo( "Some information" ); + s.persist( p ); + s.persist( pi ); + + s.getTransaction().commit(); + s.clear(); + + s.getTransaction().begin(); + + Query q = s.getNamedQuery( "PersonQuery" ); + List persons = q.list(); + assertEquals( persons.size(), 1 ); + assertEquals( persons.get( 0 ).getName(), "Alfio" ); + + s.getTransaction().commit(); + s.clear(); + + s.getTransaction().begin(); + + p = (Person) s.get( Person.class, persons.get( 0 ).getId() ); + assertEquals( p.getName(), "Alfio" ); + + s.getTransaction().commit(); + s.close(); + } + @Override protected Class[] getAnnotatedClasses() { return new Class[] { Foo.class, - Bar.class + Bar.class, + Person.class, + PersonInfo.class }; } diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/derivedidentities/bidirectional/Person.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/derivedidentities/bidirectional/Person.java new file mode 100644 index 000000000000..77a0727489de --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/derivedidentities/bidirectional/Person.java @@ -0,0 +1,77 @@ +package org.hibernate.test.annotations.derivedidentities.bidirectional; + +import java.io.Serializable; +import javax.persistence.Basic; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.NamedQuery; +import javax.persistence.OneToOne; + +@Entity +@NamedQuery(name="PersonQuery", query="SELECT p FROM Person p") +public class Person + implements Serializable +{ + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + private Integer id; + + @Basic + private String name; + + @OneToOne(mappedBy="id") + private PersonInfo personInfo; + + public Integer getId() + { + return this.id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public int hashCode() + { + int hash = 0; + hash += (this.id != null ? this.id.hashCode() : 0); + return hash; + } + + public boolean equals(Object object) + { + if (!(object instanceof Person)) { + return false; + } + Person other = (Person)object; + + return ((this.id != null) || (other.id == null)) && ((this.id == null) || (this.id.equals(other.id))); + } + + public String toString() + { + return "nogroup.hibertest.Person[ id=" + this.id + " ]"; + } + + public PersonInfo getPersonInfo() + { + return this.personInfo; + } + + public void setPersonInfo(PersonInfo personInfo) + { + this.personInfo = personInfo; + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/derivedidentities/bidirectional/PersonInfo.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/derivedidentities/bidirectional/PersonInfo.java new file mode 100644 index 000000000000..08a1ff971495 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/derivedidentities/bidirectional/PersonInfo.java @@ -0,0 +1,60 @@ +package org.hibernate.test.annotations.derivedidentities.bidirectional; + +import java.io.Serializable; +import javax.persistence.Basic; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity +public class PersonInfo + implements Serializable +{ + private static final long serialVersionUID = 1L; + + @Id + @OneToOne + private Person id; + + @Basic + private String info; + + public Person getId() + { + return this.id; + } + + public void setId(Person id) { + this.id = id; + } + + public String getInfo() { + return this.info; + } + + public void setInfo(String info) { + this.info = info; + } + + public int hashCode() + { + int hash = 0; + hash += (this.id != null ? this.id.hashCode() : 0); + return hash; + } + + public boolean equals(Object object) + { + if (!(object instanceof PersonInfo)) { + return false; + } + PersonInfo other = (PersonInfo)object; + + return ((this.id != null) || (other.id == null)) && ((this.id == null) || (this.id.equals(other.id))); + } + + public String toString() + { + return "nogroup.hibertest.PersonInfo[ id=" + this.id + " ]"; + } +} \ No newline at end of file