gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:37:20 25 4
gpt4 key购买 nike

我们正在尝试使用 JPA 构建 JSF 应用程序。现在,我们想要实现登录功能,但是当我们在 glassfish 服务器上运行应用程序时,出现异常:

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

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

Here is the directory structure

持久性.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>

用户.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();
}
}

最佳答案

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

关于java - 无法弄清楚如何解决 javax.persistence.PersistenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4972816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com