我创建了一个转换为另一个类的字段,通过它我得到了json,问题发生在哪里,但我不明白我做错了什么User班级public class User extends BaseEntity<Integer> { private String firstName; private String lastName; @Column(name = "username") private String username; @Convert(converter = PurshasedProductConverter.class) private PurshasedProductConverter purshasedProducts;}PurshasedProductConverter班级public class PurshasedProductConverter implements AttributeConverter<PurshasedProduct, String> { private static final Logger LOG = (Logger) LoggerFactory.getLogger(PurshasedProductConverter.class); private final ObjectMapper mapper = new ObjectMapper(); @Override public String convertToDatabaseColumn(PurshasedProduct attribute) { try { if (attribute != null) return mapper.writeValueAsString(attribute); else return null; } catch (JsonProcessingException e) { LOG.error(e.getMessage(), e); throw new RuntimeException(e); } } @Override public PurshasedProduct convertToEntityAttribute(String dbData) { TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() { }; try { PurshasedProduct purshasedProduct = new PurshasedProduct(); if (dbData != null) purshasedProduct.strings = mapper.readValue(dbData, typeRef); return purshasedProduct; } catch (IOException e) { LOG.error(e.getMessage(), e); throw new RuntimeException(e); } }}PurshasedProduct班级public class PurshasedProduct { public Map<String, String> strings = new HashMap<>(); public static PurshasedProduct create(String key, String value) { PurshasedProduct purshasedProduct = new PurshasedProduct(); purshasedProduct.set(key, value); return purshasedProduct; } public void set(String lang, String text) { if (text == null || lang == null) { return; } strings.put(lang, text); }
1 回答

慕桂英546537
TA贡献1848条经验 获得超10个赞
看一看:
@Convert(converter = PurshasedProductConverter.class)
private PurshasedProductConverter purshasedProducts;
您也用于PurshasedProductConverter注释和字段类型。您应该将其更改为要从/转换为 in 的字段类型PurshasedProductConverter。
@Convert(converter = PurshasedProductConverter.class)
private PurshasedProduct purshasedProducts;
添加回答
举报
0/150
提交
取消