我創(chuàng)建了一個轉(zhuǎn)換為另一個類的字段,通過它我得到了json,問題發(fā)生在哪里,但我不明白我做錯了什么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貢獻(xiàn)1848條經(jīng)驗 獲得超10個贊
看一看:
@Convert(converter = PurshasedProductConverter.class)
private PurshasedProductConverter purshasedProducts;
您也用于PurshasedProductConverter注釋和字段類型。您應(yīng)該將其更改為要從/轉(zhuǎn)換為 in 的字段類型PurshasedProductConverter。
@Convert(converter = PurshasedProductConverter.class)
private PurshasedProduct purshasedProducts;
添加回答
舉報
0/150
提交
取消