6 Replies Latest reply: May 8, 2012 2:14 AM by Jackie Li RSS

Problem with native query

Kaim Khairov Newbie

Hello!

I have experienced a problem using native query for selecting CHAR(3) column from DB.

The code is:

@Stateless
public class NtrBean implements NtrRemote
{
 @PersistenceContext(unitName = "probe")
 EntityManager entityManager;

 public String getMeasure(Long tnvedId) throws Exception
 {
 Query getMeasureQuery = entityManager.createNativeQuery("select test from test_table where id = :id");
 getMeasureQuery.setParameter("id", tnvedId);
 try
 {
 return (String)getMeasureQuery.getSingleResult();
 }
 catch(NoResultException ex)
 {
 return null;
 }
 }
}


As a result of executing of the code snippet I get:

javax.ejb.EJBException: java.lang.ClassCastException: java.lang.Character


So, here it is: native query selects CHAR(3) as one Character object. If I have 'her' value in my 'test' column, then I get 'h'.

I couldn't find any reasonable explanation of this. I've tried this on Informix 9.4 and MySQL 5.0 DB's and had the same effect.

When

Query getMeasureQuery = entityManager.createNativeQuery("select test from test_table where id = :id");


is replaced by:

Query getMeasureQuery = entityManager.createQuery("select ts.test from TestTable as ts where ts.id = :id");


everything works just great.

I don't think problem could reside in my DB-Java mappings in standardjbosscmp-jdbc.xm as I've already tried to play with it.

Could anyone help me?