Skip to content

Commit 5c36abf

Browse files
committed
HHH-9409 - non-auto-apply converter fails at element collection
(cherry picked from commit 39e3dbb)
1 parent d12ce67 commit 5c36abf

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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.jpa.test.convert;
8+
9+
import java.util.Arrays;
10+
import java.util.HashMap;
11+
import java.util.HashSet;
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.Set;
15+
import javax.persistence.AttributeConverter;
16+
import javax.persistence.CollectionTable;
17+
import javax.persistence.Column;
18+
import javax.persistence.Convert;
19+
import javax.persistence.Converter;
20+
import javax.persistence.ElementCollection;
21+
import javax.persistence.Entity;
22+
import javax.persistence.EntityManager;
23+
import javax.persistence.EntityManagerFactory;
24+
import javax.persistence.FetchType;
25+
import javax.persistence.Id;
26+
import javax.persistence.JoinColumn;
27+
import javax.persistence.Table;
28+
import javax.persistence.UniqueConstraint;
29+
30+
import org.hibernate.cfg.AvailableSettings;
31+
import org.hibernate.jpa.boot.spi.Bootstrap;
32+
import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter;
33+
34+
import org.hibernate.testing.TestForIssue;
35+
import org.hibernate.testing.junit4.BaseUnitTestCase;
36+
import org.junit.Test;
37+
38+
import static org.junit.Assert.assertEquals;
39+
40+
/**
41+
* @author Steve Ebersole
42+
*/
43+
@TestForIssue( jiraKey = "HHH-8529" )
44+
public class CollectionElementExplicitConversionTest extends BaseUnitTestCase {
45+
@Test
46+
public void testElementCollectionConversion() {
47+
final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() {
48+
@Override
49+
public List<String> getManagedClassNames() {
50+
return Arrays.asList( Customer.class.getName(), ColorTypeConverter.class.getName() );
51+
}
52+
};
53+
54+
final Map settings = new HashMap();
55+
settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
56+
57+
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build();
58+
try {
59+
EntityManager em = emf.createEntityManager();
60+
em.getTransaction().begin();
61+
Customer customer = new Customer( 1 );
62+
customer.colors.add( ColorType.BLUE );
63+
em.persist( customer );
64+
em.getTransaction().commit();
65+
em.close();
66+
67+
em = emf.createEntityManager();
68+
em.getTransaction().begin();
69+
assertEquals( 1, em.find( Customer.class, 1 ).colors.size() );
70+
em.getTransaction().commit();
71+
em.close();
72+
73+
em = emf.createEntityManager();
74+
em.getTransaction().begin();
75+
customer = em.find( Customer.class, 1 );
76+
em.remove( customer );
77+
em.getTransaction().commit();
78+
em.close();
79+
}
80+
finally {
81+
emf.close();
82+
}
83+
}
84+
85+
@Entity( name = "Customer" )
86+
@Table( name = "CUST" )
87+
public static class Customer {
88+
@Id
89+
private Integer id;
90+
91+
@ElementCollection(fetch = FetchType.EAGER)
92+
@CollectionTable(
93+
name = "cust_color",
94+
joinColumns = @JoinColumn(name = "cust_fk", nullable = false),
95+
uniqueConstraints = @UniqueConstraint(columnNames = { "cust_fk", "color" })
96+
)
97+
@Column(name = "color", nullable = false)
98+
@Convert( converter = ColorTypeConverter.class )
99+
private Set<ColorType> colors = new HashSet<ColorType>();
100+
101+
public Customer() {
102+
}
103+
104+
public Customer(Integer id) {
105+
this.id = id;
106+
}
107+
}
108+
109+
110+
// an enum-like class (converters are technically not allowed to apply to enums)
111+
public static class ColorType {
112+
public static ColorType BLUE = new ColorType( "blue" );
113+
public static ColorType RED = new ColorType( "red" );
114+
public static ColorType YELLOW = new ColorType( "yellow" );
115+
116+
private final String color;
117+
118+
public ColorType(String color) {
119+
this.color = color;
120+
}
121+
122+
public String toExternalForm() {
123+
return color;
124+
}
125+
126+
public static ColorType fromExternalForm(String color) {
127+
if ( BLUE.color.equals( color ) ) {
128+
return BLUE;
129+
}
130+
else if ( RED.color.equals( color ) ) {
131+
return RED;
132+
}
133+
else if ( YELLOW.color.equals( color ) ) {
134+
return YELLOW;
135+
}
136+
else {
137+
throw new RuntimeException( "Unknown color : " + color );
138+
}
139+
}
140+
}
141+
142+
@Converter( autoApply = false )
143+
public static class ColorTypeConverter implements AttributeConverter<ColorType, String> {
144+
145+
@Override
146+
public String convertToDatabaseColumn(ColorType attribute) {
147+
return attribute == null ? null : attribute.toExternalForm();
148+
}
149+
150+
@Override
151+
public ColorType convertToEntityAttribute(String dbData) {
152+
return ColorType.fromExternalForm( dbData );
153+
}
154+
}
155+
}

0 commit comments

Comments
 (0)