Witam
Struktura projektu wygląda następująco src->main->java->Grywalizacja->resources->hbm
W paczce /resources znajduje się plik Hibernate.cfg.xml dalej w /hbm jest Event.hbm.xml
konfiguracja pliku Hibernate.cfg.xml wygląda tak:
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306</property>
<property name="connection.username">root</property>
<property name="connection.password">52Dembiec!</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</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">create</property>
** <mapping resource="hbm/Event.hbm.xml"/>**
<mapping class="Grywalizacja.EmployeersEntity"/>
</session-factory>
</hibernate-configuration>
Natomiast pliku Event
<?xml version="1.0"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
**<hibernate-mapping package="hbm">**
<class name="Grywalizacja.CharacterCard" table="employeers">
<id name="Login" column="Imie">
<generator class="increment"/>
</id>
<property name="points" type="timestamp" column="IloscPkt"/>
<property name="title"/>
</class>
</hibernate-mapping>
Oraz klasa main
package Grywalizacja;
import org.hibernate.*;
import org.hibernate.cfg.*;
class DatabaseApplication {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration()
.configure("/resources/hibernate.cfg.xml") // configures settings from hibernate.cfg.xml
.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
CharacterCard characterCard = (CharacterCard) session.load(CharacterCard.class, new Integer(1));
if(characterCard != null)
System.out.println(characterCard.getPoints());
session.close();
sessionFactory.close();
}
}
Dodając ścieżkę do pliku Event.hbm.xml InteliJ śpiewa mi, że "Cannot resolve file hbm/Event.hbm.xml" strukturę projektu tworzyłem niejako za pomocą InteliJ, to znaczy: po wpisaniu ścieżki klikałem kursorem na czerwony wykrzyknik, który IDE mi wyświetlał pozwalając, żeby sam tworzył sobie katalogi i w końcu plik.
Jeśli chodzi o linijkę w main
SessionFactory sessionFactory = new Configuration()
.configure("/resources/hibernate.cfg.xml") // configures settings from hibernate.cfg.xml
.buildSessionFactory();
Początkowo wyglądała ona tak:
SessionFactory sessionFactory = new Configuration()
.configure() // configures settings from hibernate.cfg.xml
.buildSessionFactory();
Dopiero w internecie znalazłem propozycję dodania ścieżki do pliku konfiguracyjnego.
Mimo wszystko nic nie działa, podczas próby kompilacji programu wyświetla mi błąd
"log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [/resources/hibernate.cfg.xml]
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
at org.hibernate.cfg.Configuration.configure(Configuration.java:258)
at Grywalizacja.DatabaseApplication.main(DatabaseApplication.java:12)"
Pomożecie?