无法弄清楚如何解决javax.persistence.PersistenceException

时间:2011-02-11 18:46:39

标签: java jsf jpa persistence.xml

我们正在尝试使用JPA构建JSF应用程序。目前,我们想要创建登录功能,但是当我们在glassfish服务器上运行应用程序时,有例外:

javax.persistence.PersistenceException:没有名为siteMami的EntityManager的持久性提供程序

我们认为问题出在persistence.xml的某个地方,也许是在提供商处,请帮助我们。谢谢!这是目录结构:

Here is the directory structure

的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    version="1.0">

    <persistence-unit name="siteMami" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>model.Admin</class>
        <class>model.User</class>
        <class>model.Client</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:mysql://localhost/siteMami" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="" />
        </properties>
    </persistence-unit>
</persistence>

User.java:

/**
 * 
 */
package model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "useri")
@Inheritance(strategy = InheritanceType.JOINED)
public class User implements Serializable
{
    @Transient
    private static long serialVersionUID    = 6837935606727700935L;

    @Id
    @GeneratedValue
    @Column(name = "idUseri")
    private long        id;

    @Column(unique = true)
    private String      username;
    private String      password;

    /**
     * @param id
     * @param userName
     * @param password
     */
    public User(long id, String username, String password)
    {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
    }

    /**
     * @return the id
     */
    public long getId()
    {
        return id;
    }

    /**
     * @return the userName
     */
    public String getUsername()
    {
        return username;
    }

    /**
     * @return the password
     */
    public String getPassword()
    {
        return password;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    public void setUsername(String userName)
    {
        this.username = userName;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }
}

UserManager.java:

package dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import model.User;

public class UserManager
{
    private EntityManagerFactory    factory;

    public UserManager()
    {
        factory = Persistence.createEntityManagerFactory("siteMami");
    }

    public User getUser(String username, String password)
    {
        EntityManager entityManager = factory.createEntityManager();

        EntityTransaction entityTransaction = entityManager.getTransaction();

        entityTransaction.begin();

        Query q = entityManager.createQuery("SELECT * FROM User WHERE User.username = '" + username + "' and User.password = '" + password + "'");

        entityTransaction.commit();

        return (User) q.getSingleResult();
    }
}

2 个答案:

答案 0 :(得分:4)

它只是以某种方式弹出屏幕截图:您的文件名为
“persistence.xml”而不是
“persistence.xml中”。

答案 1 :(得分:1)

在我更改了persistence.xml文件名后,我在src中制作了META-INF的副本,现在它可以工作了。我们现在有另一个例外,但我们会看到。谢谢你的回答。