Issue
I'm trying to figure out class mapping via xml in hibernate, following the tutorial from the official site, and I keep getting the same error - org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found, as a result i cannot get SessionFactory and Session. I have thoroughly checked all the paths and project structure, tried using both maven and gradle but nothing has helped so far. I'm using postgres DB. Here's my project structure:
Here's 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>
Mapping 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>
Main class:
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();
}
}
Entity class itself (Monster) provides both no-args constructor and getters and setters for each field.
Also, here's stack trace:
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)
Maybe I missed something, I will be grateful for any hint!
Solution
I have deleted everything about config, cause project structure was changed.
You should update your pom.xml
and add build info for your case
<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>
Answered By - Alex