Skip to content

Commit 97f75f2

Browse files
committed
HHH-14881 Test attribute converters provided through CDI and configured through orm.xml
1 parent 7cc0c83 commit 97f75f2

File tree

5 files changed

+212
-1
lines changed

5 files changed

+212
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
6+
*/
7+
package org.hibernate.test.cdi.converters;
8+
9+
import java.util.Objects;
10+
11+
import org.hibernate.annotations.Immutable;
12+
13+
@Immutable
14+
public class MyData {
15+
public final String value;
16+
17+
public MyData(String value) {
18+
this.value = value;
19+
}
20+
21+
@Override
22+
public boolean equals(Object o) {
23+
if ( this == o ) {
24+
return true;
25+
}
26+
if ( o == null || getClass() != o.getClass() ) {
27+
return false;
28+
}
29+
MyData myData = (MyData) o;
30+
return value.equals( myData.value );
31+
}
32+
33+
@Override
34+
public int hashCode() {
35+
return Objects.hash( value );
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
6+
*/
7+
package org.hibernate.test.cdi.converters;
8+
9+
import javax.persistence.AttributeConverter;
10+
11+
public class OrmXmlConverterBean implements AttributeConverter<MyData,String> {
12+
private final MonitorBean monitor;
13+
14+
@javax.inject.Inject
15+
public OrmXmlConverterBean(MonitorBean monitor) {
16+
this.monitor = monitor;
17+
}
18+
19+
@Override
20+
public String convertToDatabaseColumn(MyData attribute) {
21+
monitor.toDbCalled();
22+
if ( attribute == null ) {
23+
return null;
24+
}
25+
return attribute.value;
26+
}
27+
28+
@Override
29+
public MyData convertToEntityAttribute(String dbData) {
30+
monitor.fromDbCalled();
31+
if ( dbData == null ) {
32+
return null;
33+
}
34+
return new MyData( dbData );
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
6+
*/
7+
package org.hibernate.test.cdi.converters;
8+
9+
// This entity is mapped using an orm.xml file
10+
public class TheOrmXmlEntity {
11+
private Integer id;
12+
private String name;
13+
private MyData data;
14+
15+
public TheOrmXmlEntity() {
16+
}
17+
18+
public TheOrmXmlEntity(Integer id, String name, MyData data) {
19+
this.id = id;
20+
this.name = name;
21+
this.data = data;
22+
}
23+
24+
public Integer getId() {
25+
return id;
26+
}
27+
28+
public void setId(Integer id) {
29+
this.id = id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public MyData getData() {
41+
return data;
42+
}
43+
44+
public void setData(MyData data) {
45+
this.data = data;
46+
}
47+
}

hibernate-core/src/test/java/org/hibernate/test/cdi/converters/standard/CdiHostedConverterTest.java

+73-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
import org.hibernate.engine.spi.SessionFactoryImplementor;
1919
import org.hibernate.tool.schema.Action;
2020

21+
import org.hibernate.testing.TestForIssue;
2122
import org.hibernate.testing.junit4.BaseUnitTestCase;
2223
import org.hibernate.test.cdi.converters.ConverterBean;
2324
import org.hibernate.test.cdi.converters.MonitorBean;
25+
import org.hibernate.test.cdi.converters.MyData;
26+
import org.hibernate.test.cdi.converters.OrmXmlConverterBean;
2427
import org.hibernate.test.cdi.converters.TheEntity;
28+
import org.hibernate.test.cdi.converters.TheOrmXmlEntity;
2529
import org.junit.Test;
2630

2731
import static org.hibernate.testing.transaction.TransactionUtil2.inTransaction;
@@ -34,7 +38,7 @@
3438
*/
3539
public class CdiHostedConverterTest extends BaseUnitTestCase {
3640
@Test
37-
public void testIt() {
41+
public void testAnnotations() {
3842
MonitorBean.reset();
3943

4044
final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance()
@@ -99,4 +103,72 @@ public void testIt() {
99103
}
100104
}
101105
}
106+
107+
@Test
108+
@TestForIssue(jiraKey = "HHH-14881\n")
109+
public void testOrmXml() {
110+
MonitorBean.reset();
111+
112+
final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance()
113+
.disableDiscovery()
114+
.addBeanClasses( MonitorBean.class, OrmXmlConverterBean.class );
115+
try ( final SeContainer cdiContainer = cdiInitializer.initialize() ) {
116+
BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
117+
118+
final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder( bsr )
119+
.applySetting( AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP )
120+
.applySetting( AvailableSettings.CDI_BEAN_MANAGER, cdiContainer.getBeanManager() )
121+
.build();
122+
123+
final SessionFactoryImplementor sessionFactory;
124+
125+
try {
126+
sessionFactory = (SessionFactoryImplementor) new MetadataSources( ssr )
127+
.addResource( "org/hibernate/test/cdi/converters/orm.xml" )
128+
.buildMetadata()
129+
.getSessionFactoryBuilder()
130+
.build();
131+
}
132+
catch ( Exception e ) {
133+
StandardServiceRegistryBuilder.destroy( ssr );
134+
throw e;
135+
}
136+
137+
// The CDI bean should have been built immediately...
138+
assertTrue( MonitorBean.wasInstantiated() );
139+
assertEquals( 0, MonitorBean.currentFromDbCount() );
140+
assertEquals( 0, MonitorBean.currentToDbCount() );
141+
142+
try {
143+
inTransaction(
144+
sessionFactory,
145+
session -> session.persist( new TheOrmXmlEntity( 1, "me", new MyData( "foo" ) ) )
146+
);
147+
148+
assertEquals( 0, MonitorBean.currentFromDbCount() );
149+
assertEquals( 1, MonitorBean.currentToDbCount() );
150+
151+
inTransaction(
152+
sessionFactory,
153+
session -> {
154+
TheOrmXmlEntity it = session.find( TheOrmXmlEntity.class, 1 );
155+
assertNotNull( it );
156+
}
157+
);
158+
159+
assertEquals( 1, MonitorBean.currentFromDbCount() );
160+
assertEquals( 1, MonitorBean.currentToDbCount() );
161+
}
162+
finally {
163+
inTransaction(
164+
sessionFactory,
165+
session -> {
166+
session.createQuery( "delete TheOrmXmlEntity" ).executeUpdate();
167+
}
168+
);
169+
170+
sessionFactory.close();
171+
}
172+
}
173+
}
102174
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
~ Hibernate, Relational Persistence for Idiomatic Java
5+
~
6+
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
7+
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
8+
-->
9+
<entity-mappings xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
10+
version="2.1">
11+
<entity class="org.hibernate.test.cdi.converters.TheOrmXmlEntity">
12+
<attributes>
13+
<id name="id" />
14+
<basic name="name" />
15+
<basic name="myData" />
16+
</attributes>
17+
</entity>
18+
<converter class="org.hibernate.test.cdi.converters.OrmXmlConverterBean" auto-apply="true"/>
19+
</entity-mappings>

0 commit comments

Comments
 (0)