diff --git a/hibernate-core/src/main/java/org/hibernate/boot/jaxb/hbm/internal/EntityModeConverter.java b/hibernate-core/src/main/java/org/hibernate/boot/jaxb/hbm/internal/EntityModeConverter.java index eeee468ba2..d9548064fa 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/jaxb/hbm/internal/EntityModeConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/jaxb/hbm/internal/EntityModeConverter.java @@ -17,6 +17,6 @@ public class EntityModeConverter { } public static String toXml(EntityMode entityMode) { - return entityMode.name(); + return ( null == entityMode ) ? null : entityMode.getExternalName(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/jaxb/hbm/internal/GenerationTimingConverter.java b/hibernate-core/src/main/java/org/hibernate/boot/jaxb/hbm/internal/GenerationTimingConverter.java index 55b7d93053..546bc0801c 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/jaxb/hbm/internal/GenerationTimingConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/jaxb/hbm/internal/GenerationTimingConverter.java @@ -19,6 +19,8 @@ public class GenerationTimingConverter { } public static String toXml(GenerationTiming generationTiming) { - return generationTiming.name().toLowerCase( Locale.ENGLISH ); + return ( null == generationTiming ) ? + null : + generationTiming.name().toLowerCase( Locale.ENGLISH ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/boot/jaxb/hbm/internal/EntityModeConverterTest.java b/hibernate-core/src/test/java/org/hibernate/test/boot/jaxb/hbm/internal/EntityModeConverterTest.java new file mode 100644 index 0000000000..7bf232d7c5 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/boot/jaxb/hbm/internal/EntityModeConverterTest.java @@ -0,0 +1,49 @@ +/* + * 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.boot.jaxb.hbm.internal; + +import org.hibernate.EntityMode; +import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping; +import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmRootEntityType; +import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmSimpleIdType; +import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmTuplizerType; +import org.hibernate.tuple.entity.DynamicMapEntityTuplizer; + +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.Test; + +/** + * @author Jean-François Boeuf + */ +public class EntityModeConverterTest extends BaseUnitTestCase { + + @Test + public void testMashallNullEntityMode() throws Exception { + XmlBindingChecker.checkValidGeneration( generateXml( false ) ); + } + + @Test + public void testMashallNotNullEntityMode() throws Exception { + XmlBindingChecker.checkValidGeneration( generateXml( true ) ); + } + + private JaxbHbmHibernateMapping generateXml(boolean includeEntityMode) + throws Exception { + JaxbHbmHibernateMapping hm = new JaxbHbmHibernateMapping(); + JaxbHbmRootEntityType clazz = new JaxbHbmRootEntityType(); + JaxbHbmTuplizerType tuplizer = new JaxbHbmTuplizerType(); + tuplizer.setClazz( DynamicMapEntityTuplizer.class.getCanonicalName() ); + if ( includeEntityMode ) { + tuplizer.setEntityMode( EntityMode.MAP ); + } + clazz.getTuplizer().add( tuplizer ); + JaxbHbmSimpleIdType id = new JaxbHbmSimpleIdType(); + clazz.setId( id ); + hm.getClazz().add( clazz ); + return hm; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/boot/jaxb/hbm/internal/GenerationTimingConverterTest.java b/hibernate-core/src/test/java/org/hibernate/test/boot/jaxb/hbm/internal/GenerationTimingConverterTest.java new file mode 100644 index 0000000000..98d29cbc30 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/boot/jaxb/hbm/internal/GenerationTimingConverterTest.java @@ -0,0 +1,37 @@ +/* + * 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.boot.jaxb.hbm.internal; + +import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmBasicAttributeType; +import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping; +import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmRootEntityType; +import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmSimpleIdType; + +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.Test; + +/** + * @author Jean-François Boeuf + */ +public class GenerationTimingConverterTest extends BaseUnitTestCase { + + @Test + public void testMashallAttributeWithNullGenerationTiming() + throws Exception { + JaxbHbmHibernateMapping hm = new JaxbHbmHibernateMapping(); + JaxbHbmRootEntityType clazz = new JaxbHbmRootEntityType(); + JaxbHbmSimpleIdType id = new JaxbHbmSimpleIdType(); + JaxbHbmBasicAttributeType att = new JaxbHbmBasicAttributeType(); + att.setName( "attributeName" ); + clazz.getAttributes().add( att ); + clazz.setId( id ); + hm.getClazz().add( clazz ); + + XmlBindingChecker.checkValidGeneration( hm ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/boot/jaxb/hbm/internal/XmlBindingChecker.java b/hibernate-core/src/test/java/org/hibernate/test/boot/jaxb/hbm/internal/XmlBindingChecker.java new file mode 100644 index 0000000000..21d9c30aac --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/boot/jaxb/hbm/internal/XmlBindingChecker.java @@ -0,0 +1,38 @@ +/* + * 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.boot.jaxb.hbm.internal; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; + +import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.spi.XmlMappingBinderAccess; +import org.hibernate.service.ServiceRegistry; + +/** + * @author Jean-François Boeuf + */ +public class XmlBindingChecker { + + public static void checkValidGeneration(JaxbHbmHibernateMapping hbmMapping) + throws Exception { + JAXBContext jaxbContext = JAXBContext + .newInstance( JaxbHbmHibernateMapping.class ); + + Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); + jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true ); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + jaxbMarshaller.marshal( hbmMapping, bos ); + ByteArrayInputStream is = new ByteArrayInputStream( bos.toByteArray() ); + ServiceRegistry sr = new StandardServiceRegistryBuilder().build(); + new XmlMappingBinderAccess( sr ).bind( is ); + } +}