问题描述
我最近在Eclipse IDE的TomEE 1.7中迁移到了JPA 2.0(EclipseLink 2.4.2 Juno),因为我正在存储它们的值,所以可以很好地存储和检索它们,但是在使用它们时它们并没有持久化到数据库中entitymanager.flush()
它显示javax.persistence.TransactionRequiredException
这是我的代码
I have recently migrated to JPA 2.0 (EclipseLink 2.4.2 Juno) in TomEE 1.7 in Eclipse IDE, As i am storing the values they are getting stored and retrieved fine, but they are not persisting into the database when is use entitymanager.flush()
it is showing javax.persistence.TransactionRequiredException
Here is my code
Create.java(注册方法)
Create.java (register method)
public static int register(String first, String last, String email,
String date, String phone, String address, String pin, Login login) {
try {
System.out.println("registering persisting the entity");
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("FirstEE");
EntityManager manager = emf.createEntityManager();
manager.getTransaction().begin();
//
// Query query = manager
// .createQuery("select l from Truck l");
Login log = login;
System.out.println(log.getUsername() + "username"
+ log.getPassword() + "password");
User reg = new User();
reg.setLogin(log);
reg.setDate(date);
reg.setEmail(email);
reg.setFirst(first);
reg.setLast(last);
reg.setPhone(phone);
reg.setAddress(address);
reg.setPin(pin);
manager.persist(reg);
manager.getTransaction().commit();
manager.flush();
manager.close();
emf.close();
// FacesContext.getCurrentInstance().addMessage("reg:result",
// new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error Message",
// "Registered Successfully"));
FacesContext facesContext = FacesContext.getCurrentInstance();
FacesMessage facesMessage = new FacesMessage(
"Registered Successfully");
facesContext.addMessage(null, facesMessage);
System.out.println("after message global");
return 1;
} catch (Exception e) {
System.out.println("hai this is exception caught:" + e);
System.out.println("hai" + e.getMessage());
FacesContext.getCurrentInstance().addMessage(
"reg:result",
new FacesMessage("Something went wrong",
"\tSomething went wrong\t"));
// FacesContext facesContext = FacesContext.getCurrentInstance();
// FacesMessage facesMessage = new
// FacesMessage("Something went wrong");
// facesContext.addMessage(null, facesMessage);
}
return 0;
}
persistence.xml
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="FirstEE" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<non-jta-data-source>FirstEE</non-jta-data-source>
<!-- <exclude-unlisted-classes>false</exclude-unlisted-classes> -->
<class>com.jason.Entity.User</class>
<class>com.jason.ManagedBean.Login</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/yash" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.ddl-generation" value="create-tables" />
</properties>
</persistence-unit>
</persistence>
我无法弄清楚问题数据正在获取和存储,但是没有更新并且没有持久存储在数据库中
I cant figure out the problem data is getting retreived and stored but it is not updating and not getting persisted in database
推荐答案
commit()
方法立即提交您的交易.因此,所做的更改已写入数据库&中.您之前打开的交易也将在此刻结束.之后再调用flush()
时,没有打开的事务,因此flush()
操作-遇到您的抱怨-使用javax.persistence.TransactionRequiredException
.另请参见 javax.persistence接口EntityManager#flush()
The commit()
method is committing your transaction immediately. Therefore, the changes are writting into the database & your previously open transaction also ends in this moment. When you call flush()
afterwards, there is no open transaction so the flush()
operation - complains as you experience it - with a javax.persistence.TransactionRequiredException
. See also javax.persistenceInterface EntityManager#flush()
将持久性上下文同步到基础数据库.
Synchronize the persistence context to the underlying database.
投掷: TransactionRequiredException-如果没有交易
Throws: TransactionRequiredException - if there is no transaction
您应该在同步EntityManager
的状态后调用commit()
方法 ,就像这样
You should call the commit()
method after you synced the state of your EntityManager
, like so
manager.getTransaction.begin();
// ...
// do something with your entities in between
// ...
manager.persist(reg);
manager.flush(); // in your case: could also be skipped
// finally - if nothing fails - we are safe to commit
manager.getTransaction().commit();
另一个提示:
您应该避免将UI端代码(JSF ...)与后端代码混合使用.这通常被认为是意大利面条"反模式.
You should avoid to mix your UI-side code (JSF...) with Backend code. This is generally considered 'spaghetti' anti-pattern here.
此外,请勿每次调用该UI绑定方法(register(...)
)时都打开和关闭EntityManagerFactory
,因为这是性能的杀手-至少每当有人尝试注册时,它都会产生不必要的处理这里.至少,您应该创建一个EntityManagerFactory
(或:EntityManager
)的实例作为一个字段(通过DBService类),并重用该实例与应用程序后端进行通信.
Moreover, do not open and close EntityManagerFactory
every time (!) you call this UI-bound method (register(...)
), as this is a performance killer - at least it produces unnecessary processing every time somebody tries to register here. At least, you should create an instance of EntityManagerFactory
(or: EntityManager
) as a field (via a DBService class) and reuse this to communicate with the application backend.
这篇关于我的实体没有持久显示javax.persistence.TransactionRequiredException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!