0

My Hibernate named query calls a stored procedure which returns a date as a formatted string (e.g. "2017/03/15") as one of its columns.

<sql-query name="myReportQuery">
    <return alias="myReportQuery" class="com.example.report.ReportLine" />
    { call sproc_report( :fromDate, :toDate) }
</sql-query>

Sample output from callilng stored procedure:

Date        Item    Price  Qty
----        ----    -----  ---
2017/01/22  Banana  0.75   10
2017/02/15  Apple   1.00   5
2017/02/25  Pear    1.50   8
2017/03/05  Apple   1.00   15

Trouble is that I would like to store this field as a Date instead of a String.

public class ReportLine {
    private Date date;
    private String item;
    private BigDecimal price;
    private int quantity;
}

Is there any way for me to do this?

For reference, I am using Java 6 and Hibernate 3.5.

1
  • 1
    Couldn't you make string-to-date/date-to-string conversion in your getter and setter methods? Commented Mar 15, 2017 at 18:52

2 Answers 2

2

Simple code in setter may work:

String sDate1="31/12/1998";  
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
Sign up to request clarification or add additional context in comments.

2 Comments

I ended up using a variant of your solution.
Yes, you just needed to alter it a little bit (yyyy/mm/dd) I guess!
0

One of the constructors for the Date class is Date(int year, int month, int day).

So you can take your formatted string, split it with String.split("/"), and extract the appropriate fields to use in the constructor.

2 Comments

While this will work, don't re-invent the wheel. SimpleDateFormat will handle conversions.
My hexagonal wheels will catch on someday.

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.