简体   繁体   English

休眠Java选择查询

[英]hibernate java select queries

i am new to this and today i tried to play hibernate with a method that returns the result of selected row...if is selected then it can return the result in int.. here is my method 我对此并不陌生,今天我尝试使用一种返回选定行结果的方法播放休眠状态...如果被选中,那么它可以将结果返回为int ..这是我的方法

 public int validateSub(String slave, String source, String table){
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();


    Query q = session.createQuery("from Subscribers where slave = :slave AND source = :source AND tbl = :tbl");
    q.setParameter("slave", slave);
    q.setParameter("source", source);
    q.setParameter("tbl", table);

    int result = q.executeUpdate();

    return result;

}

from this method i tried to validate the 3 values that i get from the Subscribers table but at the end i tried to compile having this error 通过这种方法,我尝试验证从订户表中获得的3个值,但最后,我尝试进行编译并出现此错误

     Exception in thread "Thread-0" org.hibernate.hql.QueryExecutionRequestException: Not supported for select queries [from com.datadistributor.main.Subscribers where slave = :slave AND source = :source AND tbl = :tbl]

here you want to select record so it is posible without select key word 在这里您要选择记录,因此无需选择关键字就可以

sessionFactory sesionfatory;
ArrayList list = (ArrayList)sessionfactory.getCurruntSession().find(from table where name LIKE "xyz");

long size = list.get(0);

You can have a look at the below links that how executeUpdate works, one is from the hibernate docs and other the java docs for JPA which defines when the exception is thrown by the method 您可以看一下下面的链接,它们说明executeUpdate的工作方式,一个来自休眠文档,另一个来自JPA的Java文档,它们定义了方法何时抛出异常。

http://docs.oracle.com/javaee/6/api/javax/persistence/Query.html#executeUpdate() http://docs.oracle.com/javaee/6/api/javax/persistence/Query.html#executeUpdate()

https://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Query.html#executeUpdate() https://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Query.html#executeUpdate()

Alternatively you can use 或者,您可以使用

 List list = query.list();    
 int count = list != null ? list.size() : 0;
 return count; 

you are running a select query, Eventhough you are not using the select keyword here hibernate will add that as part of the generated SQL. 您正在运行选择查询,即使您未在此处使用select关键字,hibernate也会将其添加为生成的SQL的一部分。

what you need to do to avoid the exception is the say 您需要做的事来避免例外是说

q.list();

now, this will return a List ( here is the documentation). 现在,这将返回一个列表( 这里是文档)。 if you are trying to get the size of the elements you can say 如果您要获取元素的大小,可以说

    Query q = session.createQuery("select count(s) from Subscribers s where slave = :slave AND source = :source AND tbl = :tbl");
Long countOfRecords = (Long)q.list().get(0);

you can execute update statements as well in HQL, it follows a similar structure as SQL (except with object and properties). 您也可以在HQL中执行更新语句,它遵循与SQL类似的结构(对象和属性除外)。

Hope this helps. 希望这可以帮助。

I also happened to make the same mistake today. 我今天也碰巧犯了同样的错误。
Your SQL statement is not correct. 您的SQL语句不正确。

You can try: 你可以试试:

DELETE from Subscribers WHERE slave = :slave AND source

尝试这个:

int result = q.list().size();

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

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