Whenever I'm disconnected from the internet, I get the following exception:
org.hibernate.HibernateException: Could not parse configuration: com/mashlife/resources/hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1542)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1035)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1017)
Caused by: org.dom4j.DocumentException: www.hibernate.org Nested exception: www.hibernate.org
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1532)
... 45 more
This only happens when I'm offline. Does hibernate try to read the DTD when parsing the config? What's the root cause here?
Here is my 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">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/foo</property>
<property name="connection.username">user</property>
<property name="connection.password">pass</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- DO NOT Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- Names the annotated entity class -->
<!--<mapping class="org.hibernate.tutorial.annotations.Event"/>-->
</session-factory>
</hibernate-configuration>
Hibernate can resolve the DTDs locally (without a network connection).
Your DOCTYPE is using the new namespace (http://www.hibernate.org/dtd/) for Hibernate 3.6, so you might have an older version of the Hibernate libraries in your classpath.
I experienced the same issue after upgrading to Hibernate 3.6.8.Final. I had multiple versions of hibernate3.jar on the classpath causing an old incompatible version of the DTD Entity Resolver to be loaded which only works with the old namespace (http://hibernate.sourceforge.net/). For reference, here's a link to the newer DTD Entity Resolver.
I'm using hibernate3-maven-plugin which has a transitive dependency on an older version of Hibernate so I just had to specify a plugin dependency on Hibernate 3.6.8.Final.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
...
</configuration>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.8.Final</version>
</dependency>
</dependencies>
</plugin>
It is not possible because, hibernate jar files is also load the some dtd content but slow internet connection it is worked.
(1) Hibernate Configuration File Location
The first solution was to provide the DTD file location in the system using classpath. So the DocType that worked offline would be;
<!DOCTYPE hibernate-configuration SYSTEM
"classpath://org/hibernate/hibernate-configuration-3.0.dtd">
(2)Use SourceForge DTD URL with SYSTEM
Another solution I found working is when I change the DTD URL to SourceForge and changed the declaration from PUBLIC to SYSTEM.
So below will also work if your system is offline.
<!DOCTYPE hibernate-configuration SYSTEM
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
Hibernate Work Offline
Another Way is You should download DTD file and set the file path.
and also set the dtd file location in java Build path (eclipse).
Hibernate-configuration
Orignal DTD
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
Corrected DTD
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://localhost:8080/YourProject/DTDLocation/hibernate-configuration-3.0.dtd">
Hibernate-mapping
Orignal DTD
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Corrected DTD
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://localhost:8080/YourProject/DTDLocation/hibernate-mapping-3.0.dtd">
in my situation:
JBoss AS 7
I check:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
and exclude dom4j in pom.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.9-SNAPSHOT</version>
<exclusions>
...........
<exclusion>
<artifactId>dom4j</artifactId>
<groupId>dom4j</groupId>
</exclusion>
</exclusions>
</dependency>
In case this helps anyone else ... my problem was that I was including the wrong Maven artifact. I was including spring-hibernate3:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-hibernate3</artifactId>
<version>2.0.8</version>
</dependency>
Replacing it with spring-orm fixed this issue:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>2.5.6.SEC03</version>
</dependency>
Just check this site https://forum.hibernate.org/viewtopic.php?f=1&t=943281&start=0
Hope that it will solve your problem.
I think you are using Hibernate3 jar file, but the Hibernate4 DTD file.
so the solution is choose one of them: 3 or 4.
BTW, I strongly recommend you using Maven for the jar dependency management.
You can use an internal DTD (not pretty IMO) or download the DTD file to your filesystem.
Check W3Schools' for more information: http://www.w3schools.com/dtd/dtd_intro.asp
In your mapping files, you must have the exactly SAME doctype as found in the mapping DTD's.
Then and only then you'll see that the dtd's found in hibernate3.jar can be found through the classpath, and running behind a firewall, stand-alone, etc. will be no problem at all.
No local dtd's in your projects to solve this issue, no code to intercept. :-)
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
The same is of course applicable for the configuration file.
I had this problem too.
My DOCTYPE was:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
It should be:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
Can you see the difference?
The first URI has NOT www and the second URI has www
So, the www in the URI must be declared in the configuration file and in all the mapping files.
It is not your case (cause I can see you have the http://www... URI), but it may help somebody.
Regards.
I have used following method to skip the validation of Doctype in the Configuration file
Code:-
public static Document parseConfiguration(String resourcePath) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
DocumentBuilder builder = factory.newDocumentBuilder();
File f=new File(resourcePath);
FileInputStream fs=new FileInputStream(f);
Document dtd = builder.parse(fs);
return dtd;
}
public static void main(String[] args)
{
Document dtd=null;
try {
dtd = parseConfiguration("src/hibernate.cfg.xml");
} catch (Exception e) {
e.printStackTrace();
}
SessionFactory factory=new AnnotationConfiguration()
.configure(dtd).buildSessionFactory();
/*
Now this code worked for me
You just have to use annotation instead of hbm.xml file because I was not able to skip the validation of mapping file as it is written inside the cfg.xml file
Reply if you got some other answer to run hibernate application in offline */
instead of
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
use
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
it worked fine for me
Alberto Daniel actually is right, indeed adding the "www." fixed for me the problem.
I guess, since the hibernate-core.jar file contains the dtd files, exactly the location http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd is treated somehow specially by the XML parser, so that the dtd from the jar file is used. I did not verify this, but it may be an explanation. However: Thank you Alberto!
Regards.
Related
I use the Hibernate Reverse Engineering through JBoss Tools > Hibernate Tools to generate Model Classes.
There, when I add the Hibernate Configuration and Run it / Rebuild It, it list all the DBs although I mention only one DB in the hibernate.cfg.xml's hibernate.connection.url.
eg : jdbc:mysql://localhost:3306/booksdb.
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"-//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.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/booksdb</property>
<property name="hibernate.connection.username">xxxx</property>
<property name="hibernate.connection.password">xxxx</property>
</session-factory>
</hibernate-configuration>
Then when I generate the entities code it scans all other Database tables also to generate the entity codes for all. Sometimes it impact by some duplicated tables in different DBs.
Question :
How to do this for single database mentioned in the hibernate.connection.url?
Hibernate Code Generation Configurations > Main
Hibernate Code Generation Configurations > Exporters
Hibernate version : 5.4
MySQL version Info
innodb_version:5.7.26
protocol_version:10
version:5.7.26-0ubuntu0.18.04.1
version_comment:(Ubuntu)
version_compile_machine:x86_64
version_compile_os:Linux
In this screen go to setup near reveng.xml. It will ask you to create a xml, from there you'll be able to exclude the databases you don't want.
<hibernate-reverse-engineering>
<table-filter match-name=".*" exclude="true" match-catalog="YOUR_DATABASE_NAME" />
</hibernate-reverse-engineering>
I´m trying to create a MySQL database table by using hibernate but I get this error message:
Exception in thread "main" org.hibernate.HibernateException: Error accessing stax stream
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:107)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
at org.hibernate.cfg.Configuration.configure(Configuration.java:259)
at com.anika.hibernate.Main.main(Main.java:18)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[2,34]
This answers do not solve my problem: Error connecting with database using hibernate
Exception in thread "main" org.hibernate.HibernateException: Error accessing stax stream
This is my Main.java file:
package com.anika.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Main {
public static void main(String[] args){
Student_Info student = new Student_Info();
student.setName("Anika");
student.setRollNo(1);
SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(student);
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}
My 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, 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>.
-->
<hibernate-configuration
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatetutorials</property>
<property name="connection.username">root</property>
<property name="connection.password"></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.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">create</property>
<mapping class="com.anika.hibernate.Stundent_Info"/>
</session-factory>
</hibernate-configuration>
Thank you for your help
I faced the same issue. It turns out that system is not able to access the hibernate-configuration-3.0.dtd from the url provided.
<DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
So, I referenced it from the local system.
<!DOCTYPE hibernate-configuration SYSTEM
"classpath://org/hibernate/hibernate-configuration-3.0.dtd">
Worked well for me. Hope it helps!
cleared the space at the begining of hibernate.cfg.xml file. lt worked
<?xml version="1.0" encoding="UTF-8"?>
You need to close the tag <hibernate-configuration.
<hibernate-configuration>
There is not the ! character in the DOCTYPE:
<DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
Should be
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
The issue for me was the lack of HTTPS.
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-config">
I changed to:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "https://www.hibernate.org/dtd/hibernate-config">
That fixed the issue.
My XML file
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.sourceforge.net/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:#192.168.252.128:1521:orcl</property>
<property name="connection.username">system</property>
<property name="connection.passowrd">manager</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.nttdata.domain.Employee"/>
</session-factory>
</hibernate-configuration>
Console after execution ::
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.HibernateException: problem parsing configuration/hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1222)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1161)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1147)
at com.nttdata.util.HibernateUtil.getSessionFactory(HibernateUtil.java:11)
at com.nttdata.dao.EmployeeDao.saveEmployee(EmployeeDao.java:13)
at com.nttdata.client.Driver.main(Driver.java:10)
Caused by: org.dom4j.DocumentException: www.hibernate.sourceforge.net Nested exception: www.hibernate.sourceforge.net
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1217)
... 5 more
What can be the error any help??
Try changing the DOCTYPE to this:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!DOCTYPE hibernate-configuration SYSTEM
"classpath://org/hibernate/hibernate-configuration-3.0.dtd">
This means that hibernate will load the DTD from classpath - it is usually included in hibernate jar in org/hibernate directory.
However, we use hibernate 3.5.6 - I don't hnow if this approach still works in the newer version - give it a try. The benefit of this is that you are completely independent on internet connection, proxies and so on.
Hibernate Configuration File Location
The first solution was to provide the DTD file location in the system using classpath. So the DocType that worked offline would be;
<!DOCTYPE hibernate-configuration SYSTEM
"classpath://org/hibernate/hibernate-configuration-3.0.dtd">
Use SourceForge DTD URL with SYSTEM
Another solution I found working is when I change the DTD URL to SourceForge and changed the declaration from PUBLIC to SYSTEM.
So below will also work if your system is offline.
<!DOCTYPE hibernate-configuration SYSTEM
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
I am new to Hibernate, so I have few dumb questions hope someone would be able to assist me. I have a query in regards to configuring hibernate file i.e. hibernate.cfg.xml file. Normally we configure it as below :
<?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="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:#localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">oracle</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Where the considers relevant mapping files. Lets say I have another mapping file, call it company.hbm.xml file and have a one-to-many relationship with employee.hbm.xml file. So, as and when the number of mapping files increases we include them into tag.
Query : How is it different to include multiple mapping files with individual tag from having just a single mapping file with all the relevant mappings.
Appreciate your response
Because such separation is cleaner. Single file with whole configuration tends to grow up and to become a yet another huge, unmaintainable and unreadable XML file.
Please also note, that using annotations right inside your model classes seems to be the right way for you (just as #Shiju Babu stated).
I am new to hibernate and i am having trouble with specifying the location of the mapping file in hibernate.cfg.xml file.
I have created an Event object in org.hibernate.tutorial.chapter1.domain.Event.java package with its corresponding Event.hbm.xml file in the same location.
I am trying to specify the location in the hibernate.cfg.xml mapping tag but I am getting an InvalidMappingException ().
I have added to the post: the exception, the mapping from the mapping file and the project file structure.
any advice would be great.
484 [main] ERROR
org.hibernate.util.xml.ErrorLogger -
Error parsing XML (1) : cvc-elt.1:
Cannot find the declaration of element
'hibernate-mapping'. 495 [main] ERROR
org.hibernate.util.xml.ErrorLogger -
Error parsing XML (2) : cvc-elt.1:
Cannot find the declaration of element
'hibernate-mapping'. Initial
SessionFactory creation
failed.org.hibernate.InvalidMappingException:
Unable to read XML
<!-- Names the annotated entity class -->
<mapping resource="org/hibernate/tutorial/chapter1/domain/Event.hbm.xml"/>
Make sure you have a DOCTYPE in your Event.hbm.xml at the top of the XML content such as:
<?xml version="1.0"?>
<!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.hibernate.tutorial.domain">
[...]
</hibernate-mapping>
My problem was that my XML file was missing :
<?xml version="1.0" encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
after inserting this to the beginning of the XML file everything turned out great.
Thanks!