简体   繁体   English

将SQL语句'LIKE'转换为JPQL语句

[英]Convert SQL statement 'LIKE' to JPQL statement

I want to write this SQL statement in eclipse JPQL query. 我想在Eclipse JPQL查询中编写此SQL语句。 It works in SQL but I'm not sure how to write it in eclipse. 它可以在SQL中工作,但是我不确定如何在Eclipse中编写它。

SELECT * 
FROM hardware h
WHERE h.`Staffid` LIKE '%150%'

My staffid in hardware table is a foreign key to staff table's staffid primary key. 我在硬件表中的人员ID是人员表的人员ID主键的外键。 So the staff in the hardware table is 所以硬件表中的人员是

private Staff staff;

This is what I write to run my search: 这是我写来运行搜索的内容:

@SuppressWarnings("unchecked")
public List<Hardware> searchstaff(Staff a) {
    try {
        entityManager.getTransaction().begin();             

        Query query = entityManager
            .createQuery("SELECT st from Hardware st where st.staff LIKE :x");
        query.setParameter("x", a);

        List<Hardware> list = query.getResultList(); 
        entityManager.getTransaction().commit();         
        return list;                     
    } catch (Exception e) {
        return null;
    }
}

But it shows 但这表明

javax.servlet.ServletException: java.lang.IllegalStateException: 
Exception Description: Transaction is currently active 

when I run the search. 当我运行搜索时。

Can anyone help me correct? 谁能帮我纠正?

The like operator work only with string. like运算符仅适用于字符串。 So do something like that: 所以做这样的事情:

Query query = entityManager
    .createQuery("SELECT st from Hardware st where st.staff.id LIKE :x");
query.setParameter("x", '%' + a.getId() + '%');

Link : 连结:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM