Created
May 31, 2016 19:03
-
-
Save thjanssen/9f1d80b3743cac0066c1ce1a563e3ef6 to your computer and use it in GitHub Desktop.
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
@Entity | |
public class MyEntity { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
@Column(name = "id", updatable = false, nullable = false) | |
private Long id; | |
@Column | |
private MyJson jsonProperty; | |
... | |
} |
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
CREATE TABLE myentity | |
( | |
id bigint NOT NULL, | |
jsonproperty jsonb, | |
CONSTRAINT myentity_pkey PRIMARY KEY (id) | |
) |
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
@Entity | |
public class MyEntity { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
@Column(name = "id", updatable = false, nullable = false) | |
private Long id; | |
@Column | |
@Type(type = "MyJsonType") | |
private MyJson jsonProperty; | |
... | |
} |
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
public class MyJson implements Serializable { | |
private String stringProp; | |
private Long longProp; | |
public String getStringProp() { | |
return stringProp; | |
} | |
public void setStringProp(String stringProp) { | |
this.stringProp = stringProp; | |
} | |
public Long getLongProp() { | |
return longProp; | |
} | |
public void setLongProp(Long longProp) { | |
this.longProp = longProp; | |
} | |
} |
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
public class MyJsonType implements UserType { | |
@Override | |
public int[] sqlTypes() { | |
return new int[]{Types.JAVA_OBJECT}; | |
} | |
@Override | |
public Class<MyJson> returnedClass() { | |
return MyJson.class; | |
} | |
... | |
} |
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
@Override | |
public Object nullSafeGet(final ResultSet rs, final String[] names, final SessionImplementor session, | |
final Object owner) throws HibernateException, SQLException { | |
final String cellContent = rs.getString(names[0]); | |
if (cellContent == null) { | |
return null; | |
} | |
try { | |
final ObjectMapper mapper = new ObjectMapper(); | |
return mapper.readValue(cellContent.getBytes("UTF-8"), returnedClass()); | |
} catch (final Exception ex) { | |
throw new RuntimeException("Failed to convert String to Invoice: " + ex.getMessage(), ex); | |
} | |
} | |
@Override | |
public void nullSafeSet(final PreparedStatement ps, final Object value, final int idx, | |
final SessionImplementor session) throws HibernateException, SQLException { | |
if (value == null) { | |
ps.setNull(idx, Types.OTHER); | |
return; | |
} | |
try { | |
final ObjectMapper mapper = new ObjectMapper(); | |
final StringWriter w = new StringWriter(); | |
mapper.writeValue(w, value); | |
w.flush(); | |
ps.setObject(idx, w.toString(), Types.OTHER); | |
} catch (final Exception ex) { | |
throw new RuntimeException("Failed to convert Invoice to String: " + ex.getMessage(), ex); | |
} | |
} |
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
@Override | |
public Object deepCopy(final Object value) throws HibernateException { | |
try { | |
// use serialization to create a deep copy | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
ObjectOutputStream oos = new ObjectOutputStream(bos); | |
oos.writeObject(value); | |
oos.flush(); | |
oos.close(); | |
bos.close(); | |
ByteArrayInputStream bais = new ByteArrayInputStream(bos.toByteArray()); | |
return new ObjectInputStream(bais).readObject(); | |
} catch (ClassNotFoundException | IOException ex) { | |
throw new HibernateException(ex); | |
} | |
} |
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
public class MyPostgreSQL94Dialect extends PostgreSQL94Dialect { | |
public MyPostgreSQL94Dialect() { | |
this.registerColumnType(Types.JAVA_OBJECT, "jsonb"); | |
} | |
} |
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
@org.hibernate.annotations.TypeDef(name = "MyJsonType", typeClass = MyJsonType.class) | |
package org.thoughts.on.java.model; |
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
MyEntity e = em.find(MyEntity.class, 10000L); | |
e.getJsonProperty().setStringProp("changed"); | |
e.getJsonProperty().setLongProp(789L); |
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
MyEntity e = (MyEntity) em.createNativeQuery("SELECT * FROM myentity e WHERE e.jsonproperty->'longProp' = '456'", MyEntity.class).getSingleResult(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment