HHH-14881 Test attribute converters provided through CDI and configured through orm.xml
This commit is contained in:
parent
7cc0c8370b
commit
97f75f2aeb
|
@ -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 http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.test.cdi.converters;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import org.hibernate.annotations.Immutable;
|
||||
|
||||
@Immutable
|
||||
public class MyData {
|
||||
public final String value;
|
||||
|
||||
public MyData(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
MyData myData = (MyData) o;
|
||||
return value.equals( myData.value );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( value );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* 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.cdi.converters;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
|
||||
public class OrmXmlConverterBean implements AttributeConverter<MyData,String> {
|
||||
private final MonitorBean monitor;
|
||||
|
||||
@javax.inject.Inject
|
||||
public OrmXmlConverterBean(MonitorBean monitor) {
|
||||
this.monitor = monitor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertToDatabaseColumn(MyData attribute) {
|
||||
monitor.toDbCalled();
|
||||
if ( attribute == null ) {
|
||||
return null;
|
||||
}
|
||||
return attribute.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyData convertToEntityAttribute(String dbData) {
|
||||
monitor.fromDbCalled();
|
||||
if ( dbData == null ) {
|
||||
return null;
|
||||
}
|
||||
return new MyData( dbData );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* 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.cdi.converters;
|
||||
|
||||
// This entity is mapped using an orm.xml file
|
||||
public class TheOrmXmlEntity {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private MyData data;
|
||||
|
||||
public TheOrmXmlEntity() {
|
||||
}
|
||||
|
||||
public TheOrmXmlEntity(Integer id, String name, MyData data) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
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 MyData getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(MyData data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
|
@ -18,10 +18,14 @@ import org.hibernate.cfg.AvailableSettings;
|
|||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.tool.schema.Action;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
||||
import org.hibernate.test.cdi.converters.ConverterBean;
|
||||
import org.hibernate.test.cdi.converters.MonitorBean;
|
||||
import org.hibernate.test.cdi.converters.MyData;
|
||||
import org.hibernate.test.cdi.converters.OrmXmlConverterBean;
|
||||
import org.hibernate.test.cdi.converters.TheEntity;
|
||||
import org.hibernate.test.cdi.converters.TheOrmXmlEntity;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil2.inTransaction;
|
||||
|
@ -34,7 +38,7 @@ import static org.junit.Assert.assertTrue;
|
|||
*/
|
||||
public class CdiHostedConverterTest extends BaseUnitTestCase {
|
||||
@Test
|
||||
public void testIt() {
|
||||
public void testAnnotations() {
|
||||
MonitorBean.reset();
|
||||
|
||||
final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance()
|
||||
|
@ -99,4 +103,72 @@ public class CdiHostedConverterTest extends BaseUnitTestCase {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-14881\n")
|
||||
public void testOrmXml() {
|
||||
MonitorBean.reset();
|
||||
|
||||
final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance()
|
||||
.disableDiscovery()
|
||||
.addBeanClasses( MonitorBean.class, OrmXmlConverterBean.class );
|
||||
try ( final SeContainer cdiContainer = cdiInitializer.initialize() ) {
|
||||
BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
|
||||
|
||||
final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder( bsr )
|
||||
.applySetting( AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP )
|
||||
.applySetting( AvailableSettings.CDI_BEAN_MANAGER, cdiContainer.getBeanManager() )
|
||||
.build();
|
||||
|
||||
final SessionFactoryImplementor sessionFactory;
|
||||
|
||||
try {
|
||||
sessionFactory = (SessionFactoryImplementor) new MetadataSources( ssr )
|
||||
.addResource( "org/hibernate/test/cdi/converters/orm.xml" )
|
||||
.buildMetadata()
|
||||
.getSessionFactoryBuilder()
|
||||
.build();
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
StandardServiceRegistryBuilder.destroy( ssr );
|
||||
throw e;
|
||||
}
|
||||
|
||||
// The CDI bean should have been built immediately...
|
||||
assertTrue( MonitorBean.wasInstantiated() );
|
||||
assertEquals( 0, MonitorBean.currentFromDbCount() );
|
||||
assertEquals( 0, MonitorBean.currentToDbCount() );
|
||||
|
||||
try {
|
||||
inTransaction(
|
||||
sessionFactory,
|
||||
session -> session.persist( new TheOrmXmlEntity( 1, "me", new MyData( "foo" ) ) )
|
||||
);
|
||||
|
||||
assertEquals( 0, MonitorBean.currentFromDbCount() );
|
||||
assertEquals( 1, MonitorBean.currentToDbCount() );
|
||||
|
||||
inTransaction(
|
||||
sessionFactory,
|
||||
session -> {
|
||||
TheOrmXmlEntity it = session.find( TheOrmXmlEntity.class, 1 );
|
||||
assertNotNull( it );
|
||||
}
|
||||
);
|
||||
|
||||
assertEquals( 1, MonitorBean.currentFromDbCount() );
|
||||
assertEquals( 1, MonitorBean.currentToDbCount() );
|
||||
}
|
||||
finally {
|
||||
inTransaction(
|
||||
sessionFactory,
|
||||
session -> {
|
||||
session.createQuery( "delete TheOrmXmlEntity" ).executeUpdate();
|
||||
}
|
||||
);
|
||||
|
||||
sessionFactory.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!--
|
||||
~ 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>.
|
||||
-->
|
||||
<entity-mappings xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
|
||||
version="2.1">
|
||||
<entity class="org.hibernate.test.cdi.converters.TheOrmXmlEntity">
|
||||
<attributes>
|
||||
<id name="id" />
|
||||
<basic name="name" />
|
||||
<basic name="myData" />
|
||||
</attributes>
|
||||
</entity>
|
||||
<converter class="org.hibernate.test.cdi.converters.OrmXmlConverterBean" auto-apply="true"/>
|
||||
</entity-mappings>
|
Loading…
Reference in New Issue