如何在不使用hql和sql的情况下使用hibernate更新java中的表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21250183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:40:06  来源:igfitidea点击:

how to update the table in java using hibernate without using hql and sql

javahibernatehql

提问by user3205372

I have tried a lot to update my table using hql but i didn't find the solution , i have searched on internet too, I am new in java and hibernate please help me to find the solution.

我尝试了很多使用 hql 更新我的表,但我没有找到解决方案,我也在互联网上搜索过,我是 Java 和休眠的新手,请帮助我找到解决方案。

my code is written below.

我的代码写在下面。

session.getTransaction().begin();
Query query = session.createQuery("update DocDetail set DocName = :docname" + 
    " where Id = :docId");
query.setParameter("docname", "Hyman");
query.setParameter("docId", 3);
int result = query.executeUpdate();
session.getTransaction().commit();

but I got the following error.

但我收到以下错误。

Exception in thread "AWT-EventQueue-0" org.hibernate.QueryException: query must begin with SELECT or FROM: update [update clinic.entity.DocDetail set DocName = :studentName where Id = :studentId]
at org.hibernate.hql.classic.ClauseParser.token(ClauseParser.java:106)
at org.hibernate.hql.classic.PreprocessingParser.token(PreprocessingParser.java:131)
at org.hibernate.hql.classic.ParserHelper.parse(ParserHelper.java:51)

回答by faisalbhagat

If you are using hibernate, you should try to access entities not tables.
The biggest advantage of hibernate is that it provides you ORM (object relational mapping).
Here is the example how to update an entity with hibernate
(of course corresponding table is also updated).

如果您正在使用休眠,您应该尝试访问实体而不是表。
Hibernate 的最大优点是它为您提供了 ORM(对象关系映射)。
下面是如何用hibernate更新实体的例子
(当然相应的表也更新了)。

/* Method to UPDATE salary for an employee */
   public void updateEmployee(Integer EmployeeID, int salary ){
      Session session = factory.openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         Employee employee = 
                    (Employee)session.get(Employee.class, EmployeeID); 
         employee.setSalary( salary );
         session.update(employee); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }

回答by Divers

To update object without SQL or HQL you can use next code snippet.

要在没有 SQL 或 HQL 的情况下更新对象,您可以使用下一个代码片段。

 Session sess = factory.openSession();
 Transaction tx;
 try {
     tx = sess.beginTransaction();
     sess.update(yourObject);
     tx.commit();
 }
 catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
 }
 finally {
     sess.close();
 }

Read documentation about update- possible you have to use mergeor saveOrUpdate.

阅读有关更新的文档- 可能您必须使用mergesaveOrUpdate

回答by Ashish Jagtap

You are creating a Native(SQL) query using createQuery()method instead of createSQLQuery()method so just change your code as follows

您正在使用createQuery()方法而不是createSQLQuery()方法创建本机(SQL)查询,因此只需按如下方式更改代码

session.getTransaction().begin();
Query query = session.createSQLQuery(
    "update DocDetail set DocName = :docname" + " where Id = :docId");
query.setParameter("docname", "Hyman");
query.setParameter("docId", 3);
int result = query.executeUpdate();
session.getTransaction().commit();

read about about this in detail:

详细阅读:

回答by Roopa Veriton

Here a way of updating data into table using hibernate hql:

这是一种使用 hibernate hql 将数据更新到表中的方法:

Configuration cfg = new Configuration();
cfg.configure("HibernateService/hibernate.cfg.xml");

SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction t = session.beginTransaction();

String hql = "UPDATE Userreg SET uname = :uname, uemail = :uemail, uphone = :uphone WHERE uemail = :uemail";

Query query = session.createQuery(hql);
query.setParameter("uname", uname);
query.setParameter("uemail", uemail);
query.setParameter("uphone", uphone);
int rr = query.executeUpdate();

t.commit();

if (rr != 0) {
    return true;
} else {
    return true;
}

回答by u8987698

you can use hibernate session's merge. such as

您可以使用休眠会话的合并。如

User user = session.find("1"); 
//get Persistence entity``String userName = user.getUserName(); //  userName = "enzo"
//user.setUserName("leo");
session.merge(user);
// Test entity user's useName
String userNameNew = session.find("1").getUserName; // now userName is "leo"

I hope can help you;

希望能帮到你;