无法弄清楚如何解决 javax.persistence.PersistenceException
我们正在尝试使用 JPA 构建 JSF 应用程序。现在,我们想要实现登录功能,但是当我们在 glassfish 服务器上运行应用程序时,出现异常:
javax.persistence.PersistenceException: No Persistence provider for EntityManager name siteMami
我们认为问题出在 persistence.xml 中的某个地方,也许在提供商处,请帮助我们。谢谢!这是目录结构:
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();
}
}
We are trying to build a JSF application with JPA. For now, we want to make the login feature, but when we run the application on our glassfish server, there is the exception:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named siteMami
We think the problem is somewhere from persistence.xml, maybe at the provider, please help us. Thanks! 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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它只是以某种方式在屏幕截图上弹出:您的文件名为
“persitence.xml”而不是
“持久性.xml”。
It just popped out on the screenshot somehow: Your file is named
"persitence.xml" instead of
"persistence.xml".
在我更改 persistence.xml 文件名后,我在 src 中制作了 META-INF 的副本,现在它可以工作了。目前我们还有另一个例外,但我们拭目以待。谢谢您的回答。
After i changed the persistence.xml file name, i made a copy of META-INF in src and now it works. We have another exception for now, but we'll see. Thank you for your answer.