Does JPA have the equivalent of Hibernate SQLQuery.addScalar ()?

My old hibernate code uses something like

session.createSQLQuery("SELECT * FROM CATS")
.addScalar("ID", Hibernate.LONG)
.addScalar("NAME", Hibernate.STRING)
.addScalar("BIRTHDATE", Hibernate.DATE)

In the new project, we use the Hibernate EntityManager (JPA implementation).

Is there an equivalent to these addScalar () calls? Do I need to specify return column types as before?

For example, if I do not use addScalar, will the results of the SQL query be cached?

+3
source share
3 answers

Hibernate native query message - char (3) column

seems to offer a similar, shorter solution that seems to work for me:

Query q2 = em.createNativeQuery("select sc_cur_code, sc_amount from sector_costs");
q2.unwrap(SQLQuery.class).addScalar("sc_cur_code", StringType.INSTANCE);
+2
source

I searched the same and could not find anything in JPA.

Wow, 6 months and don't answer :)

, , :

, SQLQuery, HibernateQuery, getHibernateQuery, SQLQuery.

(scala) :

val sql = "select distinct story as story from ...";
val q: Query = getEntityManager().createNativeQuery(sql);
//hello nasty hack
q.asInstanceOf[HibernateQuery].getHibernateQuery().asInstanceOf[SQLQuery].addScalar("story", StandardBasicTypes.LONG);
//next, caching  
q.setHint("org.hibernate.cacheable", true);
q.setHint("org.hibernate.cacheRegion", "query.getTopLinks");

, :)

+1

, , -

createNativeQuery , , ,

val sql = "select distinct story as story from ...";
Query = getEntityManager().createNativeQuery(sql,Story.class);
+1

Source: https://habr.com/ru/post/1775880/


All Articles