Pergunta

I am using a bean object to retrieve data from database, called AudienceInfo. AudienceInfo has two attributes:

public String messageBody;
public BigDecimal audienceNo;

in database messageBody is varchar2 and audienceNo is number. for fetching data in DAO class, everything is ok and data have been selected. but when I pass the object (customer) form Dao class to my main class an error happens. here is my code in main class:

Iterator<AudienceInfo> iterator = customer.iterator();
int i = 0;
while (iterator.hasNext()) {
    BigDecimal com = (customer.get(i).getAudienceno());
    String msgBody = customer.get(i).getMessageBody();
    iterator.next();
    ++i;
}

the error is:

Exception in thread "main" java.lang.ClassCastException: java.math.BigDecimal cannot be cast to com.htsc.sms.model.bo.AudienceInfo

when I use this code in jdbc, everything is ok and I can see the result set but in hibernate I have this problem.

Foi útil?

Solução

I myself found the answer: using addScalar in query solved the problem

Query query = session.createSQLQuery("select AUDIANCE_NO as audianceno , MSG_BODY as messageBody from SMS_MSG_QUEUE").addScalar("audianceno" , new BigDecimalType()).addScalar("messageBody" , new StringType()).setResultTransformer(Transformers.aliasToBean(AudinceInfo.class));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top