java - 为什么我无法使用 JPA 连接数据库?

标签 java database jpa jakarta-ee data-persistence

用户实体:

package model.entity;
    
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)     
    private int eid;
   
    private String username;
    private String password;
    private String email;
    private String function;
    
    public UserEntity(int eid, String username, String password, String email, String function){
        this.eid = eid;
        this.username = username;
        this.password = password;
        this.email = email;
        this.function = function;
    }
    
    public UserEntity(){
        super();
    }

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFunction() {
        return function;
    }

    public void setFunction(String function) {
        this.function = function;
    }
    
    public int getEid() {
        return eid;
    }

    public void setEid(int eid) {
        this.eid = eid;
    }
  
}

和 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="FastFoodJSF" transaction-type="RESOURCE_LOCAL">
    
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>model.entity.UserEntity</class>
    
    <properties>
        
        <property name="javax.persistance.jdbc.url" value="jdbc:mysql://localhost:3306/fastfood" />
        <property name="javax.persistance.jdbc.user" value="fastfoodadmin" />
        <property name="javax.persistance.jdbc.password" value="fastfood" />
        <property name="javax.persistance.jdbc.driver" value="com.mysql.jdbc.Driver" />

    </properties>
    
    </persistence-unit>
</persistence>

这显示一个错误:

[EL Info]: 2017-04-05 12:17:38.926--ServerSession(2006034581)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
[EL Severe]: ejb: 2017-04-05 12:17:38.934--ServerSession(2006034581)--Exception [EclipseLink-4021] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [null], user [null] and URL [null].  Verify that you have set the expected driver class and URL.  Check your login, persistence.xml or sessions.xml resource.  The jdbc.driver property should be set to a class that is compatible with your database platform
Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-4021] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [null], user [null] and URL [null].  Verify that you have set the expected driver class and URL.  Check your login, persistence.xml or sessions.xml resource.  The jdbc.driver property should be set to a class that is compatible with your database platform
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:766)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:304)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:336)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:302)
    at model.service.FindUsers.main(FindUsers.java:13)
Caused by: Exception [EclipseLink-4021] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [null], user [null] and URL [null].  Verify that you have set the expected driver class and URL.  Check your login, persistence.xml or sessions.xml resource.  The jdbc.driver property should be set to a class that is compatible with your database platform
    at org.eclipse.persistence.exceptions.DatabaseException.unableToAcquireConnectionFromDriverException(DatabaseException.java:383)
    at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:91)
    at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:204)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:741)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:239)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:685)
    ... 5 more

在我看来,它无法连接到数据库,因为它看不到持久性的属性? 我是这项技术的新手,也许我不明白 JPA 应该如何工作。 谢谢:)

最佳答案

您可以尝试用 javax.persistence.jdbc.* 替换 javax.persistance.jdbc.* 吗?

关于java - 为什么我无法使用 JPA 连接数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43228517/

相关文章:

JavaFX 饼图 - 重叠标签和缺失标签

php - 将本地 mySQL 数据库从 Laravel/Homestead 移动到实时站点数据库

java - 插入到1 :m relation java spring

java - 使用 Java 和 Netbeans 从我的数据库中选择计数

java - 如何将常量传递给方法?

ios - 核心数据关系不返回任何值

sql - SSIS 将百分比转换为小数

java - Hibernate通过无状态 session 保存延迟获取的实体

java - 审计数据库回滚

java - 将从 HashMap 获取的 ArrayList 转换为 Array