質問

I have a 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>

a User class:

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

}

and a RegisterBean class:

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.
    }
}

The problem: the register() method of the RegisterBean does not affect the MySQL table, i.e. the em.persist() does not throw any exception, but the entity does not go into my database. Can anyone help me?

EDIT: as reuquested
persistence.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>
役に立ちましたか?

解決

In Java EE 6, in order to create a transaction you need either an EJB (Container Managed Transactions) or manage the transaction boundaries yourself (Bean Managed Transactions). See the linked tutorial.

The easiest way is the first of the two: to implement it, simply move the relevant code in a @Stateless EJB (make sure your Persistence Context is not extended)

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

Useful Links

他のヒント

You are using an extended persistence context in a JSF managed bean. This means you have no active transaction and all your DML-s are queued instead of executed.

see

If you interact with an extended persistence context outside of a transaction, the inserts, updates, and deletes will be queued until you access the persistence context within a transaction. This means that any persist(), merge(), or remove() method you call will not actually result in a JDBC execution and thus an update of the database until you manually call EntityManager.flush().

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top