Skip to content

Commit 29346e6

Browse files
jrenaatmbellade
authored andcommitted
HHH-17635 - Add test for issue
Signed-off-by: Jan Schatteman <[email protected]>
1 parent d6f09d6 commit 29346e6

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/mapping/basic/MonetaryAmountUserType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
public class MonetaryAmountUserType implements CompositeUserType<MonetaryAmount> {
2222

23+
public static final MonetaryAmountUserType INSTANCE = new MonetaryAmountUserType();
24+
2325
@Override
2426
public Object getPropertyValue(MonetaryAmount component, int property) throws HibernateException {
2527
switch ( property ) {

hibernate-core/src/test/java/org/hibernate/orm/test/type/contributor/usertype/ContributedUserTypeTest.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77
package org.hibernate.orm.test.type.contributor.usertype;
88

99
import java.io.Serializable;
10+
import java.math.BigDecimal;
11+
import java.util.Currency;
1012

13+
import org.hibernate.boot.model.TypeContributor;
14+
import org.hibernate.orm.test.mapping.basic.MonetaryAmount;
1115
import org.hibernate.type.CustomType;
1216
import org.hibernate.type.Type;
1317
import org.hibernate.type.UserComponentType;
1418

19+
import org.hibernate.testing.orm.junit.BootstrapServiceRegistry;
1520
import org.hibernate.testing.orm.junit.DomainModel;
21+
import org.hibernate.testing.orm.junit.Jira;
1622
import org.hibernate.testing.orm.junit.JiraKey;
1723
import org.hibernate.testing.orm.junit.SessionFactory;
1824
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@@ -29,11 +35,26 @@
2935
annotatedClasses = {
3036
ContributedUserTypeTest.StringWrapperTestEntity.class,
3137
ContributedUserTypeTest.MyCompositeValueTestEntity.class,
38+
ContributedUserTypeTest.Wallet.class
3239
},
3340
typeContributors = { StringWrapperTypeContributor.class, MyCompositeValueTypeContributor.class }
3441
)
3542
@SessionFactory
43+
@BootstrapServiceRegistry(
44+
javaServices = {
45+
@BootstrapServiceRegistry.JavaService( role = TypeContributor.class, impl = ServiceLoadedCustomUserTypeTypeContributor.class)
46+
}
47+
)
3648
public class ContributedUserTypeTest {
49+
50+
public void tearDown(SessionFactoryScope scope) {
51+
scope.inTransaction(
52+
session -> {
53+
session.createMutationQuery( "delete from Wallet" ).executeUpdate();
54+
}
55+
);
56+
}
57+
3758
@Test
3859
@JiraKey( "HHH-14408" )
3960
public void test(SessionFactoryScope scope) {
@@ -81,6 +102,28 @@ public void testCompositeParameter(SessionFactoryScope scope) {
81102
);
82103
}
83104

105+
@Test
106+
@Jira( value = "https://hibernate.atlassian.net/browse/HHH-17635" )
107+
public void testServiceLoadedCustomUserType(SessionFactoryScope scope) {
108+
scope.inTransaction(
109+
session -> {
110+
Wallet wallet = new Wallet();
111+
wallet.setId( 1L );
112+
wallet.setMoney( new MonetaryAmount( new BigDecimal( 1000 ), Currency.getInstance("EUR")) );
113+
session.persist( wallet );
114+
}
115+
);
116+
scope.inTransaction(
117+
session -> {
118+
Wallet w = session.createSelectionQuery( "from Wallet", Wallet.class ).getSingleResult();
119+
MonetaryAmount amount = w.getMoney();
120+
Assertions.assertNotNull( amount );
121+
Assertions.assertEquals( 1000, amount.getAmount().intValue() );
122+
Assertions.assertEquals( "EUR", amount.getCurrency().getCurrencyCode() );
123+
}
124+
);
125+
}
126+
84127
@Entity( name = "StringWrapperTestEntity" )
85128
public static class StringWrapperTestEntity implements Serializable {
86129
@Id
@@ -94,4 +137,30 @@ public static class MyCompositeValueTestEntity implements Serializable {
94137
private Integer id;
95138
private MyCompositeValue compositeValue;
96139
}
140+
141+
@Entity(name = "Wallet")
142+
public static class Wallet {
143+
144+
@Id
145+
private Long id;
146+
147+
private MonetaryAmount money;
148+
149+
public Long getId() {
150+
return id;
151+
}
152+
153+
public void setId(Long id) {
154+
this.id = id;
155+
}
156+
157+
public MonetaryAmount getMoney() {
158+
return money;
159+
}
160+
161+
public void setMoney(MonetaryAmount money) {
162+
this.money = money;
163+
}
164+
}
165+
97166
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.orm.test.type.contributor.usertype;
8+
9+
import org.hibernate.boot.model.TypeContributions;
10+
import org.hibernate.boot.model.TypeContributor;
11+
import org.hibernate.orm.test.mapping.basic.MonetaryAmountUserType;
12+
import org.hibernate.service.ServiceRegistry;
13+
14+
/**
15+
* @author Jan Schatteman
16+
*/
17+
public class ServiceLoadedCustomUserTypeTypeContributor implements TypeContributor {
18+
19+
@Override
20+
public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
21+
typeContributions.contributeType( MonetaryAmountUserType.INSTANCE );
22+
}
23+
}

0 commit comments

Comments
 (0)