Skip to content

Commit d1e4f9b

Browse files
committed
Adding support for Set user type as json column.
1 parent 4e83a58 commit d1e4f9b

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright (C) 2016 Marvin Herman Froeder ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marvinformatics.hibernate.json;
17+
18+
import java.util.Iterator;
19+
import java.util.Map;
20+
import java.util.Set;
21+
import com.fasterxml.jackson.databind.JavaType;
22+
import com.fasterxml.jackson.databind.ObjectMapper;
23+
import org.hibernate.HibernateException;
24+
import org.hibernate.collection.internal.PersistentSet;
25+
import org.hibernate.collection.spi.PersistentCollection;
26+
import org.hibernate.engine.spi.SessionImplementor;
27+
import org.hibernate.persister.collection.CollectionPersister;
28+
import org.hibernate.usertype.UserCollectionType;
29+
30+
public class JsonSetUserType extends JsonUserType implements UserCollectionType {
31+
public JsonSetUserType() {
32+
}
33+
34+
public JavaType createJavaType(ObjectMapper mapper) {
35+
return mapper.getTypeFactory().constructCollectionType(Set.class, this.returnedClass());
36+
}
37+
38+
public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister) throws HibernateException {
39+
return new PersistentSet(session);
40+
}
41+
42+
private PersistentSet cast(Object collection) {
43+
return (PersistentSet) collection;
44+
}
45+
46+
public PersistentCollection wrap(SessionImplementor session, Object collection) {
47+
return new PersistentSet(session, (Set<?>) collection);
48+
}
49+
50+
public Iterator<?> getElementsIterator(Object collection) {
51+
return this.cast(collection).iterator();
52+
}
53+
54+
public boolean contains(Object collection, Object entity) {
55+
return this.cast(collection).contains(entity);
56+
}
57+
58+
public Object indexOf(Object collection, Object entity) {
59+
throw new UnsupportedOperationException();
60+
}
61+
62+
public Object replaceElements(Object original,
63+
Object target,
64+
CollectionPersister persister,
65+
Object owner,
66+
Map copyCache,
67+
SessionImplementor session) throws HibernateException {
68+
PersistentSet originalSet = this.cast(original);
69+
PersistentSet targetSet = this.cast(target);
70+
targetSet.clear();
71+
targetSet.addAll(originalSet);
72+
return target;
73+
}
74+
75+
public Object instantiate(int anticipatedSize) {
76+
return new PersistentSet();
77+
}
78+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Copyright (C) 2016 Marvin Herman Froeder ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marvinformatics.hibernate.json;
17+
18+
import java.util.HashSet;
19+
import java.util.Set;
20+
import com.marvinformatics.hibernate.json.model.Label;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import static org.hamcrest.MatcherAssert.assertThat;
25+
import static org.hamcrest.Matchers.allOf;
26+
import static org.hamcrest.Matchers.containsInAnyOrder;
27+
import static org.hamcrest.Matchers.containsString;
28+
import static org.hamcrest.Matchers.equalTo;
29+
import static org.hamcrest.Matchers.hasProperty;
30+
import static org.hamcrest.Matchers.hasSize;
31+
import static org.hamcrest.Matchers.is;
32+
import static org.hamcrest.Matchers.not;
33+
import static org.hamcrest.Matchers.sameInstance;
34+
35+
public class JsonSetUserTypeTest {
36+
37+
private JsonSetUserType type = null;
38+
39+
@Before
40+
public void createType() {
41+
type = new JsonSetUserType() {
42+
@Override
43+
public Class<?> returnedClass() {
44+
return Label.class;
45+
}
46+
};
47+
}
48+
49+
@SuppressWarnings("unchecked")
50+
@Test
51+
public void testConvertJsonToObject() throws Exception {
52+
String json = "[{\"value\": \"eclair\", \"lang\":\"fr\"}, {\"value\": \"saffron bun\", \"lang\":\"en\"}]";
53+
54+
Set<Label> labels = (Set<Label>) type.convertJsonToObject(json);
55+
56+
assertThat(labels, hasSize(2));
57+
assertThat(labels, containsInAnyOrder(
58+
allOf(hasProperty("value", is("eclair")), hasProperty("lang", is("fr"))),
59+
allOf(hasProperty("value", is("saffron bun")), hasProperty("lang", is("en")))));
60+
}
61+
62+
@Test
63+
public void testConvertObjectToJson() throws Exception {
64+
Set<Label> labels = new HashSet<>();
65+
labels.add(new Label("eclair", "fr", 1));
66+
labels.add(new Label("saffron bun", "en", 2));
67+
68+
String json = type.convertObjectToJson(labels);
69+
70+
assertThat(json, containsString("{\"value\":\"eclair\",\"lang\":\"fr\",\"order\":1}"));
71+
assertThat(json, containsString("{\"value\":\"saffron bun\",\"lang\":\"en\",\"order\":2}"));
72+
}
73+
74+
@SuppressWarnings("unchecked")
75+
@Test
76+
public void testDeepCopy() throws Exception {
77+
Set<Label> labels = new HashSet<>();
78+
labels.add(new Label("eclair", "fr", 3));
79+
labels.add(new Label("saffron bun", "en", 4));
80+
81+
Set<Label> copy = (Set<Label>) type.deepCopy(labels);
82+
83+
assertThat(labels, hasSize(2));
84+
assertThat(copy, equalTo(labels));
85+
assertThat(copy, not(sameInstance(labels)));
86+
87+
assertThat(labels, containsInAnyOrder(
88+
allOf(hasProperty("value", is("eclair")), hasProperty("lang", is("fr"))),
89+
allOf(hasProperty("value", is("saffron bun")), hasProperty("lang", is("en")))));
90+
91+
}
92+
93+
}

0 commit comments

Comments
 (0)