From 7f001e50b29d04e3897e546d31ac39d5dd2dae5f Mon Sep 17 00:00:00 2001 From: John Lin Date: Mon, 12 Nov 2018 17:11:08 +0800 Subject: [PATCH] HHH-13073 - AnyMetaDef.metaType should default to "string" --- .../org/hibernate/annotations/AnyMetaDef.java | 2 +- .../org/hibernate/hibernate-mapping-3.0.dtd | 2 +- .../xsd/mapping/legacy-mapping-4.0.xsd | 2 +- .../any/AnyDefaultMetaTypeHbmTest.java | 72 +++++++++++++++++++ .../test/annotations/any/EmbeddedAnyTest.java | 2 +- .../test/annotations/any/HbmPropertySet.java | 45 ++++++++++++ .../test/annotations/any/PropertyMap.java | 2 +- .../test/annotations/any/PropertySet.java | 4 +- .../test/annotations/any/package-info.java | 2 +- .../any/AnyDefaultMetaTypeHbmTest.hbm.xml | 27 +++++++ 10 files changed, 152 insertions(+), 8 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/any/AnyDefaultMetaTypeHbmTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/any/HbmPropertySet.java create mode 100644 hibernate-core/src/test/resources/org/hibernate/test/annotations/any/AnyDefaultMetaTypeHbmTest.hbm.xml diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/AnyMetaDef.java b/hibernate-core/src/main/java/org/hibernate/annotations/AnyMetaDef.java index 30b6a21e60..f23f16108a 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/AnyMetaDef.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/AnyMetaDef.java @@ -37,7 +37,7 @@ public @interface AnyMetaDef { * Names the discriminator Hibernate Type for this Any/ManyToAny mapping. The default is to use * {@link org.hibernate.type.StringType} */ - String metaType(); + String metaType() default "string"; /** * Names the identifier Hibernate Type for the entity associated through this Any/ManyToAny mapping. diff --git a/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-3.0.dtd b/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-3.0.dtd index 9de0ef239a..517dfcf7a3 100644 --- a/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-3.0.dtd +++ b/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-3.0.dtd @@ -459,7 +459,7 @@ holding the name of the class (for that row). --> - + diff --git a/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd b/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd index fea059f7f0..fa4f9bc15e 100644 --- a/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd +++ b/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd @@ -1110,7 +1110,7 @@ - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/AnyDefaultMetaTypeHbmTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/AnyDefaultMetaTypeHbmTest.java new file mode 100644 index 0000000000..6f1d43f7b1 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/AnyDefaultMetaTypeHbmTest.java @@ -0,0 +1,72 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.annotations.any; + +import javax.persistence.TypedQuery; + +import org.hibernate.query.Query; + +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class AnyDefaultMetaTypeHbmTest extends BaseCoreFunctionalTestCase { + @Test + public void testDefaultAnyAssociation() { + doInHibernate( this::sessionFactory, s -> { + HbmPropertySet set1 = new HbmPropertySet( "string" ); + Property property = new StringProperty( "name", "Alex" ); + set1.setSomeProperty( property ); + s.save( set1 ); + + HbmPropertySet set2 = new HbmPropertySet( "integer" ); + property = new IntegerProperty( "age", 33 ); + set2.setSomeProperty( property ); + s.save( set2 ); + } ); + + doInHibernate( this::sessionFactory, s -> { + TypedQuery q = s + .createQuery( "select s from HbmPropertySet s where name = :name", HbmPropertySet.class ); + q.setParameter( "name", "string" ); + HbmPropertySet result = q.getSingleResult(); + + assertNotNull( result ); + assertNotNull( result.getSomeProperty() ); + assertTrue( result.getSomeProperty() instanceof StringProperty ); + assertEquals( "Alex", result.getSomeProperty().asString() ); + + q.setParameter( "name", "integer" ); + result = q.getSingleResult(); + + assertNotNull( result ); + assertNotNull( result.getSomeProperty() ); + assertTrue( result.getSomeProperty() instanceof IntegerProperty ); + assertEquals( "33", result.getSomeProperty().asString() ); + } ); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + StringProperty.class, + IntegerProperty.class, + LongProperty.class, + }; + } + + @Override + public String[] getMappings() { + return new String[] { + "annotations/any/AnyDefaultMetaTypeHbmTest.hbm.xml" + }; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/EmbeddedAnyTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/EmbeddedAnyTest.java index b64902b872..289261a9fa 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/EmbeddedAnyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/EmbeddedAnyTest.java @@ -113,7 +113,7 @@ public class EmbeddedAnyTest extends BaseEntityManagerFunctionalTestCase { @Embeddable public static class FooEmbeddable { - @AnyMetaDef(idType = "integer", metaType = "string", metaValues = { + @AnyMetaDef(idType = "integer", metaValues = { @MetaValue(value = "1", targetEntity = Bar1.class), @MetaValue(value = "2", targetEntity = Bar2.class) }) diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/HbmPropertySet.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/HbmPropertySet.java new file mode 100644 index 0000000000..9db91247c3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/HbmPropertySet.java @@ -0,0 +1,45 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.annotations.any; + +public class HbmPropertySet { + private Integer id; + private String name; + private Property someProperty; + + public HbmPropertySet() { + super(); + } + + public HbmPropertySet(String name) { + this.name = name; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Property getSomeProperty() { + return someProperty; + } + + public void setSomeProperty(Property someProperty) { + this.someProperty = someProperty; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertyMap.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertyMap.java index 812040403e..c33fec17a5 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertyMap.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertyMap.java @@ -57,7 +57,7 @@ public class PropertyMap { @ManyToAny( metaColumn = @Column( name = "property_type" ) ) @AnyMetaDef( - idType = "integer", metaType = "string", + idType = "integer", metaValues = { @MetaValue( value = "S", targetEntity = StringProperty.class ), @MetaValue( value = "I", targetEntity = IntegerProperty.class ) } ) diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertySet.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertySet.java index 0d7f19bf6b..442b7f73d3 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertySet.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/PropertySet.java @@ -41,7 +41,7 @@ public class PropertySet { @ManyToAny( metaColumn = @Column( name = "property_type" ) ) - @AnyMetaDef( idType = "integer", metaType = "string", + @AnyMetaDef( idType = "integer", metaValues = { @MetaValue( value = "S", targetEntity = StringProperty.class ), @MetaValue( value = "I", targetEntity = IntegerProperty.class ) } ) @@ -76,7 +76,7 @@ public class PropertySet { @Any( metaColumn = @Column( name = "property_type" ) ) @Cascade( value = { CascadeType.ALL } ) - @AnyMetaDef( idType = "integer", metaType = "string", metaValues = { + @AnyMetaDef( idType = "integer", metaValues = { @MetaValue( value = "S", targetEntity = StringProperty.class ), @MetaValue( value = "I", targetEntity = IntegerProperty.class ) } ) diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/package-info.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/package-info.java index 411994573a..e74c9bf56d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/any/package-info.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/any/package-info.java @@ -6,7 +6,7 @@ */ // $Id$ -@AnyMetaDef( name= "Property", metaType = "string", idType = "integer", +@AnyMetaDef( name= "Property", idType = "integer", metaValues = { @MetaValue(value = "C", targetEntity = CharProperty.class), @MetaValue(value = "I", targetEntity = IntegerProperty.class), diff --git a/hibernate-core/src/test/resources/org/hibernate/test/annotations/any/AnyDefaultMetaTypeHbmTest.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/test/annotations/any/AnyDefaultMetaTypeHbmTest.hbm.xml new file mode 100644 index 0000000000..7fe62345fe --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/test/annotations/any/AnyDefaultMetaTypeHbmTest.hbm.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file