我正在尝试使用Hibernate更新表中的记录,但是记录没有更新,并且在下次执行时需要花费很多时间并引发以下错误:Exception in thread "main" org.hibernate.exception.LockAcquisitionException: could not execute statement
员工:
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.*;
@Entity
@Table(name = "Emp_Table")
public class Employee {
@Id
@GeneratedValue
private int id;
private String name;
private double salary;
@OneToOne(mappedBy = "emp")
@Cascade(value = org.hibernate.annotations.CascadeType.ALL)
private Address addr;
/* Getter-Setter methods */
}
地址:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
@Entity
@Table(name = "Addr_Table")
public class Address {
@Id
@Column(name = "eid")
@GeneratedValue(generator = "gen")
@GenericGenerator(name = "gen", strategy = "foreign", parameters = {@Parameter(name = "property", value = "emp")})
private int id;
private String state;
private String country;
@OneToOne
@PrimaryKeyJoinColumn
private Employee emp;
/* Getter-Setter methods */
}
HQLExample 包含CRUD操作。
package com.Hibernate.Main;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.Hibernate.Model.Employee;
import com.Hibernate.Util.HibernateUtil;
public class HQLExample {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Employee");
List<Employee> emp = query.list();
for (Employee empList : emp) {
System.out.println("\nID:\t" + empList.getId() + "\nName:\t" + empList.getName() + "\nSalary:\t"
+ empList.getSalary());
}
Query query1 = session.createQuery("from Employee where id = :id");
query1.setInteger("id", 2);
Employee empList = (Employee) query1.uniqueResult();
System.out.println(
"\nID:\t" + empList.getId() + "\nName:\t" + empList.getName() + "\nSalary:\t" + empList.getSalary());
Query query2 = session.createQuery("from Employee");
query2.setFirstResult(1);
query2.setFetchSize(1);
emp = query2.list();
for (Employee empList1 : emp) {
System.out.println("\nID:\t" + empList1.getId() + "\nName:\t" + empList1.getName() + "\nSalary:\t"
+ empList1.getSalary());
}
query = session.createQuery("update Employee set name = :name where id = :id");
query.setParameter("name", "Gaurav");
query.setInteger("id", 2);
int result = query.executeUpdate();
System.out.println("\n\nEmployee updated successfully: "+result);
query = session.createQuery("delete from Employee where id: eid");
query.setInteger("eid", 4);
result = query.executeUpdate();
System.out.println("Employee deleted successfully "+ result);*/
}
}
我能够检索记录,但在更新记录期间,它要么不更新记录,要么花费很长时间并抛出错误:线程“main”中的异常org.hibernate.exception.LockAcquisitionException:无法执行语句。
使用命令 show processlist ,我试图在睡眠模式下杀死进程。在杀死这些进程后,我能够执行更新语句,但它不会反映到数据库中。当试图再次执行代码时,将进行长时间运行并失败。
hibernate.cfg.xml中:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root.123</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/Hibernate</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<mapping class="com.Hibernate.Model.Address"></mapping>
<mapping class="com.Hibernate.Model.Employee"></mapping>
</session-factory>
</hibernate-configuration>
答案 0 :(得分:0)
您必须提交事务以将修改保存到数据库。您还必须关闭会话和会话工厂。如果您没有关闭资源,则意味着代码将锁定数据库中的记录。
我还在删除查询中发现了语法错误。以下是您可以执行的更改,以使代码正常工作。
{
$group: {
_id: "$month",
total: { $sum: 1 },
total_canceled: { $sum: { $cond: [{ $eq: ['cancelled_flights', true] }, 1, 0] } }
total_non_canceled: { $sum: { $cond: [{ $eq: ['cancelled_flights', false] }, 1, 0] } }
}
},