Hibernate JPA-在Wildfly启动时未检测到实体

问题描述

im正在使用Java进行项目。我的想法是在React Fr-End端使用Java作为后端来喂数据库。到目前为止,我已经准备好了后端

环境: IntellJ,Wildfly21,GitHub

上的项目

启动wildfly并尝试保留实体时,在日志中总是看到 “原因:java.lang.IllegalArgumentException:未知实体:entities.Account”

这很有趣,因为如果我执行JUnit测试,则持久性可以完美地工作。 我可以通过在persistence.xml中添加类来“解决”该问题:

        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <persistence 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_2.xsd"
                 version="2.2">
        <persistence-unit name="cleverCore">
            <description>Database connector</description>
            <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>entities.Account</class>
        <class>entities.AccountRole</class>            
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
            <properties>
                <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
                <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/cleverCore" />
                <property name="javax.persistence.jdbc.user" value="cleverCoreManager" />
                <property name="javax.persistence.jdbc.password" value="password123" />
    
                <!-- Scan for annotated classes and Hibernate mapping XML files -->
                <property name="hibernate.allow_update_outside_transaction" value="true" />
                <property name="hibernate.connection.autocommit" value="true" />
                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
                <!-- Enable the logging of all the generated SQL statements to the console -->
                <property name="hibernate.show_sql" value="true" />
                <!-- validate,update,create,create-drop,non -->
                <property name="hibernate.hbm2ddl.auto" value="create" />
                <property name="hibernate.archive.autodetection" value="class" />
                <property name="hibernate.id.new_generator_mappings" value="false" />
    
            </properties>
        </persistence-unit>
    </persistence>

我还添加了自动检测功能,希望可以自动检测到它们,但事实并非如此。有什么办法可以在启动时检测到所有@Entity类?我很确定肯定有一个我看不到的微小设置。

package entities;

import helper.PasswordHelper;
import javax.persistence.*;
import javax.ws.rs.QueryParam;

import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "account")
public class Account {

    @Id
    @GeneratedValue
    @Column(name = "account_id",unique=true)
    private int id;

    @Column(nullable=false,unique=true)
    @QueryParam("accountName")
    private String accountName;

    @Column(nullable=false)
    @QueryParam("password")
    private String password;

    @ElementCollection
    @CollectionTable(name = "account_role")
    @Enumerated(EnumType.STRING)
    private Set<AccountRole> accountRoles = new HashSet<AccountRole>();

    public Account() {

    }

    public Account(String accountName,String password) {
        this.accountName = accountName;
        this.password = PasswordHelper.generatePassword(password);
    }

    public int getId() {
        return id;
    }

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

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public String getPassword() {
        return password;
    }

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

    public Set<AccountRole> getAccountRoles() {
        return accountRoles;
    }

    public void setAccountRoles(Set<AccountRole> accountRoles) {
        this.accountRoles = accountRoles;
    }

    public void addAccountRole(AccountRole role) {
        accountRoles.add(role);
    }

    public void removeAccountRole(AccountRole role) {
        accountRoles.remove(role);
    }

    public void encryptPassowrt(String unhashedPass) {
        this.password = PasswordHelper.generatePassword(unhashedPass);
    }

    public boolean isAuthorized(AccountRole role) {
        return accountRoles.contains(AccountRole.ADMIN)
                || accountRoles.contains(role);
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)