Using mybatis to convert joda-money with including the currency code

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Using mybatis to convert joda-money with including the currency code

Valentin Wittich
What would be the best practice to convert the joda-money class?

We have stored the amount as DECIMAL(15,2) and the user local in a different table (so far no extra currency code, we use what comes with the local). At the moment our moneyConverter has no options to read the currency code or local from the db. Though we added to the converter an attribute that can be set to a different local so that it can handle different currency inputs from the frontend. But we still have no way to give that information when the value comes from the db.

The question is: what would be the best practice to convert money values from the db into the joda-money class?

eg.
org.joda.money.Money.of(CurrencyUnit.EUR, doubleValue);


Regards Valentin

ps: so far the only subject I found here in the list quite old: https://groups.google.com/d/topic/mybatis-user/N1qtaoXFvPk/discussion

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/c88d55cb-fe36-4e70-9275-8104a1546356%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Using mybatis to convert joda-money with including the currency code

Valentin Wittich
We found the working options to access to the ResultSet in the TypeHandler, therewith we have access to another column.


public Money getNullableResult(ResultSet rs, String columnName) throws SQLException {
   
BigDecimal amount = rs.getBigDecimal(columnName);
   
String code = "EUR";
   
try {
       
int column = rs.findColumn("Code");
       
if (column > 0) {
        code
= rs.getString("Code");
       
}
   
} catch (SQLException e) {
       
// ...
   
}
   
CurrencyUnit currencyUnit = CurrencyUnit.of(code);
   
if (amount == null) {
       
return Money.zero(currencyUnit);
   
} else {
       
return Money.of(currencyUnit, amount);
   
}
}

Hope that helps somebody else!

Regards Valentin


Am Mittwoch, 25. September 2019 10:27:05 UTC+2 schrieb Valentin Wittich:
What would be the best practice to convert the joda-money class?

We have stored the amount as DECIMAL(15,2) and the user local in a different table (so far no extra currency code, we use what comes with the local). At the moment our moneyConverter has no options to read the currency code or local from the db. Though we added to the converter an attribute that can be set to a different local so that it can handle different currency inputs from the frontend. But we still have no way to give that information when the value comes from the db.

The question is: what would be the best practice to convert money values from the db into the joda-money class?

eg.
org.joda.money.Money.of(CurrencyUnit.EUR, doubleValue);


Regards Valentin

ps: so far the only subject I found here in the list quite old: <a href="https://groups.google.com/d/topic/mybatis-user/N1qtaoXFvPk/discussion" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/topic/mybatis-user/N1qtaoXFvPk/discussion&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/topic/mybatis-user/N1qtaoXFvPk/discussion&#39;;return true;">https://groups.google.com/d/topic/mybatis-user/N1qtaoXFvPk/discussion

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/2a53049d-441a-4e91-ae6c-a69e963338da%40googlegroups.com.