HHH-6813 Corrected EntityType#getRHSUniqueKeyPropertyName() and added

regression test.
This commit is contained in:
Brett Meyer 2013-05-20 11:18:46 -04:00
parent 335ddeaefc
commit 6f6e3c62b2
4 changed files with 183 additions and 2 deletions

View File

@ -200,7 +200,9 @@ public abstract class EntityType extends AbstractType implements AssociationType
}
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() {

View File

@ -26,6 +26,9 @@ package org.hibernate.test.annotations.derivedidentities.bidirectional;
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 class OneToOneWithDerivedIdentityTest extends BaseCoreFunctionalTestCase
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<Person> 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
};
}

View File

@ -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;
}
}

View File

@ -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 + " ]";
}
}