Need help to run a hibernate project - java

I am very new to hibernate, and I am struggling for hours to make a project run. I didn't write the code, I just want to start it. I don't even know from where to begin explainin. First I will post a picture with the structure of the project
I didn't import the project, I created a new one and copied all the code. I have included the jars for hibernate. I am using postgresql, the initial project was using sqlserver. I have modified the hibernate.cfg.xml and Book.hbm.xml files, I think I will post these files here, but I get a bunch of errors:
**strong text**Exception in thread "AWT-EventQueue-0" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
at service.BookService.<init>(BookService.java:16)
at controller.BookManager.<init>(BookManager.java:18)
at views.Library.<init>(Library.java:24)
at Start.createAndShowGUI(Start.java:15)
at Start$1.run(Start.java:29)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: org.dom4j.DocumentException: Connection reset Nested exception: Connection reset
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
... 21 more
Initial hibernate.cfg.xml file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost/lab1_aop
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root
</property>
<!-- List of XML mapping files -->
<mapping resource="Book.hbm.xml"/>
</session-factory>
</hibernate-configuration>
My hibernate.cfg.xml file:
<?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>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">passw</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5434/lab1_aop</property>
<mapping resource="Book.hbm.xml"/>
</session-factory>
</hibernate-configuration>
In the Book.hbm.xml file I didn't change anything:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.Book" table="books">
<meta attribute="class-description">
This class contains a book details.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="borrowed" column="borrowed" type="boolean"/>
</class>
</hibernate-mapping>
Can anyone help me please? I can provide more of the code if nedded.
Thank you in advance!

Try replacing:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
with
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
in hibernate.cfg.xml

Related

MappingException with hibernate.cfg.xml

I have a maven project that I am using with hibernate-core 4.2.3.Final. I have followed the advice provided here:
Where to place hibernate config?
Hibernate Tutorial - Where to put Mapping File?
and the config file in src/main/resources looks like this:
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/justHibernate</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">12345</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<mapping resource="com/mycompany/app/model/Movie.hbm.xml" />
</session-factory>
</hibernate-configuration>
while the mapping file is within the package com.mycompany.app.model alongside the Movie class:
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mycompany.app.model.Movie" table="Movies">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="title" column="title"/>
<property name="director" column="director"/>
<property name="synopsis" column="synopsis"/>
</class>
</hibernate-mapping>
Exception
The configuration cannot find the mapping file.
INFO: HHH000221: Reading mappings from resource: com/mycompany/app/model/Movie.hbm.xml
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com/mycompany/app/model/Movie.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:738)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2167)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2139)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2119)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2072)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1987)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1966)
at com.mycompany.app.MovieManager.initSessionFactory(MovieManager.java:22)
at com.mycompany.app.MovieManager.<init>(MovieManager.java:17)
at com.mycompany.app.App.main(App.java:27)
Place Movie.hbm.xml under src/main/resources and change path in your config file as follows,
<mapping resource="Movie.hbm.xml" />
Hope this helps.

Error: Could not find or load main class org.hibernate.dialect.DB2390Dialect

When I run my program I get this error:
Error: Could not find or load main class
org.hibernate.dialect.DB2390Dialect
I have checked all the jar files required are in place. Please help. I guess there is problem with the hibernate configuration file. Find the configuration file below:
<?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>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">batch</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:1521:xe</property>
<property name="hibernate.connection.username">batch</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">none</property>
<mapping resource="galaxe.model.Network" />
</session-factory>
</hibernate-configuration>
In configuration you need to mention for DB2390 as below
property name="hibernate.dialect" value="org.hibernate.dialect.DB2390Dialect"
if you use oracle, it will be different
property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect">

Hibernate invalid configuration mapping

Good Morning to everyone!
I´m trying to use Hibernate to read an MSAccess database in Java using Eclipse, but it gives me a MappingException
here is my hibernateaccess.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<session-factory>
<property name='connection.driver_class'>net.ucanaccess.jdbc.UcanaccessDriver</property>
<property name='connection.username'></property>
<property name='connection.password'></property>
<!-- JDBC connection pool (use the built-in) -->
<property name='connection.pool_size'>1000</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.max_size">600000</property>
<!-- SQL dialect -->
<property name='dialect'>dialect.MSAccessDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name='show_sql'>true</property>
<!-- Mapping files -->
<mapping class="TransporteAccess.hbm.xml" />
</session-factory>
</hibernate-mapping>
and the TransporteAccess.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.Transporte" table="Transportes">
<property name="transporte" column="TRANSPORTES" type="string"></property>
</class>
</hibernate-mapping>
Which I´m doing wrong???
Thanks a lot!
It is mandatory to have Primary Key in Hibernate, refer
Add Primary key as well,
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
apart from this the hibernateaccess.xml contains some irrelevant tags
Update top lines in hibernateaccess.xml with
<?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>
& change
<mapping class="TransporteAccess.hbm.xml" />
to
<mapping resource="TransporteAccess.hbm.xml" />

Invalid Mapping Exception in hibernate.hbm.xml

I am posting my hbm file with the cfg file along with my error log. I found my questions based on similar problems are tried out their solution too but still my error is not fading away.
log
Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Could not parse mapping document from resource net/viralpatel/hibernate/Employee.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
at net.viralpatel.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
at net.viralpatel.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:8)
at net.viralpatel.hibernate.Main.main(Main.java:13)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource net/viralpatel/hibernate/Employee.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1584)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1552)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1531)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1505)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
at net.viralpatel.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:14)
... 2 more
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:508)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
... 9 more
Caused by: org.dom4j.DocumentException: Error on line 9 of document : The element type "generator" must be terminated by the matching end-tag "</generator>". Nested exception: The element type "generator" must be terminated by the matching end-tag "</generator>".
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)
... 10 more
Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Employee" table="EMPLOYEE" >
<id name="employeeId" column="EMPLOYEE_ID">
<generator class="native">
</id>
<one-to-one name="employeeDetail" class="net.viralpatel.hibernate.EmployeeDetail"
cascade="save-update"></one-to-one>
<property name="firstname" column="firstname" />
<property name="lastname" column="lastname" />
<property name="birthDate" type="date" column="birth_date" />
<property name="cellphone" column="cell_phone" />
</class>
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">tcs#1234</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="net/viralpatel/hibernate/EmployeeDetail.hbm.xml"/>
<mapping resource="net/viralpatel/hibernate/Employee.hbm.xml"/>
</session-factory>
In your Employee.hbm.xml you're not closing the generator tag.
Replace
<generator class="native">
by this
<generator class="native"/>
By looking at stack trace, you have not end the generator tag properly.
Please change your employee.hbm.xml file to end the <generator> element properly.
For Ex: It should be <generator class="native"/>

Hibernate Reverse Engineering Create Set (Composite Key)

I'm having trouble getting Hibernate to correctly reverse engineer a MySQL database into a Java Class.
I have 3 tables:
departments (pk = dept_no)
employees (pk = emp_no)
dept_emp (composite pks dept_no and emp_no)
I am trying to modify the Hibernate Reverse Engineering file to force the DeptEmp Java class that is generated to link a single department to a set of employees within that department, but I keep getting the same error, even though I have declared a name attribute for the set:
org.hibernate.MappingException: Could not configure overrides from file: C:\lib\workspace\Hibernate Analysis 2\resources\hibernate.reveng.xml
Could not configure overrides from file: C:\lib\workspace\Hibernate Analysis 2\resources\hibernate.reveng.xml
org.hibernate.MappingException: Could not configure overrides from file: C:\lib\workspace\Hibernate Analysis 2\resources\hibernate.reveng.xml
Could not configure overrides from file: C:\lib\workspace\Hibernate Analysis 2\resources\hibernate.reveng.xml
org.hibernate.MappingException: invalid override definition
invalid override definition
org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 88; Attribute "name" must be declared for element type "set".
Attribute "name" must be declared for element type "set".
My Reverse Engineering File:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
<table-filter match-catalog="employees" match-name="departments" />
<table-filter match-catalog="employees" match-name="employees" />
<table-filter match-catalog="employees" match-name="dept_emp" />
<table name="dept_emp" class="com.DeptEmp">
<set name="employeesSet" table="employees" inverse="true" lazy="true" fetch="select">
<key>
<column name="dept_no" ></column>
</key>
<one-to-many class="com.Employees"></one-to-many>
</set>
</table>
</hibernate-reverse-engineering>
My Hibernate Config:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/employees</property>
<property name="hibernate.connection.username">****</property>
<property name="hibernate.connection.password">****</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.default_catalog">employees</property>
<property name="connection.pool_size">20</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
</session-factory>
</hibernate-configuration>

Categories