Java Hibernate invalid mapping exception - java

My code was working just fine with the xml mapping then I changed to annotations and i get this error:
Exception in thread "main" org.hibernate.InvalidMappingException: Unable to read XML
This is my 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>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate1?useLegacyDatetimeCode=false&serverTimezone=UTC</property>
<property name="connection.username">hibernate1</property>
<property name="connection.password">mypassword</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property>
<mapping class="hibernate.Profesor" file="" jar="" package="" resource=""/>
<mapping class="hibernate.Direccion" file="" jar="" package="" resource=""/>
</session-factory>
</hibernate-configuration>

I was able to get it to work for me changing only connection.url, connection.username, and connection.password with:
// A SessionFactory is set up once for an application
SessionFactory sessionFactory = new Configuration()
.configure() // configures settings from hibernate.cfg.xml
.buildSessionFactory();
Delete your mapping configuration files. That is, delete Profesor.hbm.xml and Direccion.hbm.xml if they still exist. You don't need them if you are using annotations.
If you are using Maven, make sure you have the following dependencies:
mysql-connector-java
hibernate-core
Also, com.mysql.jdbc.Driver is deprecated. Use com.mysql.cj.jdbc.Driver.
It would be more helpful if you posted the exception's entire stack trace.

Related

Where to put hibernate config file

This is my hibernate config file. I put it in src/main/resources and I put it in src .
But all the time I get this error:
Could not locate cfg.xml resource [hibernate.cfg.xml]
<?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.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
</session-factory>
</hibernate-configuration>
This is how I wrote my config file. I am using spring. I just found out that actually all this is not necessary in spring. You could just autowire SessionFactory.
You can place:
hibernate.cfg.xml file to
src/main/resources/hibernate.cfg.xml
or persistence.xml file to
src/main/resources/META-INF/persistence.xml
If you are using Hibernate's proprietary API, you'll need the hibernate.cfg.xml. If you are using JPA i.e. Hibernate EntityManager, you'll need the persistence.xml.

Cannot resolve class dialect

I am trying to fix an issue for hours but nothing seems to work.I tried everything available online. I am using Intellij IDEA. I am using a Maven project. I have created hibernate.cfg.xml with following content
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="User"/>
</session-factory>
</hibernate-configuration>
Following line is throwing an error that cannot resolve class dialect
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
My External Libraries structure looks like this.
any help would be appreciated.
I fixed the problem. The problem was Intellij IDEA was not able to download maven files for Hibernate. I used maven repository for adding the repository.

Hibernate Exception while deploying on VPS server

I am using VPS server first time to deploy my application. I have created a Hibernate Project. After Deploying it is giving me following error
org.hibernate.HibernateException: Could not parse configuration:
/hibernate.cfg.xml
org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2246)
org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:230)
org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:71)
org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:212)
org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:71)
org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:206)
util.HibernateUtil.(HibernateUtil.java:26)
services.HibernateServices.(HibernateServices.java:41)
servlet.GetShopProfile.processRequest(GetShopProfile.java:42)
servlet.GetShopProfile.doGet(GetShopProfile.java:77)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
To test if my database is working fine, I created a normal database connection class and it was working fine.
hibernate.cfg.xml
<?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.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.pool_size">1</property>
<property name="hibernate.current_session_context_class">thread</property>
<!--<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>-->
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<mapping class="pojo.TestClass"/>
</session-factory>
</hibernate-configuration>
Directory Structure
WEB-INF
classes
pojo
TestClass.class
services
HibernateService.class
servlet
TestServlet.class
util
HibernateUtil.class
hibernate.cfg.xml
We have Tomcat 7.0 installed.
Kindly help!!!
I solved the problem by reinstalling the tomcat on my server.
Any ways this issue can be solved by changing the DTD as suggested in questions: here
I tried multiple solutions in my case but at last when nothing worked I reinstalled the tomcat and every thing was fine
Thanks every one for their inputs and time.
Can you try adding space in your commented lines?
<!--<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>-->
Replace above line with
<!-- <property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property> -->
I think starting tag < after your comment start section is causing xml parsing issue.
Change the hibernate.cfg.xml as
<?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 Exception : Could not parse configuration: hibernate.cfg.xml

i am new to hibernate and tried to execute a simple hibernate jave code but unfortunately i am getting this exception. Somebody has written may be its due to the DOCTYPE , i have used in my configuration file.This is 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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/userdb</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>
<mapping resource="employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
and the exception is :
Exception in thread "main" 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 com.javatpoint.mypackage.StoreData.main(StoreData.java:13) Caused
by: org.dom4j.DocumentException: Connection refused: connect Nested
exception: Connection refused: connect at
org.dom4j.io.SAXReader.read(SAXReader.java:484) at
org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
... 2 more
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
Your DOCTYPE is using the new namespace (http://www.hibernate.org/dtd/) for Hibernate 3.6 , and you might have an older version of the Hibernate libraries in your classpath.
Replace it with
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
This one is a temporary solution. Your Hibernate jars contains dtd for validating your configuration xml. Extract ‘hibernate-configuration-3.0.dtd’ and put it in some directory in your project structure (in this case, I have put it in Project root directory). Add your dtd location to DOCTYPE declaration.
<!DOCTYPE hibernate-configuration SYSTEM
"hibernate-configuration-3.0.dtd">
It worked for me. It works when system is offline. Fetches DTD from your local system.
Its just that we have to figure a way to fetch dtd from your jar.
you may do it this way :
<!DOCTYPE hibernate-configuration SYSTEM
"classpath://org/hibernate/hibernate-configuration-3.0.dtd">
but then, it is throwing
Caused by: org.dom4j.DocumentException: unknown protocol: classpath Nested exception: unknown protocol: classpath

Hibernate Configration file error

I am new to hibernate framework. i made a sample project just to test hibernate. i set up the project. but when i run it i got this error
org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:972)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:69)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:966)
The Hibernate.cfg.xml file that i put in the main package of the project.
<?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.DerbyDialect
</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="connection.pool_size">2</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!--<property name="htm2ddl.auto">create</property>-->
<property name="hibernate.connection.driver_class">
org.apache.derby.jdbc.ClientDriver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:derby://localhost:1527/NetworkDB
</property>
<property name="hibernate.connection.username">
</property>
<property name="hibernate.connection.password">
</property>
<!-- List of XML mapping files -->
<!-- <mapping resource="Employee.hbm.xml"/>-->
</session-factory>
</hibernate-configuration>
I am using netbeans 7.0
As per DTD, <hibernate-configuration> should have a single <session-factory> but you have declared it twice.
Regarding second error:
org.hibernate.HibernateException: /hibernate.cfg.xml not found
Hibernate looks for the configuration file at the root of the classpath, so check if you have placed this file at root of your classpath.
So once you build your project the hibernate.cfg.xml file should be in classes directory.
Add the following in first line of your xml:
<?xml version="1.0" encoding="UTF-8"?>

Categories