How to map a map JSON column to Java Object with JPA

前端 未结 6 1023
一整个雨季
一整个雨季 2020-12-02 14:22

We have a big table with a lot of columns. After we moved to MySQL Cluster, the table cannot be created because of:

ERROR 1118 (42000): Row size too l

6条回答
  •  伪装坚强ぢ
    2020-12-02 15:02

    I had a similar problem, and solved it by using @Externalizer annotation and Jackson to serialize/deserialize data (@Externalizer is OpenJPA-specific annotation, so you have to check with your JPA implementation similar possibility).

    @Persistent
    @Column(name = "params")
    @Externalizer("toJSON")
    private Params params;
    

    Params class implementation:

    public class Params {
        private static final ObjectMapper mapper = new ObjectMapper();
    
        private Map map;
    
        public Params () {
            this.map = new HashMap();
        }
    
        public Params (Params another) {
            this.map = new HashMap();
            this.map.putAll(anotherHolder.map);
        }
    
        public Params(String string) {
            try {
                TypeReference> typeRef = new TypeReference>() {
                };
                if (string == null) {
                    this.map = new HashMap();
                } else {
                    this.map = mapper.readValue(string, typeRef);
                }
            } catch (IOException e) {
                throw new PersistenceException(e);
            }
        }
    
        public String toJSON() throws PersistenceException {
            try {
                return mapper.writeValueAsString(this.map);
            } catch (IOException e) {
                throw new PersistenceException(e);
            }
        }
    
        public boolean containsKey(String key) {
            return this.map.containsKey(key);
        }
    
        // Hash map methods
        public Object get(String key) {
            return this.map.get(key);
        }
    
        public Object put(String key, Object value) {
            return this.map.put(key, value);
        }
    
        public void remove(String key) {
            this.map.remove(key);
        }
    
        public Object size() {
            return map.size();
        }
    }
    

    HTH

提交回复
热议问题