我正在尝试在 hibernate 中执行查询,我正在使用 StringBuffer 进行查询。我在 createNativeQuery(...)
上收到错误。当我将鼠标悬停在该方法上时,它会显示
The method createNativeQuery(String, Class) in the type EntityManager is not applicable for the arguments (StringBuffer, Class).
我尝试将方法更改为 createNamedQuery(..)
和 createQuery(..)
但它不起作用。有什么建议么?下面是我的代码
EntityManager em = getEntityManager();
logger.info("In getTrafficProfileByCosClass2Allocations className :- " + cosClassAllocation );
Query query = em.createNativeQuery(queryByCos2Allocation, TrafficProfile.class);
query.setParameter(1, direction);
query.setParameter(2, cosClassAllocation.getCos1());
query.setParameter(3, cosClassAllocation.getCos2());
return (TrafficProfile) query.getResultList();
最佳答案
createNativeQuery
方法需要一个 String
作为第一个参数,它可以轻松地从 StringBuffer
创建。
假设 queryByCos2Allocation
是 StringBuffer
,请更改此:
Query query = em.createNativeQuery(queryByCos2Allocation, TrafficProfile.class);
对此:
Query query = em.createNativeQuery(queryByCos2Allocation.toString(), TrafficProfile.class);
关于java - Hibernate,使用createNativeQuery方法时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32281065/