实体管理器不持久化类实例



我有一个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.
    }
}

问题RegisterBeanregister()方法不会影响 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>

在Java EE 6中,为了创建事务,您需要EJB(容器管理事务)或自己管理事务边界(Bean Managed Transactions)。请参阅链接的教程。

最简单的方法是两者中的第一个:要实现它,只需在@Stateless EJB 中移动相关代码(确保您的持久性上下文没有扩展)

注册豆

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);
    }
}

我的服务

@Stateless
public class MyService {
    @PersistenceContext(unitName="user-unit") 
    private EntityManager em;
    public void create(User newUser) {
        em.persist(newUser);
    }
}

有用的链接

  • Java EE 教程 - 事务

您正在 JSF 管理的 Bean 中使用扩展的持久性上下文。这意味着您没有活动事务,并且所有 DML-都排队而不是执行。

如果与事务外部的扩展持久性上下文进行交互,则插入、更新和删除将排队,直到您访问事务中的持久性上下文。这意味着您调用的任何 persist()、merge() 或 remove() 方法实际上都不会导致 JDBC 执行,从而在手动调用 EntityManager.flush() 之前更新数据库。

相关内容

  • 没有找到相关文章

最新更新