I have a HashMap of about 300 Key/String Value pairs and a POJO with about 12 string attributes where the names match the key names.
I would like to know how to get the HashMap values into the POJO?
I made this start which uses relfection and a loop but wasn't sure how to dynamically construct the setter method name, and apparently reflection is a bad idea anyway...but FWIW:
public void writeToFile(Map<String, String> currentSale) throws IOException {
SaleExport saleExport = new SaleExport();
Field[] fields = saleExport.getClass().getDeclaredFields();
for (Field field : fields ) {
System.out.println(field.getName());
saleExport.set +field(saleExport.get(field));
I have used map struct once before but it does not appear to support HashMaps.
UPDATE
This answer looks similar to what I want to do but gave a stack error on fields that didn't map:
Exception in thread "Thread-2" java.lang.IllegalArgumentException: Unrecognized field "Physical" (class com.SaleExport), not marked as ignorable (6 known properties: "date", "city", "surname", "streetName", "salesNo", "salesSurname"])
at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.SalesExport["Physical"])
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3738)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3656)
at com.CSVExport.writeToFile(CSVExport.java:20)
at com.JFrameTest.writefiletoDB(JFrameTest.java:135)
at com.JFrameTest$FileWorkerThread.run(JFrameTest.java:947)
To ignore the errors I tried :
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
But then nothing got mapped.