Skip to content

Commit 6f6e3c6

Browse files
committed
HHH-6813 Corrected EntityType#getRHSUniqueKeyPropertyName() and added
regression test.
1 parent 335ddea commit 6f6e3c6

File tree

4 files changed

+183
-2
lines changed

4 files changed

+183
-2
lines changed

hibernate-core/src/main/java/org/hibernate/type/EntityType.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ public boolean isReferenceToPrimaryKey() {
200200
}
201201

202202
public String getRHSUniqueKeyPropertyName() {
203-
return uniqueKeyPropertyName;
203+
// Return null if this type references a PK. This is important for
204+
// associations' use of mappedBy referring to a derived ID.
205+
return referenceToPrimaryKey ? null : uniqueKeyPropertyName;
204206
}
205207

206208
public String getLHSPropertyName() {

hibernate-core/src/test/java/org/hibernate/test/annotations/derivedidentities/bidirectional/OneToOneWithDerivedIdentityTest.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import static org.junit.Assert.assertEquals;
2727
import static org.junit.Assert.assertNotNull;
2828

29+
import java.util.List;
30+
31+
import org.hibernate.Query;
2932
import org.hibernate.Session;
3033
import org.hibernate.testing.TestForIssue;
3134
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
@@ -80,11 +83,50 @@ public void testSelectWithDerivedId() {
8083
s.close();
8184
}
8285

86+
@Test
87+
@TestForIssue(jiraKey = "HHH-6813")
88+
// Regression test utilizing multiple types of queries.
89+
public void testCase() {
90+
Session s = openSession();
91+
s.getTransaction().begin();
92+
93+
Person p = new Person();
94+
p.setName( "Alfio" );
95+
PersonInfo pi = new PersonInfo();
96+
pi.setId( p );
97+
pi.setInfo( "Some information" );
98+
s.persist( p );
99+
s.persist( pi );
100+
101+
s.getTransaction().commit();
102+
s.clear();
103+
104+
s.getTransaction().begin();
105+
106+
Query q = s.getNamedQuery( "PersonQuery" );
107+
List<Person> persons = q.list();
108+
assertEquals( persons.size(), 1 );
109+
assertEquals( persons.get( 0 ).getName(), "Alfio" );
110+
111+
s.getTransaction().commit();
112+
s.clear();
113+
114+
s.getTransaction().begin();
115+
116+
p = (Person) s.get( Person.class, persons.get( 0 ).getId() );
117+
assertEquals( p.getName(), "Alfio" );
118+
119+
s.getTransaction().commit();
120+
s.close();
121+
}
122+
83123
@Override
84124
protected Class<?>[] getAnnotatedClasses() {
85125
return new Class<?>[] {
86126
Foo.class,
87-
Bar.class
127+
Bar.class,
128+
Person.class,
129+
PersonInfo.class
88130
};
89131
}
90132

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.hibernate.test.annotations.derivedidentities.bidirectional;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Basic;
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.GenerationType;
8+
import javax.persistence.Id;
9+
import javax.persistence.NamedQuery;
10+
import javax.persistence.OneToOne;
11+
12+
@Entity
13+
@NamedQuery(name="PersonQuery", query="SELECT p FROM Person p")
14+
public class Person
15+
implements Serializable
16+
{
17+
private static final long serialVersionUID = 1L;
18+
19+
@Id
20+
@GeneratedValue(strategy=GenerationType.AUTO)
21+
private Integer id;
22+
23+
@Basic
24+
private String name;
25+
26+
@OneToOne(mappedBy="id")
27+
private PersonInfo personInfo;
28+
29+
public Integer getId()
30+
{
31+
return this.id;
32+
}
33+
34+
public void setId(Integer id) {
35+
this.id = id;
36+
}
37+
38+
public String getName() {
39+
return this.name;
40+
}
41+
42+
public void setName(String name) {
43+
this.name = name;
44+
}
45+
46+
public int hashCode()
47+
{
48+
int hash = 0;
49+
hash += (this.id != null ? this.id.hashCode() : 0);
50+
return hash;
51+
}
52+
53+
public boolean equals(Object object)
54+
{
55+
if (!(object instanceof Person)) {
56+
return false;
57+
}
58+
Person other = (Person)object;
59+
60+
return ((this.id != null) || (other.id == null)) && ((this.id == null) || (this.id.equals(other.id)));
61+
}
62+
63+
public String toString()
64+
{
65+
return "nogroup.hibertest.Person[ id=" + this.id + " ]";
66+
}
67+
68+
public PersonInfo getPersonInfo()
69+
{
70+
return this.personInfo;
71+
}
72+
73+
public void setPersonInfo(PersonInfo personInfo)
74+
{
75+
this.personInfo = personInfo;
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.hibernate.test.annotations.derivedidentities.bidirectional;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Basic;
5+
import javax.persistence.Entity;
6+
import javax.persistence.Id;
7+
import javax.persistence.OneToOne;
8+
9+
@Entity
10+
public class PersonInfo
11+
implements Serializable
12+
{
13+
private static final long serialVersionUID = 1L;
14+
15+
@Id
16+
@OneToOne
17+
private Person id;
18+
19+
@Basic
20+
private String info;
21+
22+
public Person getId()
23+
{
24+
return this.id;
25+
}
26+
27+
public void setId(Person id) {
28+
this.id = id;
29+
}
30+
31+
public String getInfo() {
32+
return this.info;
33+
}
34+
35+
public void setInfo(String info) {
36+
this.info = info;
37+
}
38+
39+
public int hashCode()
40+
{
41+
int hash = 0;
42+
hash += (this.id != null ? this.id.hashCode() : 0);
43+
return hash;
44+
}
45+
46+
public boolean equals(Object object)
47+
{
48+
if (!(object instanceof PersonInfo)) {
49+
return false;
50+
}
51+
PersonInfo other = (PersonInfo)object;
52+
53+
return ((this.id != null) || (other.id == null)) && ((this.id == null) || (this.id.equals(other.id)));
54+
}
55+
56+
public String toString()
57+
{
58+
return "nogroup.hibertest.PersonInfo[ id=" + this.id + " ]";
59+
}
60+
}

0 commit comments

Comments
 (0)