【发布时间】:2014-05-13 06:08:45
【问题描述】:
我有一个faces-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<managed-bean>
<managed-bean-name>registerBean</managed-bean-name>
<managed-bean-class>com.beans.RegisterBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
User 类:
package model;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the users database table.
*
*/
@Entity
@Table(name="users")
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
private int id;
private String password;
private String userName;
public User() {
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
还有一个RegisterBean 类:
package com.beans;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.transaction.UserTransaction;
import model.User;
//see faces-config.xml to see annotations
public class RegisterBean
{
@PersistenceContext(unitName="user-unit", type=PersistenceContextType.EXTENDED)
private EntityManager em;
private String name;
private String password;
private String passwordRepeat;
public String getPasswordRepeat()
{
return passwordRepeat;
}
public void setPasswordRepeat(final String passwordRepeat)
{
this.passwordRepeat = passwordRepeat;
}
public String getName ()
{
return name;
}
public void setName (final String name)
{
this.name = name;
}
public String getPassword ()
{
return password;
}
public void setPassword (final String password)
{
this.password = password;
}
public void register()
{
//TODO: validate username and password
User newUser = new User();
newUser.setPassword(password);
newUser.setUserName(name);
//User foundUser = em.find(User.class, 22); //this actually finds the user with Id 22
em.persist(newUser); //TODO: find out why this doesn't work.
}
}
问题:RegisterBean的register()方法不影响MySQL表,即em.persist()不会抛出任何异常,但是实体没有进入我的数据库。谁能帮帮我?
编辑:根据要求
持久性.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="user-unit">
<jta-data-source>java:jboss/datasources/DefaultDS</jta-data-source>
</persistence-unit>
</persistence>
【问题讨论】:
-
它应该通过容器管理的持久性自动发生,但是您可以尝试通过调用
em.flush()来结束register吗? -
@mabi well 导致
javax.servlet.ServletException: javax.persistence.TransactionRequiredException: no transaction is in progress javax.faces.webapp.FacesServlet.service(FacesServlet.java:606) -
看,
persist()调用只添加到内部缓存。它不关心交易。 您使用容器管理的持久性还是自己管理事务? -
@mabi 从发布的代码中可以明显看出根本没有事务管理
标签: java mysql jsf javabeans entitymanager