Skip to content

Commit

Permalink
Merge pull request #33 from jrfinc/master
Browse files Browse the repository at this point in the history
Added ObjectNodeConverter & JsonType to allow JSON persistency.
  • Loading branch information
ar committed Jan 14, 2016
2 parents 76377e1 + b7cbcf3 commit 1c4cc57
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions libraries.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ext {
jgroupsVersion = '3.6.3.Final'
jaxrsVersion = '2.0.1'
jsonSchemaVersion = '2.2.6'
jacksonVersion = '2.4.5'

libraries = [
//jUnit (Tests)
Expand Down
1 change: 1 addition & 0 deletions modules/dbsupport/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ dependencies {
compile libraries.hibernate_envers
compile libraries.hibernate_c3p0
compile libraries.hibernate_ehcache
compile "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.jpos.ee.converter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.io.IOException;

@Converter(autoApply = true)
public class ObjectNodeConverter implements AttributeConverter<ObjectNode,String> {

@Override
public String convertToDatabaseColumn(ObjectNode attribute) {
return attribute.toString();
}

@Override
public ObjectNode convertToEntityAttribute(String dbData) {
try {
return (ObjectNode) new ObjectMapper().readTree(dbData);
} catch (IOException e) {
return null;
}
}
}

89 changes: 89 additions & 0 deletions modules/dbsupport/src/main/java/org/jpos/ee/usertype/JsonType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.jpos.ee.usertype;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.usertype.UserType;

import java.io.IOException;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

public class JsonType implements UserType {

private static int[] TYPES = { Types.VARCHAR };
private static ObjectMapper mapper = new ObjectMapper();

@Override
public int[] sqlTypes() {
return TYPES;
}

@Override
public Class returnedClass() {
return ObjectNode.class;
}

@Override
public boolean equals(Object x, Object y) throws HibernateException {
return (x == y) || ((x != null) && (y != null) && (x.equals(y)));
}

@Override
public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}

@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
String json = rs.getString(names[0]);
if (rs.wasNull())
return null;
try {
return mapper.readTree(json);
} catch (IOException e) {
return new HibernateException("unable to read object",e);
}
}

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
if (value==null) {
st.setNull(index, Types.VARCHAR);
} else {
st.setString (index, value.toString());
}
}

@Override
public Object deepCopy(Object value) throws HibernateException {
if (value == null)
return null;
return mapper.createObjectNode().putAll((ObjectNode) value);

}

@Override
public boolean isMutable() {
return true;
}

@Override
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value;
}

@Override
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached;
}

@Override
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return this.deepCopy(original);
}
}

0 comments on commit 1c4cc57

Please # to comment.