Asuming the following entity:
{code:java} @Entity(name = "CentsAndDollarsAccount") public class CentsAndDollarsAccount {
@Id @GeneratedValue private Long id;
private Long cents;
@Formula( "cast(a.cents as double) / 100" ) private Double dollars;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public Long getCents() { return cents; }
public void setCents(Long cents) { this.cents = cents; }
public Double getDollars() { return dollars; } } {code}
When trying to load the entity, the following SQL query is generated:
{code:sql} select centsanddo0_.id as id1_2_0_, centsanddo0_.cents as cents2_2_0_, cast(a.cents as centsanddo0_.double) / 100 as formula0_0_ from CentsAndDollarsAccount centsanddo0_ where centsanddo0_.id=1 {code}
The problem is that the casting type (e.g. {{double}}) is given an alias (e.g. {{ as centsanddo0_.double }}).
Casting works normally for JPQL queries:
{code:java} Double dollars = (Double) entityManager.createQuery( "select cast(a.cents as double) / 100 as dollars from CentsAccount a" ) .getSingleResult(); assertEquals(12.45d, dollars, 0.01); {code}
Which generates the right SQL statement:
{code:sql} select cast(centsaccou0_.cents as double)/100 as col_0_0_ from CentsAccount centsaccou0_ {code} |
|