为什么 hibernate.cfg.xml 中的映射属性被忽略?

时间:2021-05-05 10:22:01

标签: java xml postgresql hibernate maven

我试图按照官方网站上的教程在 hibernate 中通过 xml 找出类映射,但我不断收到相同的错误 - org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found, as a结果我无法获得 SessionFactory 和 Session。 我已经彻底检查了所有路径和项目结构,尝试使用 maven 和 gradle,但到目前为止没有任何帮助。我正在使用 postgres 数据库。这是我的项目结构:

enter image description here

这里是 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>org.example</groupId>
    <artifactId>dungeon-hero-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.31.Final</version>
        </dependency>
    </dependencies>

</project>

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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5432/dungeon</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">nbv67iuy</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.timeout">1800</property>
        <property name="hibernate.c3p0.max_statements">50</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.PostgreSQL82Dialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">none</property>

        <mapping resource="org/dungeonhero/entities/Monster.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

映射 xml:

<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="org.dungeonhero.entities">

    <class name="Monster" table="monsters">
        <id name="id" column="id">
            <generator class="increment"/>
        </id>
        <property name="name" column="name"/>
        <property name="maxHealth" column="max_health"/>
        <property name="currentHealth" column="current_health"/>
        <property name="dexterity" column="dexterity"/>
        <property name="dextCoeff" column="dext_coeff"/>
        <property name="currentReadiness" column="current_readiness"/>
        <property name="attack" column="attack"/>


    </class>

</hibernate-mapping>

主类:

package entities;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class Main {
    public static void main(String[] args) {
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
        SessionFactory sessionFactory = null;
        try {
            sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        } catch (Exception e) {
            System.err.println("=================================");
            StandardServiceRegistryBuilder.destroy(registry);
        }

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        session.save(new Monster("Goblin", 100, 40, 30));

        session.getTransaction().commit();
        session.close();
    }
}

实体类本身 (Monster) 为每个字段提供无参数构造函数以及 getter 和 setter。

此外,这里是堆栈跟踪:

org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : org/dungeonhero/entities/Monster.hbm.xml : origin(org/dungeonhero/entities/Monster.hbm.xml)
    at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56)
    at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:311)
    at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:474)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:85)
    at org.hibernate.boot.MetadataSources.buildMetadata(MetadataSources.java:202)
    at org.dungeonhero.entities.Main.main(Main.java:14)

也许我遗漏了什么,如果有任何提示,我将不胜感激!

1 个答案:

答案 0 :(得分:2)

我已删除有关配置的所有内容,因为项目结构已更改。 您应该更新您的 pom.xml 并为您的案例添加构建信息

<build>
    <resources>
       <resource>
          <filtering>false</filtering>
             <directory>src/main/java</directory>
             <includes>
                 <include>**/*.xml</include>
             </includes>
       </resource>
       <resource>
           <directory>src/main/resources</directory>
       </resource>
   </resources>
</build>