Skip to content

Commit e676ebe

Browse files
stringintechmbellade
authored andcommitted
HHH-18764 Fix incorrect type resolution in ManyToOneType dirty check
1 parent b5fb125 commit e676ebe

File tree

2 files changed

+197
-2
lines changed

2 files changed

+197
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public boolean isDirty(
250250
}
251251
Object oldid = getIdentifier( old, session );
252252
Object newid = getIdentifier( current, session );
253-
return getIdentifierType( session ).isDirty( oldid, newid, session );
253+
return getIdentifierOrUniqueKeyType( session.getFactory() ).isDirty( oldid, newid, session );
254254
}
255255

256256
@Override
@@ -268,7 +268,7 @@ public boolean isDirty(
268268
}
269269
Object oldid = getIdentifier( old, session );
270270
Object newid = getIdentifier( current, session );
271-
return getIdentifierType( session ).isDirty( oldid, newid, checkable, session );
271+
return getIdentifierOrUniqueKeyType( session.getFactory() ).isDirty( oldid, newid, checkable, session );
272272
}
273273

274274
}
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.associations;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.FetchType;
9+
import jakarta.persistence.GeneratedValue;
10+
import jakarta.persistence.Id;
11+
import jakarta.persistence.JoinColumn;
12+
import jakarta.persistence.ManyToOne;
13+
import org.hibernate.HibernateException;
14+
import org.hibernate.annotations.NaturalId;
15+
import org.hibernate.annotations.Type;
16+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
17+
import org.hibernate.testing.orm.junit.DomainModel;
18+
import org.hibernate.testing.orm.junit.JiraKey;
19+
import org.hibernate.testing.orm.junit.SessionFactory;
20+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
21+
import org.hibernate.usertype.EnhancedUserType;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.io.Serializable;
25+
import java.sql.PreparedStatement;
26+
import java.sql.ResultSet;
27+
import java.sql.SQLException;
28+
import java.sql.Types;
29+
30+
/**
31+
* @author Kowsar Atazadeh
32+
*/
33+
@SessionFactory
34+
@DomainModel(annotatedClasses =
35+
{ManyToOneUniqueKeyReferenceWithCustomIdTest.Phone.class, ManyToOneUniqueKeyReferenceWithCustomIdTest.User.class})
36+
@JiraKey("HHH-18764")
37+
public class ManyToOneUniqueKeyReferenceWithCustomIdTest {
38+
39+
@Test
40+
void test(SessionFactoryScope scope) {
41+
scope.inTransaction( session -> {
42+
Phone phone = new Phone();
43+
44+
User user1 = new User( new CustomId( "u1" ), "Kowsar" );
45+
session.persist( user1 );
46+
phone.setUser( user1 );
47+
session.persist( phone );
48+
49+
User user2 = new User( new CustomId( "u2" ), "Someone" );
50+
session.persist( user2 );
51+
phone.setUser( user2 );
52+
session.persist( phone );
53+
} );
54+
}
55+
56+
@Entity(name = "Phone")
57+
static class Phone {
58+
@Id
59+
@GeneratedValue
60+
Long id;
61+
62+
@ManyToOne(fetch = FetchType.LAZY)
63+
@JoinColumn(referencedColumnName = "name", nullable = false)
64+
User user;
65+
66+
public Long getId() {
67+
return id;
68+
}
69+
70+
public void setId(Long id) {
71+
this.id = id;
72+
}
73+
74+
public User getUser() {
75+
return user;
76+
}
77+
78+
public void setUser(User user) {
79+
this.user = user;
80+
}
81+
}
82+
83+
@Entity(name = "_User")
84+
static class User {
85+
@Id
86+
@Type(CustomIdType.class)
87+
CustomId id;
88+
89+
@NaturalId
90+
String name;
91+
92+
public User() {
93+
}
94+
95+
public User(CustomId id, String name) {
96+
this.id = id;
97+
this.name = name;
98+
}
99+
100+
public String getName() {
101+
return name;
102+
}
103+
104+
public void setName(String name) {
105+
this.name = name;
106+
}
107+
108+
public CustomId getId() {
109+
return id;
110+
}
111+
112+
public void setId(CustomId id) {
113+
this.id = id;
114+
}
115+
}
116+
117+
static class CustomIdType implements EnhancedUserType<CustomId> {
118+
@Override
119+
public String toSqlLiteral(CustomId value) {
120+
return "'" + value.toString() + "'";
121+
}
122+
123+
@Override
124+
public String toString(CustomId value) throws HibernateException {
125+
return value.toString();
126+
}
127+
128+
@Override
129+
public void nullSafeSet(PreparedStatement st, CustomId value, int position,
130+
SharedSessionContractImplementor session) throws SQLException {
131+
st.setObject( position, value.toString(), getSqlType() );
132+
}
133+
134+
@Override
135+
public CustomId nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session)
136+
throws SQLException {
137+
String idValue = rs.getString( position );
138+
return idValue != null ? fromStringValue( idValue ) : null;
139+
}
140+
141+
@Override
142+
public CustomId fromStringValue(CharSequence sequence) throws HibernateException {
143+
return new CustomId( sequence.toString() );
144+
}
145+
146+
@Override
147+
public int getSqlType() {
148+
return Types.VARCHAR;
149+
}
150+
151+
@Override
152+
public Class<CustomId> returnedClass() {
153+
return CustomId.class;
154+
}
155+
156+
@Override
157+
public CustomId deepCopy(CustomId value) {
158+
return new CustomId( value.getId() );
159+
}
160+
161+
@Override
162+
public boolean isMutable() {
163+
return false;
164+
}
165+
166+
@Override
167+
public boolean equals(CustomId x, CustomId y) {
168+
return EnhancedUserType.super.equals( x, y );
169+
}
170+
}
171+
172+
static class CustomId implements Serializable {
173+
private String id;
174+
175+
public CustomId() {
176+
}
177+
178+
public CustomId(String id) {
179+
this.id = id;
180+
}
181+
182+
@Override
183+
public String toString() {
184+
return id;
185+
}
186+
187+
public String getId() {
188+
return id;
189+
}
190+
191+
public void setId(String id) {
192+
this.id = id;
193+
}
194+
}
195+
}

0 commit comments

Comments
 (0)