自动检测带注解的实体类时出现Hibernate错误

hpxqektj  于 2023-10-23  发布在  其他
关注(0)|答案(2)|浏览(176)

我正在使用hibernate,我不想将每个实体类都添加到hibernate.cfg.xml中,而是让hibernate自动检测我的类。
我读到我可以使用两个属性hibernate.archive.autodetectionhibernate.packageToScan,但这不起作用。我得到这个错误:Unable to locate persister: ina.project.backend.entities.UserEntity
你知道我做错了什么,怎么补救吗?提前感谢!下面你会发现我的pom.xmlhibernate.cfg.xml(旧版本和新版本),如UserEntity.java(导致问题的实体)。
如果有必要,我很乐意提供更多的信息!
P.S.一切都与旧的hibernate.cfg.xml工作
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>main</groupId>
    <artifactId>InternetAnwendungen-Project</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>
    <name>Internet Anwendung - Project</name>
    <url>https://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.16.1</version>
        </dependency>
        <dependency>
            <groupId>jakarta.platform</groupId>
            <artifactId>jakarta.jakartaee-web-api</artifactId>
            <version>9.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.2.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>7.0.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>3.0.8</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.github.cdimascio</groupId>
            <artifactId>dotenv-java</artifactId>
            <version>2.2.0</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>ROOT</finalName>
        <directory>./dev-ops/build</directory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
                <configuration>
                    <outputDirectory>./dev-ops/build</outputDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

UserEntity.java

package ina.project.backend.entities;

import jakarta.persistence.*;
import jakarta.validation.constraints.Size;

import java.io.Serializable;

@Entity
@Table(name = "User")
@NamedQuery(name = "user.byId", query = "SELECT u FROM UserEntity u  where u.id = ?1")
@NamedQuery(name = "user.byUsername", query = "SELECT u FROM UserEntity u  where u.username = ?1")
public class UserEntity implements Serializable {
    @Id
    @Column(name = "id", nullable = false, unique = true, updatable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "username", nullable = false, unique = true)
    @Size(min = 4, max = 100)
    private String username;

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

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

hibernate.cfg.xml(旧):

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <!-- Connection Properties -->
        <property name="connection.driver_class">org.mariadb.jdbc.Driver</property>

        <property name="connection.pool_size">10</property>
        <property name="dialect">org.hibernate.dialect.MariaDBDialect</property>
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">validate</property>
        <property name="current_session_context_class">thread</property>
        <mapping class="ina.project.backend.entities.UserEntity"/>
    </session-factory>

</hibernate-configuration>

hibernate.cfg.xml(新):

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <!-- Connection Properties -->
        <property name="connection.driver_class">org.mariadb.jdbc.Driver</property>

        <property name="connection.pool_size">10</property>
        <property name="dialect">org.hibernate.dialect.MariaDBDialect</property>
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">validate</property>
        <property name="current_session_context_class">thread</property>
        <property name="hibernate.archive.autodetection">class, hbm</property>
        <property name="hibernate.packageToScan">ina.project.backend.entities</property>
    </session-factory>

</hibernate-configuration>
ioekq8ef

ioekq8ef1#

packageToScan是一个spring特性,但不是一个Hibernate属性,这可以从你在AvailableSettings中找不到它的事实中得到证明,AvailableSettings定义了Hibernate支持的所有属性。
请记住,本机Hibernate hibernate.cfg.xml不支持这种自动实体扫描,这就是为什么Spring在提供与Hibernate集成时添加了这种支持。

vawmfj5a

vawmfj5a2#

也许你正在寻找的是来自fluent-hibernateEntityScanner,它将扫描包中的 @Entity 注解。
我过去曾使用它来扫描我的实体所在的模型包,例如:

List<Class<?>> classes = EntityScanner.scanPackages("com.myproject.model").result();
  for (Class<?> annotatedClass : classes) {
    metadataSources.addAnnotatedClass(annotatedClass);
  }

相关问题