当我在数据库可视化工具中运行我的查询时它工作得很好,但我认为在我的DAO类方法中转换它时语法中存在一些问题。
我希望根据提供的名称获取整个数据
在Visualizer中:
SELECT first_name,last_name,nic,phone,email FROM x_hr_user where (first_name = 'Irum');
现在在Dao
public List<XHrUser> findXHrUserByNameInTable()
{
String name ="Irum";
Query query = em.createQuery("SELECT xHrNewUserObj.firstName,xHrNewUserObj.lastName, xHrNewUserObj.nic, xHrNewUserObj.phone, xHrNewUserObj.emil FROM XHrUser xHrNewUserObj where (xHrNewUserObj.firstName) = (name)");
List<XHrUser> list = query.getResultList();
return list;
}
它不显示单行,而是显示整个数据表 谢谢
答案 0 :(得分:0)
您必须编写如下查询。其中:用于变量
Query query = em.createQuery("SELECT xHrNewUserObj.firstName,xHrNewUserObj.lastName, xHrNewUserObj.nic, xHrNewUserObj.phone, xHrNewUserObj.emil FROM XHrUser xHrNewUserObj where (xHrNewUserObj.firstName) = :name");
答案 1 :(得分:0)
您当前的查询无效JPQL。您似乎打算将原始name
字符串插入到查询中,这可以通过本机查询来完成,但肯定是不可取的。而是在JPQL查询中使用命名参数,然后将name
绑定到它。
String name = "Irum";
Query query = em.createQuery("SELECT x FROM XHrUser WHERE x.firstName = :name")
.setParameter("name", name);
List<XhrUser> list = query.getResultList();