-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from jrfinc/master
Added ObjectNodeConverter & JsonType to allow JSON persistency.
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
modules/dbsupport/src/main/java/org/jpos/ee/converter/ObjectNodeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
89
modules/dbsupport/src/main/java/org/jpos/ee/usertype/JsonType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |