AttributeOverride add more tests

This commit is contained in:
Andrea Boriero 2019-12-11 09:42:48 +00:00
parent 436060008b
commit 67f77efa91
4 changed files with 223 additions and 194 deletions

View File

@ -450,7 +450,9 @@ public abstract class SimpleValue implements KeyValue {
} }
public boolean isValid(Mapping mapping) throws MappingException { public boolean isValid(Mapping mapping) throws MappingException {
return getColumnSpan()==getType().getColumnSpan(mapping); int columnSpan = getColumnSpan();
int typeColumnSpan = getType().getColumnSpan( mapping );
return columnSpan == typeColumnSpan;
} }
// public Type getType() throws MappingException { // public Type getType() throws MappingException {

View File

@ -0,0 +1,194 @@
package org.hibernate.orm.test.annotations.embeddables.attributeOverrides;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.YearMonth;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.HibernateException;
import org.hibernate.annotations.Columns;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.IntegerType;
import org.hibernate.usertype.UserType;
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.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
/**
* @author András Eisenberger
*/
@DomainModel(
annotatedClasses = {
AttributeOverrideEnhancedUserTypeTest.TypeValue.class,
AttributeOverrideEnhancedUserTypeTest.AggregatedTypeValue.class
}
)
@SessionFactory
@ServiceRegistry
public class AttributeOverrideEnhancedUserTypeTest {
@Test
@TestForIssue(jiraKey = "HHH-11465")
public void testIt(SessionFactoryScope scope) {
AggregatedTypeValue e1 = new AggregatedTypeValue();
scope.inTransaction( session -> {
e1.id = 1L;
TypeValue t1 = new TypeValue();
t1.time = YearMonth.of( 2017, 5 );
e1.oneValue = t1;
TypeValue t2 = new TypeValue();
t2.time = YearMonth.of( 2016, 4 );
e1.otherValue = t2;
session.save( e1 );
} );
scope.inTransaction( session -> {
AggregatedTypeValue entity1 = session.get( AggregatedTypeValue.class, e1.id );
assertEquals( entity1.oneValue.time, YearMonth.of( 2017, 5 ) );
assertEquals( entity1.otherValue.time, YearMonth.of( 2016, 4 ) );
session.delete( entity1 );
} );
}
@Embeddable
public static class TypeValue {
@Type(type = "year_month")
@Columns(columns = {
@Column(name = "year", nullable = true),
@Column(name = "month", nullable = true)
})
YearMonth time;
}
@Entity
@Table(name = "AGG_TYPE")
@TypeDef(
name = "year_month",
typeClass = YearMonthUserType.class
)
public static class AggregatedTypeValue {
@Id
private Long id;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "time", column = @Column(name = "one_year")),
@AttributeOverride(name = "time", column = @Column(name = "one_month"))
})
private TypeValue oneValue;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "time", column = @Column(name = "other_year")),
@AttributeOverride(name = "time", column = @Column(name = "other_month"))
})
private TypeValue otherValue;
}
public static class YearMonthUserType implements UserType, Serializable {
@Override
public int[] sqlTypes() {
return new int[] {
IntegerType.INSTANCE.sqlType(),
IntegerType.INSTANCE.sqlType(),
};
}
@Override
public Class returnedClass() {
return YearMonth.class;
}
@Override
public boolean equals(final Object x, final Object y) throws HibernateException {
if ( x == y ) {
return true;
}
if ( x == null || y == null ) {
return false;
}
final YearMonth mtx = (YearMonth) x;
final YearMonth mty = (YearMonth) y;
return mtx.equals( mty );
}
@Override
public int hashCode(final Object x) throws HibernateException {
return x.hashCode();
}
@Override
public Object nullSafeGet(
final ResultSet rs,
final String[] names,
final SharedSessionContractImplementor session,
final Object owner) throws HibernateException, SQLException {
assert names.length == 2;
final Integer year = IntegerType.INSTANCE.nullSafeGet( rs, names[0], session );
final Integer month = IntegerType.INSTANCE.nullSafeGet( rs, names[1], session );
return year == null || month == null ? null : YearMonth.of( year, month );
}
@Override
public void nullSafeSet(
final PreparedStatement st,
final Object value,
final int index,
final SharedSessionContractImplementor session) throws HibernateException, SQLException {
if ( value == null ) {
IntegerType.INSTANCE.set( st, null, index, session );
IntegerType.INSTANCE.set( st, null, index + 1, session );
}
else {
final YearMonth YearMonth = (YearMonth) value;
IntegerType.INSTANCE.set( st, YearMonth.getYear(), index, session );
IntegerType.INSTANCE.set( st, YearMonth.getMonthValue(), index + 1, session );
}
}
@Override
public Object deepCopy(final Object value) throws HibernateException {
return value;
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Serializable disassemble(final Object value) throws HibernateException {
return (Serializable) value;
}
@Override
public Object assemble(final Serializable cached, final Object value) throws HibernateException {
return cached;
}
@Override
public Object replace(final Object original, final Object target, final Object owner)
throws HibernateException {
return original;
}
}
}

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.test.annotations.embeddables.attributeOverrides; package org.hibernate.orm.test.annotations.embeddables.attributeOverrides;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -21,16 +21,18 @@ import javax.persistence.Id;
import javax.persistence.MapKeyColumn; import javax.persistence.MapKeyColumn;
import javax.persistence.Table; import javax.persistence.Table;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property; import org.hibernate.mapping.Property;
import org.hibernate.mapping.SimpleValue; import org.hibernate.mapping.SimpleValue;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.type.BasicType; import org.hibernate.type.BasicType;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.hibernate.testing.orm.junit.DomainModel;
import org.junit.Test; import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
@ -41,16 +43,20 @@ import static org.junit.Assert.assertTrue;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class BasicAttributeOverrideTest extends BaseNonConfigCoreFunctionalTestCase { @DomainModel(
@Override annotatedClasses = {
protected Class[] getAnnotatedClasses() { BasicAttributeOverrideTest.TypeValue.class,
return new Class[] { TypeValue.class, AggregatedTypeValue.class }; BasicAttributeOverrideTest.AggregatedTypeValue.class
} }
)
@ServiceRegistry
@SessionFactory
public class BasicAttributeOverrideTest {
@Test @Test
@TestForIssue( jiraKey = "HHH-8630" ) @TestForIssue(jiraKey = "HHH-8630")
public void testIt() { public void testIt(SessionFactoryScope scope) {
final PersistentClass entityBinding = metadata().getEntityBinding( AggregatedTypeValue.class.getName() ); final PersistentClass entityBinding = scope.getMetadataImplementor().getEntityBinding( AggregatedTypeValue.class.getName() );
final Property attributesBinding = entityBinding.getProperty( "attributes" ); final Property attributesBinding = entityBinding.getProperty( "attributes" );
final org.hibernate.mapping.Map attributesMap = (org.hibernate.mapping.Map) attributesBinding.getValue(); final org.hibernate.mapping.Map attributesMap = (org.hibernate.mapping.Map) attributesBinding.getValue();
@ -59,8 +65,11 @@ public class BasicAttributeOverrideTest extends BaseNonConfigCoreFunctionalTestC
assertTrue( String.class.equals( mapKeyType.getReturnedClass() ) ); assertTrue( String.class.equals( mapKeyType.getReturnedClass() ) );
// let's also make sure the @MapKeyColumn got applied // let's also make sure the @MapKeyColumn got applied
assertThat( mapKey.getColumnSpan(), is(1) ); assertThat( mapKey.getColumnSpan(), is( 1 ) );
final org.hibernate.mapping.Column mapKeyColumn = assertTyping( org.hibernate.mapping.Column .class, mapKey.getColumnIterator().next() ); final org.hibernate.mapping.Column mapKeyColumn = assertTyping(
org.hibernate.mapping.Column.class,
mapKey.getColumnIterator().next()
);
assertThat( mapKeyColumn.getName(), equalTo( "attribute_name" ) ); assertThat( mapKeyColumn.getName(), equalTo( "attribute_name" ) );
} }
@ -73,7 +82,7 @@ public class BasicAttributeOverrideTest extends BaseNonConfigCoreFunctionalTestC
} }
@Entity @Entity
@Table( name = "AGG_TYPE" ) @Table(name = "AGG_TYPE")
public static class AggregatedTypeValue { public static class AggregatedTypeValue {
@Id @Id
UUID id; UUID id;
@ -85,7 +94,7 @@ public class BasicAttributeOverrideTest extends BaseNonConfigCoreFunctionalTestC
}) })
TypeValue content; TypeValue content;
@CollectionTable( name = "ATTRIBUTES" ) @CollectionTable(name = "ATTRIBUTES")
@ElementCollection(fetch = FetchType.EAGER) @ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "attribute_name") @MapKeyColumn(name = "attribute_name")
@AttributeOverrides({ @AttributeOverrides({

View File

@ -1,176 +0,0 @@
package org.hibernate.test.annotations.embeddables.attributeOverrides;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.annotations.Columns;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.IntegerType;
import org.hibernate.usertype.UserType;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import javax.persistence.*;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.YearMonth;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
/**
* @author András Eisenberger
*/
public class AttributeOverrideEnhancedUserTypeTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
TypeValue.class,
AggregatedTypeValue.class
};
}
@Test
@TestForIssue( jiraKey = "HHH-11465" )
public void testIt() {
AggregatedTypeValue _e1 = doInHibernate( this::sessionFactory, session -> {
AggregatedTypeValue e1 = new AggregatedTypeValue();
e1.id = 1L;
TypeValue t1 = new TypeValue();
t1.time = YearMonth.of(2017, 5);
e1.oneValue = t1;
TypeValue t2 = new TypeValue();
t2.time = YearMonth.of(2016, 4);
e1.otherValue = t2;
session.save( e1 );
return e1;
} );
doInHibernate( this::sessionFactory, session -> {
AggregatedTypeValue e1 = session.get( AggregatedTypeValue.class, _e1.id );
assertEquals(e1.oneValue.time, YearMonth.of(2017, 5));
assertEquals(e1.otherValue.time, YearMonth.of(2016, 4));
session.delete( e1 );
} );
}
@Embeddable
public static class TypeValue {
@Type(type = "year_month")
@Columns(columns = {
@Column(name = "year", nullable = true),
@Column(name = "month", nullable = true)
})
YearMonth time;
}
@Entity
@Table( name = "AGG_TYPE" )
@TypeDef(
name = "year_month",
typeClass = YearMonthUserType.class
)
public static class AggregatedTypeValue {
@Id
private Long id;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "time", column = @Column(name = "one_year")),
@AttributeOverride(name = "time", column = @Column(name = "one_month"))
})
private TypeValue oneValue;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "time", column = @Column(name = "other_year")),
@AttributeOverride(name = "time", column = @Column(name = "other_month"))
})
private TypeValue otherValue;
}
public static class YearMonthUserType implements UserType, Serializable {
@Override
public int[] sqlTypes() {
return new int[]{
IntegerType.INSTANCE.sqlType(),
IntegerType.INSTANCE.sqlType(),
};
}
@Override
public Class returnedClass() {
return YearMonth.class;
}
@Override
public boolean equals(final Object x, final Object y) throws HibernateException {
if (x == y) {
return true;
}
if (x == null || y == null) {
return false;
}
final YearMonth mtx = (YearMonth) x;
final YearMonth mty = (YearMonth) y;
return mtx.equals(mty);
}
@Override
public int hashCode(final Object x) throws HibernateException {
return x.hashCode();
}
@Override
public Object nullSafeGet(final ResultSet rs, final String[] names, final SharedSessionContractImplementor session, final Object owner) throws HibernateException, SQLException {
assert names.length == 2;
final Integer year = IntegerType.INSTANCE.nullSafeGet(rs, names[0], session);
final Integer month = IntegerType.INSTANCE.nullSafeGet(rs, names[1], session);
return year == null || month == null ? null : YearMonth.of(year, month);
}
@Override
public void nullSafeSet(final PreparedStatement st, final Object value, final int index, final SharedSessionContractImplementor session) throws HibernateException, SQLException {
if (value == null) {
IntegerType.INSTANCE.set(st, null, index, session);
IntegerType.INSTANCE.set(st, null, index + 1, session);
} else {
final YearMonth YearMonth = (YearMonth) value;
IntegerType.INSTANCE.set(st, YearMonth.getYear(), index, session);
IntegerType.INSTANCE.set(st, YearMonth.getMonthValue(), index + 1, session);
}
}
@Override
public Object deepCopy(final Object value) throws HibernateException {
return value;
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Serializable disassemble(final Object value) throws HibernateException {
return (Serializable) value;
}
@Override
public Object assemble(final Serializable cached, final Object value) throws HibernateException {
return cached;
}
@Override
public Object replace(final Object original, final Object target, final Object owner) throws HibernateException {
return original;
}
}
}