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



im正在用Java进行一个项目。想法是使用一个react前端,我将使用java作为后端来提供数据库。到目前为止,我准备了后端

环境:IntellJ,Wildfly21,GitHub 项目

当wildfly启动并且我试图持久化一个实体时,在日志中我总是看到"引起原因:java.lang.IollegalArgumentException:未知实体:实体。帐户";

这很有趣,因为如果我执行一个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);
}
}

persistence.xml必须与要扫描的类在同一个jar/包/级别中,或者如果这些类在一个单独的jar中,则必须在class标记之前用jar-file标记列出它。如果您的persistence.xml与最终类路径中的类分离,那么您必须使用MappedSuperclassEmbeddableEntity注释列出每个类。

您可以检查为EntityManagerFactory扫描的类路径,这不会更改解决方案。

我有一个助手脚本可以做到这一点,所以我可以在测试中加载它们。

#!/bin/sh
grep -rEl '@(javax.persistence.)?(MappedSuperclass|Embeddable|Entity)' src/main/java/ | grep -oP '^[^.]+' | LC_ALL=C sort | sed -e 's#/#.#g' | sed -re 's#^src.main.java.(.+)$#    <class>1</class>#'

最新更新