2011-02-11 174 views
0

我們正試圖用JPA構建一個JSF應用程序。現在,我們要登錄的功能,但是,當我們運行GlassFish服務器上的應用程序,有例外:無法弄清楚如何解決javax.persistence.PersistenceException

javax.persistence.PersistenceException:否EntityManager的持久性提供者名爲siteMami

我們認爲,問題來自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(); 
    } 
} 
+0

如何在Android Studio中解決此問題/ – 2015-06-09 16:33:31

回答

4

它只是膨化以某種方式截圖:您的文件被命名爲
「persitence.xml」而不是
「persistence.xml」。

+0

是的。引導程序將尋找'persistence.xml'。嘗試重命名文件,如果你沒有發現的話。 – 2011-02-11 19:02:31

1

在我更改了persistence.xml文件名後,我在src中創建了一個META-INF的副本,現在它可以工作。我們現在有另一個例外,但我們會看到。謝謝您的回答。