我有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.
}
}
问题:register()
的{{1}}方法不会影响MySQL表格,即RegisterBean
不会抛出任何异常,但实体不会进入我的数据库。任何人都可以帮助我吗?
编辑:如重新提出的那样 的persistence.xml:
em.persist()
答案 0 :(得分:1)
您正在JSF托管bean中使用扩展持久性上下文。这意味着您没有活动事务,并且所有DML都排队而不是执行。
如果与事务之外的扩展持久性上下文进行交互,则插入,更新和删除将排队,直到您访问事务中的持久性上下文。这意味着您调用的任何persist(),merge()或remove()方法实际上都不会导致JDBC执行,因此在您手动调用EntityManager.flush()之前会更新数据库。
答案 1 :(得分:1)
在Java EE 6中,为了创建事务,您需要EJB(容器管理事务)或自己管理事务边界(Bean管理事务)。请参阅链接的教程。
最简单的方法是两者中的第一个:实现它,只需在@Stateless
EJB中移动相关代码(确保不扩展持久化上下文)
<强> RegisterBean 强>
public class RegisterBean {
@EJB MyService service;
public void register() {
//TODO: validate username and password
User newUser = new User();
newUser.setPassword(password);
newUser.setUserName(name);
service.create(newUser);
}
}
<强>为MyService 强>
@Stateless
public class MyService {
@PersistenceContext(unitName="user-unit")
private EntityManager em;
public void create(User newUser) {
em.persist(newUser);
}
}
有用的链接