I have the simple code below:
@PersistenceContext(name = "mycontext")
private EntityManager entityManager;
public void getAggregatePower() {
String sqlString = "SELECT SUM(power) FROM mytable";
Object singleResult = entityManager.createNativeQuery(sqlString).getSingleResult();
System.out.println(singleResult.getClass().getName());
}
When I run this in a real environment, the print instructions prints java.math.BigDecimal
. But when I run this in my unit tests environment, the print instructions prints java.lang.Double
.
In both cases I use a WildFly 9 server and a Postgresql 9.4 database. I also use Arquillian for unit tests. For me, the only noticeable difference is the number of records in database.
The power
column in mytable
table is a numeric(10,3)
.
I would like to avoid ugly code such as:
if (singleResult instance of Double) {
...
} else if (singleResult instance of BigDecimal) {
...
}
Is there a way to always have the same instance no matter my running environment ?