Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JPA-无法标记PersistenceException上的回滚_Jpa - Fatal编程技术网

JPA-无法标记PersistenceException上的回滚

JPA-无法标记PersistenceException上的回滚,jpa,Jpa,这是我在这个网站上的第一篇帖子,我敢肯定我会找到解决问题的办法。我对JPA不太在行,所以我只专注于一个问题。无法共享完整的代码,但请通过以下示例了解我试图实现的目标: underOneMethod(){ Person personObj1 = entityManager.createNamedQuery("Select * from Person where smthing.."); personObj1.setFirstName("John"); personObj1 = entityMan

这是我在这个网站上的第一篇帖子,我敢肯定我会找到解决问题的办法。我对JPA不太在行,所以我只专注于一个问题。无法共享完整的代码,但请通过以下示例了解我试图实现的目标:

underOneMethod(){

Person personObj1 = entityManager.createNamedQuery("Select * from Person where smthing..");
personObj1.setFirstName("John");
personObj1 = entityManager.merge(personObj1);
entityManager.flush();

Person personObj2 = entityManager.createNamedQuery("Select * from Person where smthing..");
personObj2.setLastName("Cruz");
personObj2 = entityManager.merge(personObj1);
entityManager.flush();

} // This code throws me exception. I think it will work if I use entityManager.clear() method after first flush call but still it will be good, of someone can me some idea of what is happening in this case and how can it be resolved. Thanks in advance..!

您的代码甚至无法编译,createNamedQuery返回的查询不是Person。假设您调用getSingleResult,但createNamedQuery仍然使用名称而不是SQL,因此您需要createNativeQuery

此外,合并不是必需的,因为您刚刚选择了对象,所以它已经被管理了


包括异常堆栈跟踪和真实代码会有所帮助。

好吧,我很久以前就得到了答案,但在这里没有时间响应。无论如何,现在是回答问题的好时机。正如我在之前的评论中已经提到的那样。我使用了entityManager.clear()方法,它成功了。希望这对其他人也有帮助

underOneMethod(){

Person personObj1 = entityManager.createNamedQuery("Select * from Person where smthing..");
personObj1.setFirstName("John");
personObj1 = entityManager.merge(personObj1);
entityManager.flush();

entityManager.clear();

Person personObj2 = entityManager.createNamedQuery("Select * from Person where smthing..");
personObj2.setLastName("Cruz");
personObj2 = entityManager.merge(personObj1);
entityManager.flush();

}

它编译得很好,返回我们想要的任何类型的对象。是的,你的观点是正确的,它将采取一个名称而不是查询。如果我评论第二部分(从person2到flush调用),我只给出了一个场景及其工作原理。正如James指出的,代码是无法编译的。您的返回类型和输入参数都不正确。