HHH-16368 UnsupportedOperationException: compare() not implemented for EntityType

This commit is contained in:
Andrea Boriero 2023-03-27 15:44:15 +02:00 committed by Steve Ebersole
parent e4e25bddfc
commit 867ce874e2
7 changed files with 259 additions and 4 deletions

View File

@ -11,6 +11,7 @@ import java.util.List;
import org.hibernate.Incubating;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.IndexedConsumer;
import org.hibernate.metamodel.mapping.BasicValuedMapping;
@ -169,4 +170,9 @@ public interface BasicType<T> extends Type, BasicDomainType<T>, MappingType, Bas
final BasicValueConverter<T, ?> valueConverter = getValueConverter();
return valueConverter == null ? null : valueConverter.getSpecializedTypeDeclaration( getJdbcType(), dialect );
}
@Override
default int compare(Object x, Object y, SessionFactoryImplementor sessionFactory) {
return compare( x, y );
}
}

View File

@ -125,6 +125,11 @@ public abstract class CollectionType extends AbstractType implements Association
return 0; // collections cannot be compared
}
@Override
public int compare(Object x, Object y, SessionFactoryImplementor sessionFactory) {
return compare( x, y );
}
@Override
public int getHashCode(Object x) {
throw new UnsupportedOperationException( "cannot doAfterTransactionCompletion lookups on collections" );

View File

@ -220,6 +220,20 @@ public class ComponentType extends AbstractType implements CompositeTypeImplemen
return 0;
}
@Override
public int compare(Object x, Object y, SessionFactoryImplementor sessionFactory) {
if ( x == y ) {
return 0;
}
for ( int i = 0; i < propertySpan; i++ ) {
int propertyCompare = propertyTypes[i].compare( getPropertyValue( x, i ), getPropertyValue( y, i ), sessionFactory );
if ( propertyCompare != 0 ) {
return propertyCompare;
}
}
return 0;
}
public boolean isMethodOf(Method method) {
return false;
}

View File

@ -12,7 +12,6 @@ import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;

View File

@ -75,6 +75,11 @@ public class MetaType extends AbstractType {
return String.class;
}
@Override
public int compare(Object x, Object y, SessionFactoryImplementor sessionFactory) {
return compare( x, y );
}
@Override
public void nullSafeSet(
PreparedStatement st,

View File

@ -218,9 +218,8 @@ public interface Type extends Serializable {
*/
int compare(Object x, Object y);
default int compare(Object x, Object y, SessionFactoryImplementor sessionFactory) {
return compare( x, y );
}
int compare(Object x, Object y, SessionFactoryImplementor sessionFactory);
/**
* Should the parent be considered dirty, given both the old and current value?
*

View File

@ -0,0 +1,227 @@
package org.hibernate.orm.test.orderupdates;
import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedList;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import static jakarta.persistence.CascadeType.REMOVE;
@DomainModel(
annotatedClasses = {
OrderUpdatesTest.Parent.class,
OrderUpdatesTest.Child.class
}
)
@SessionFactory
@ServiceRegistry(
settings = {
@Setting(name = AvailableSettings.ORDER_UPDATES, value = "true")
}
)
@TestForIssue(jiraKey = "HHH-16368")
public class OrderUpdatesTest {
@Test
public void testIt(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Parent parent1 = new Parent();
parent1 = session.merge( parent1 );
Parent parent2 = new Parent();
parent2 = session.merge( parent2 );
session.flush();
// create two children with the same name, so that they differ only in their parent
// otherwise the key doesn't trigger the exception
Child child1 = new Child();
child1.setName( "name1" );
child1.setValue( "value" );
parent1.addChild( child1 );
child1 = session.merge( child1 );
Child child2 = new Child();
child2.setName( "name1" );
child2.setValue( "value" );
parent2.addChild( child2 );
child2 = session.merge( child2 );
session.flush();
child1.setValue( "new-value" );
child2.setValue( "new-value" );
session.flush();
}
);
}
@Entity(name = "Parent")
public static class Parent {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy = "parent", cascade = { REMOVE }, fetch = FetchType.LAZY)
private Collection<Child> children = new LinkedList<>();
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public void addChild(Child child) {
children.add( child );
child.setParent( this );
}
public void removeChild(Child child) {
children.remove( child );
child.setParent( null );
}
}
@Entity(name = "Chil;d")
@IdClass(Key.class)
public static class Child {
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID")
private Parent parent;
@Id
protected String name;
@Column(name = "VALUE_COLUMN")
protected String value;
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( !( o instanceof Child ) ) {
return false;
}
return parent != null && parent.equals( ( (Child) o ).getParent() );
}
@Override
public int hashCode() {
return getClass().hashCode();
}
}
public static class Key implements Serializable, Comparable<Key> {
protected Parent parent;
protected String name;
public Key() {
}
public Key(Parent parent, String name) {
this.parent = parent;
this.name = name;
}
public Parent getParent() {
return parent;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
Key key = (Key) o;
if ( parent != null ?
!parent.getId().equals( key.parent != null ? key.parent.getId() : null ) :
key.parent != null ) {
return false;
}
if ( name != null ? !name.equals( key.name != null ? key.name : null ) : key.name != null ) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = parent != null ? parent.getId().hashCode() : 0;
result = 31 * result + ( name != null ? name.hashCode() : 0 );
return result;
}
private final static Comparator<Key> COMPARATOR = Comparator.comparing( Key::getName )
.thenComparing( c -> c.getParent() == null ? null : c.getParent().getId() );
@Override
public int compareTo(Key other) {
return COMPARATOR.compare( this, other );
}
}
}