简体   繁体   English

JPA查询引发异常

[英]JPA query throws the exception

I am trying to save one record into the mysql 5.1 db. 我试图将一条记录保存到mysql 5.1 db中。 When I use entity manager.persist() or merge() method then it is not even saving a record to db and when I try to use native insert statement like this the it throws exception 当我使用实体manager.persist()或merge()方法时,它甚至都没有将记录保存到db,并且当我尝试使用此类原生插入语句时,它将引发异常

Query query = entityManager.createNativeQuery("insert into Employee(firstName,lastName,phone,email) values(?,?,?,?);");
    query.setParameter(1, employee.getFirstName());
    query.setParameter(2, employee.getLastName());
    query.setParameter(3, employee.getPhone());
    query.setParameter(4, employee.getEmail());

Exception thrown from the above query is 从上述查询引发的异常是

org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.GenericJDBCException: could not execute native bulk manipulation query; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute native bulk manipulation query

Please help me !!!! 请帮我 !!!!

Thanks 谢谢

While I can't immediately help you with the query, because you are most probably omitting an important part of the stacktrace, I have a solution: 虽然我不能立即为您提供查询帮助,但是由于您很可能省略了堆栈跟踪的重要部分,因此我有一个解决方案:

Don't use INSERT HQL queries. 不要使用INSERT HQL查询。 This is not the way JPA is supposed to work. 这不是JPA应该工作的方式。 Instead create a new Emplyee() object, which should be an @Entity , and set its attributes, then entityManager.persist(employee) . 而是创建一个new Emplyee()对象,该对象应为@Entity并设置其属性,然后设置entityManager.persist(employee)

In order for this to work, you need a running transaction. 为了使其正常工作,您需要一个正在运行的事务。 entityManager.getTransaction().being() would start one, and entityManager.getTransaction().commit() would commit it. entityManager.getTransaction().being()将开始一个,而entityManager.getTransaction().commit()将提交它。

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

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