All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kasinf.framework.rest.eneity.converter.HashMapConverter Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.kasinf.framework.rest.eneity.converter;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.hibernate.HibernateException;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.HashMap;
import java.util.Map;

/**
 * JPA 2.1属性转换器,将HashMap转换成json存储到数据库
 * 

* 键的类型只能是String * * @author lkhsh */ @Converter public class HashMapConverter implements AttributeConverter { @Override public String convertToDatabaseColumn(HashMap attribute) { return MapUtil.isEmpty(attribute) ? "" : JSONObject.toJSONString(attribute, SerializerFeature.WriteClassName); } @Override @SuppressWarnings("unchecked") public HashMap convertToEntityAttribute(String dbData) { if (!StrUtil.isEmpty(dbData)) { HashMap result = new HashMap(); Map map = JSONObject.parseObject(dbData); try { for (Object key : map.keySet()) { Object value = map.get(key); result.put(key, value); } return result; } catch (Exception e) { throw new HibernateException(e); } } return null; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy