0

I tried to run native sql query with resulttransformer (AliasToBeanResultTransformer), it gives error like below.

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.ozpas.ozentegre.entity.EntDevirlog cannot be cast to java.util.Map
    at org.hibernate.property.access.internal.PropertyAccessMapImpl$SetterImpl.set(PropertyAccessMapImpl.java:102)
    at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:78)

By the way, my native sql query does not include all fields in the entity ( EntDevirlog ), there are only some fields in that entity. shall the query include all fields in the entity ?

as i understood, hibernate transforms result into a map object instead EntDevirlog entity. It uses PropertyAccessMapImpl. how can i solve this problem to get the result as a list ( arraylist ) ? thanks.

    Session session = HibernateUtilMikro.getSessionFactory().openSession();

    List<EntDevirlog> results = new ArrayList<EntDevirlog>();
    Transaction tx = null;

String sql =  "mynativequery";

SQLQuery query = session.createSQLQuery(sql);

query.setParameter("tarih", tarih);
query.setParameter("srmkodu", srmkodu);

query.setParameter("s1", EnumPanoislemtipleri.islem1.getValue());
query.setParameter("s2", EnumPanoislemtipleri.islem2.getValue());


query.setResultTransformer(new AliasToBeanResultTransformer(EntDevirlog.class));

results = query.list();
            tx.commit();
2
  • try to modify HibernateUtilMikro.getSessionFactory().openSession().getSession(EntityMode.POJO) Commented Oct 9, 2015 at 7:49
  • there is not a method like getSession ( EntityMode.POJO ) , i use Hibernate 5 Commented Oct 9, 2015 at 8:02

1 Answer 1

1

Just use the quotes for the aliases

"select firstName as \"firstName\", 
    lastName as \"lastName\" from Employee"

Read for a more deeply explanation here: mapping Hibernate query results to custom class?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.