Skip to content

Commit 64c3969

Browse files
NathanQingyangXudreab8
authored andcommitted
HHH-15211 fix embeddable basic attribute converter hash code bug
1 parent 1946aea commit 64c3969

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public boolean isEqual(Object one, Object another) {
161161

162162
@Override
163163
@SuppressWarnings("unchecked")
164-
public final int getHashCode(Object x) {
164+
public int getHashCode(Object x) {
165165
return javaType.extractHashCode( (T) x );
166166
}
167167

hibernate-core/src/main/java/org/hibernate/type/descriptor/converter/AttributeConverterTypeAdapter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public JavaType<?> getRelationalJtd() {
8888
return relationalJtd;
8989
}
9090

91-
public JpaAttributeConverter<? extends T,?> getAttributeConverter() {
91+
public JpaAttributeConverter<? extends T, ?> getAttributeConverter() {
9292
return attributeConverter;
9393
}
9494

@@ -121,6 +121,12 @@ public boolean isEqual(Object one, Object another) {
121121
return ( (JavaType<Object>) getDomainJtd() ).areEqual( one, another );
122122
}
123123

124+
@Override
125+
public int getHashCode(Object x) {
126+
//noinspection unchecked
127+
return getDomainJtd().extractHashCode( (T) x );
128+
}
129+
124130
@Override
125131
public String toString() {
126132
return description;
Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
55
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
66
*/
7-
package org.hibernate.orm.test.mapping.converted.converter.elementCollection.embeddable;
7+
package org.hibernate.orm.test.mapping.converted.converter.elementCollection;
88

99
import java.math.BigDecimal;
1010
import java.util.ArrayList;
@@ -15,6 +15,8 @@
1515
import org.hibernate.testing.orm.junit.DomainModel;
1616
import org.hibernate.testing.orm.junit.SessionFactory;
1717
import org.hibernate.testing.orm.junit.SessionFactoryScope;
18+
import org.junit.jupiter.api.AfterEach;
19+
import org.junit.jupiter.api.BeforeEach;
1820
import org.junit.jupiter.api.Test;
1921

2022
import jakarta.persistence.AttributeConverter;
@@ -23,7 +25,6 @@
2325
import jakarta.persistence.Embeddable;
2426
import jakarta.persistence.Entity;
2527
import jakarta.persistence.FetchType;
26-
import jakarta.persistence.GeneratedValue;
2728
import jakarta.persistence.Id;
2829

2930
/**
@@ -32,35 +33,50 @@
3233
*/
3334

3435
@DomainModel(annotatedClasses = {
35-
EmbeddableTests.ProductEntity.class,
36-
EmbeddableTests.MyBigDecimalConverter.class
36+
CollectionEmbeddableElementConversionTest.ProductEntity.class,
37+
CollectionEmbeddableElementConversionTest.MyBigDecimalConverter.class
3738
})
3839
@SessionFactory
3940
@TestForIssue(jiraKey = "HHH-15211")
40-
class EmbeddableTests {
41+
class CollectionEmbeddableElementConversionTest {
4142

42-
@Test
43-
void testNoClassCastExceptionThrown(SessionFactoryScope scope) {
44-
final ProductEntity entity = new ProductEntity();
43+
@BeforeEach
44+
void setUp(SessionFactoryScope scope) {
45+
final ProductEntity entity = new ProductEntity( 1 );
4546
entity.prices = Collections.singletonList( new ProductPrice( new MyBigDecimal( 100.0 ) ) );
46-
final Integer productId = scope.fromTransaction( session -> {
47+
scope.fromTransaction( session -> {
4748
session.persist( entity );
4849
return entity.productId;
4950
} );
51+
}
5052

51-
// without fixing, the following statement would thrown "ClassCastException"
53+
@Test
54+
void testNoClassCastExceptionThrown(SessionFactoryScope scope) {
5255
scope.inTransaction( session -> session.get(
5356
ProductEntity.class,
54-
productId
57+
1
5558
) );
5659
}
5760

61+
@AfterEach
62+
void tearDown(SessionFactoryScope scope) {
63+
scope.inTransaction(
64+
(session) -> session.createQuery( "delete ProductEntity" ).executeUpdate()
65+
);
66+
}
67+
5868
@Entity(name = "ProductEntity")
5969
static class ProductEntity {
6070
@Id
61-
@GeneratedValue
6271
Integer productId;
6372

73+
ProductEntity() {
74+
}
75+
76+
ProductEntity(Integer productId) {
77+
this.productId = productId;
78+
}
79+
6480
@ElementCollection(fetch = FetchType.EAGER)
6581
List<ProductPrice> prices = new ArrayList<>();
6682
}
@@ -69,7 +85,9 @@ static class ProductEntity {
6985
static class ProductPrice {
7086
MyBigDecimal price;
7187

72-
ProductPrice() {}
88+
ProductPrice() {
89+
}
90+
7391
ProductPrice(MyBigDecimal price) {
7492
this.price = price;
7593
}
@@ -95,9 +113,6 @@ public MyBigDecimal convertToEntityAttribute(BigDecimal dbData) {
95113
return new MyBigDecimal( dbData.doubleValue() );
96114
}
97115

98-
public MyBigDecimalConverter() {
99-
System.out.println( "Registered MyBigDecimalConverter" );
100-
}
101116
}
102117

103118
}

0 commit comments

Comments
 (0)