Created
January 9, 2019 19:29
-
-
Save mageddo/dea8cd62825464a0f625b1c2fd74afd9 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
package com.mageddo; | |
import org.hibernate.annotations.Type; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.Table; | |
@Table(name = "CUSTOMER") | |
@Entity | |
public class TempEntity { | |
private Integer id; | |
public String json; | |
@Column(name = "JSN_DETAILS") | |
@Type(type = "com.mageddo.JsonBType") | |
public String getJson() { | |
return json; | |
} | |
public TempEntity setJson(String json) { | |
this.json = json; | |
return this; | |
} | |
@Id | |
@Column(name = "IDT_CUSTOMER") | |
public Integer getId() { | |
return id; | |
} | |
public TempEntity setId(Integer id) { | |
this.id = id; | |
return this; | |
} | |
} |
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
package com.mageddo; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.SerializationFeature; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.jmx.export.annotation.ManagedOperation; | |
import org.springframework.jmx.export.annotation.ManagedResource; | |
import org.springframework.stereotype.Component; | |
import org.springframework.transaction.annotation.Transactional; | |
@Component | |
@ManagedResource | |
public class TempJMX { | |
@Autowired | |
private TempDao tempDao; | |
@Transactional | |
@ManagedOperation | |
public void create(){ | |
tempDao.create(new TempEntity().setId((int) System.currentTimeMillis()).setJson("{}")); | |
} | |
@Transactional | |
@ManagedOperation | |
public String find(int id) throws JsonProcessingException { | |
return new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(tempDao.find(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
package com.mageddo; | |
import org.hibernate.HibernateException; | |
import org.hibernate.engine.spi.SessionImplementor; | |
import org.hibernate.type.StandardBasicTypes; | |
import org.hibernate.usertype.EnhancedUserType; | |
import org.postgresql.util.PGobject; | |
import java.io.Serializable; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Types; | |
import java.util.Objects; | |
public class JsonBType implements EnhancedUserType, Serializable { | |
@Override | |
public int[] sqlTypes() { | |
return new int[]{Types.VARCHAR}; | |
} | |
@Override | |
public Class returnedClass() { | |
return String.class; | |
} | |
@Override | |
public boolean equals(Object x, Object y) throws HibernateException { | |
return Objects.equals(x, y); | |
} | |
@Override | |
public int hashCode(Object o) throws HibernateException { | |
return Objects.hashCode(o); | |
} | |
@Override | |
public Object nullSafeGet( | |
ResultSet rs, String[] names, SessionImplementor session, Object owner | |
) throws HibernateException, SQLException { | |
final PGobject o = (PGobject) rs.getObject(names[0]); | |
return o.getValue(); | |
} | |
@Override | |
public void nullSafeSet( | |
PreparedStatement stm, Object value, int index, SessionImplementor session | |
) throws HibernateException, SQLException { | |
if (value == null) { | |
stm.setNull(index, Types.OTHER); | |
} else { | |
stm.setObject(index, value, Types.OTHER); | |
} | |
} | |
@Override | |
public Object deepCopy(Object value) throws HibernateException { | |
return value; | |
} | |
@Override | |
public boolean isMutable() { | |
return false; | |
} | |
@Override | |
public Serializable disassemble(Object value) throws HibernateException { | |
return (Serializable) value; | |
} | |
@Override | |
public Object assemble(Serializable cached, Object value) throws HibernateException { | |
return cached; | |
} | |
@Override | |
public Object replace(Object original, Object target, Object owner) throws HibernateException { | |
return original; | |
} | |
@Override | |
public String objectToSQLString(Object object) { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public String toXMLString(Object o) { | |
return o == null ? null : String.valueOf(o); | |
} | |
@Override | |
public Object fromXMLString(String str) { | |
return str; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment