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

问题描述 投票:0回答:1

我正在使用 hibernate,我不想将每个实体类添加到 hibernate.cfg.xml 中,而是让 hibernate 自动检测我的类。

我读到我可以使用两个属性

hibernate.archive.autodetection
hibernate.packageToScan
,但这不起作用。相反,我收到此错误:
Unable to locate persister: ina.project.backend.entities.UserEntity

你知道我做错了什么以及如何解决吗? 提前致谢! 下面你会发现我的

pom.xml
hibernate.cfg.xml
(旧版本和新版本),例如
UserEntity.java
(导致问题的实体)。

如有需要,我很乐意提供更多信息!

附注一切都与旧的兼容

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>
hibernate hibernate-mapping
1个回答
0
投票

packageToScan
是Spring的一个特性,但不是Hibernate的属性,这可以从你在
AvailableSettings
中找不到它的事实来证明,它定义了Hibernate支持的所有属性。

认为原生hibernate

hibernate.cfg.xml
不支持这种自动实体扫描,这就是为什么spring在与hibernate集成时添加了这种支持。

© www.soinside.com 2019 - 2024. All rights reserved.