HHH-13073 - AnyMetaDef.metaType should default to "string"

This commit is contained in:
John Lin 2018-11-12 17:11:08 +08:00 committed by Vlad Mihalcea
parent 29e135c015
commit 7f001e50b2
10 changed files with 152 additions and 8 deletions

View File

@ -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.

View File

@ -459,7 +459,7 @@ holding the name of the class (for that row). -->
<!ELEMENT any (meta*,meta-value*,column,column+)>
<!ATTLIST any id-type CDATA #REQUIRED>
<!ATTLIST any meta-type CDATA #IMPLIED> <!--- default: Hibernate.STRING -->
<!ATTLIST any meta-type CDATA "string"> <!--- default: Hibernate.STRING -->
<!ATTLIST any name CDATA #REQUIRED>
<!ATTLIST any access CDATA #IMPLIED>
<!ATTLIST any insert (true|false) "true">

View File

@ -1110,7 +1110,7 @@
<!-- include the columns spanned by this association in an index -->
<xs:attribute name="insert" default="true" type="xs:boolean"/>
<xs:attribute name="lazy" default="false" type="xs:boolean"/>
<xs:attribute name="meta-type" type="xs:string"/>
<xs:attribute name="meta-type" default="string" type="xs:string"/>
<!--- default: Hibernate.STRING -->
<xs:attribute name="name" use="required" type="xs:string"/>
<xs:attribute name="node" type="xs:string"/>

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<HbmPropertySet> 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"
};
}
}

View File

@ -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)
})

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}

View File

@ -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 ) } )

View File

@ -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 )
} )

View File

@ -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),

View File

@ -0,0 +1,27 @@
<?xml version="1.0"?>
<!--
~ 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.annotations.any">
<class name="HbmPropertySet" table="lazy_property_set">
<id name="id" type="java.lang.Integer">
<generator class="increment"/>
</id>
<property name="name"/>
<any name="someProperty" id-type="integer" cascade="all" lazy="false">
<meta-value value="S" class="StringProperty"/>
<meta-value value="I" class="IntegerProperty"/>
<column name="PROP_TYPE"/>
<column name="property_id"/>
</any>
</class>
</hibernate-mapping>