HHH-15211 fix embeddable basic attribute converter hash code bug

This commit is contained in:
Nathan Xu 2022-04-15 11:12:57 -04:00 committed by Andrea Boriero
parent 1946aea068
commit 64c39691ff
3 changed files with 39 additions and 18 deletions

View File

@ -161,7 +161,7 @@ public abstract class AbstractStandardBasicType<T>
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final int getHashCode(Object x) { public int getHashCode(Object x) {
return javaType.extractHashCode( (T) x ); return javaType.extractHashCode( (T) x );
} }

View File

@ -121,6 +121,12 @@ public class AttributeConverterTypeAdapter<T> extends AbstractSingleColumnStanda
return ( (JavaType<Object>) getDomainJtd() ).areEqual( one, another ); return ( (JavaType<Object>) getDomainJtd() ).areEqual( one, another );
} }
@Override
public int getHashCode(Object x) {
//noinspection unchecked
return getDomainJtd().extractHashCode( (T) x );
}
@Override @Override
public String toString() { public String toString() {
return description; return description;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.orm.test.mapping.converted.converter.elementCollection.embeddable; package org.hibernate.orm.test.mapping.converted.converter.elementCollection;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
@ -15,6 +15,8 @@ import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import jakarta.persistence.AttributeConverter; import jakarta.persistence.AttributeConverter;
@ -23,7 +25,6 @@ import jakarta.persistence.ElementCollection;
import jakarta.persistence.Embeddable; import jakarta.persistence.Embeddable;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.FetchType; import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id; import jakarta.persistence.Id;
/** /**
@ -32,35 +33,50 @@ import jakarta.persistence.Id;
*/ */
@DomainModel(annotatedClasses = { @DomainModel(annotatedClasses = {
EmbeddableTests.ProductEntity.class, CollectionEmbeddableElementConversionTest.ProductEntity.class,
EmbeddableTests.MyBigDecimalConverter.class CollectionEmbeddableElementConversionTest.MyBigDecimalConverter.class
}) })
@SessionFactory @SessionFactory
@TestForIssue(jiraKey = "HHH-15211") @TestForIssue(jiraKey = "HHH-15211")
class EmbeddableTests { class CollectionEmbeddableElementConversionTest {
@Test @BeforeEach
void testNoClassCastExceptionThrown(SessionFactoryScope scope) { void setUp(SessionFactoryScope scope) {
final ProductEntity entity = new ProductEntity(); final ProductEntity entity = new ProductEntity( 1 );
entity.prices = Collections.singletonList( new ProductPrice( new MyBigDecimal( 100.0 ) ) ); entity.prices = Collections.singletonList( new ProductPrice( new MyBigDecimal( 100.0 ) ) );
final Integer productId = scope.fromTransaction( session -> { scope.fromTransaction( session -> {
session.persist( entity ); session.persist( entity );
return entity.productId; return entity.productId;
} ); } );
}
// without fixing, the following statement would thrown "ClassCastException" @Test
void testNoClassCastExceptionThrown(SessionFactoryScope scope) {
scope.inTransaction( session -> session.get( scope.inTransaction( session -> session.get(
ProductEntity.class, ProductEntity.class,
productId 1
) ); ) );
} }
@AfterEach
void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
(session) -> session.createQuery( "delete ProductEntity" ).executeUpdate()
);
}
@Entity(name = "ProductEntity") @Entity(name = "ProductEntity")
static class ProductEntity { static class ProductEntity {
@Id @Id
@GeneratedValue
Integer productId; Integer productId;
ProductEntity() {
}
ProductEntity(Integer productId) {
this.productId = productId;
}
@ElementCollection(fetch = FetchType.EAGER) @ElementCollection(fetch = FetchType.EAGER)
List<ProductPrice> prices = new ArrayList<>(); List<ProductPrice> prices = new ArrayList<>();
} }
@ -69,7 +85,9 @@ class EmbeddableTests {
static class ProductPrice { static class ProductPrice {
MyBigDecimal price; MyBigDecimal price;
ProductPrice() {} ProductPrice() {
}
ProductPrice(MyBigDecimal price) { ProductPrice(MyBigDecimal price) {
this.price = price; this.price = price;
} }
@ -95,9 +113,6 @@ class EmbeddableTests {
return new MyBigDecimal( dbData.doubleValue() ); return new MyBigDecimal( dbData.doubleValue() );
} }
public MyBigDecimalConverter() {
System.out.println( "Registered MyBigDecimalConverter" );
}
} }
} }