2

Working with JasperReports where user can save report's parameter HashMap<String, Object> in DB so that it can be used later for auto scheduling and regenerate that report again.

Currently Report Parameters are being stored as string and then encoded into bytes as BLOB i.e. rs.setParameters(reportParams.toString().getBytes("UTF-8"));

This approach is expensive (when auto scheduling triggers) as an overhead to regenerate parameters again and also creates problem with complex parameter values.

I want to change the approach of saving HashMap, So what would that be ?

I can either keep the DB schema as it is and save reportParam by serialising it and deserialise when required again? like shown below, Or would it be better to add another table with 1..* multiplicity, to store parameters separately for each report?

Or is there is any better approach I'm missing ? Also concerned with performance when retrieving reportParams back. Thanks.

public static byte[] serialise(Object obj) {

    byte[] bytearray = null;
    ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();

    ObjectOutputStream objectOutStream;
    try {
        objectOutStream = new ObjectOutputStream(byteOutStream);
        objectOutStream.writeObject(obj);
        bytearray = byteOutStream.toByteArray();
        objectOutStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bytearray;
}

public static ReportParameters deSerialize(byte[] byteArray) throws IOException
{
    ReportParameters reportParam = null;
    ByteArrayInputStream byteInstream = new ByteArrayInputStream(byteArray);
    ObjectInputStream objectInStream;

    try {
        objectInStream = new ObjectInputStream(byteInstream);
        reportParam = (ReportParameters)objectInStream.readObject();
        objectInStream.close();

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    return reportParam ;
}
6
  • do you the requirement to update the hashmap ever. Commented Aug 15, 2018 at 10:12
  • Store the data properly in a database table. There should be plenty of examples out there on persisting maps via JPA e.g. stackoverflow.com/questions/853076/jpa-mapstring-string-mapping Commented Aug 15, 2018 at 10:13
  • 1
    @Dinesh No, Once Scheduled user reportParam hashmap can never be updated. Commented Aug 15, 2018 at 10:21
  • How about saving it as json? Commented Aug 15, 2018 at 10:24
  • 1
    Have you tested serializing and deserializing the HashMap? Have you benchmarked it and compared the performance to your current method? Commented Aug 15, 2018 at 15:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.