Why wont hibernate config file parse - java

I am building my first hibernate application on eclipse and reads in a set of SFTP credentials from an Oracle table because I want to make them configurable and not hard code them. But when I attempt to run the application it will not parse the hibernate.cfg.xml file. I have tried typing in all the config file and not cut and paste and I have tried changing www.hibernate.org/dtd/hibernate-configuration-3.0.dtd in the config file to http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd. Why won't this work?
Here is the console output;
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: 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 name.of.package.used.retrieve.main(retrieve.java:14)
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
Here is the mapping file;
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping
3.0.dtd">
<hibernate-mapping>
<class name="name.of.package.used.SFTPDetails" table="SFTP_Creds">
<id column="SFTP_HOST" name="sftp_host">
<generator class="assigned" />
</id>
<property column="SFTP_PORT" generated="never" lazy="false"
name="sftp_port" />
<property column="SFTP_USERNAME" generated="never" lazy="false"
name="sftp_username" />
<property column="SFTP_PASSWORD" generated="never" lazy="false"
name="sftp_password" />
<property column="SFTP_HOSTKEY" generated="never" lazy="false"
name="sftp_hostkey" />
</class>
</hibernate-mapping>
This is the 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.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:#hostname:1523:TRNG</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<property
name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="sftpcreds.hbm.xml"/>
</session-factory>
</hibernate-configuration>
My main Java file that I am running the application is;
package name.of.package.used;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.List;
import org.apache.log4j.Logger;
public class retrieve {
public static void main(String args[]) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");// populates the data of the configuration file
// creating seession factory object
SessionFactory factory = cfg.buildSessionFactory();
// creating session object
Session session = factory.openSession();
// creating transaction object
Transaction t = session.beginTransaction();
Query query = session.createQuery("from SFTPCreds");
java.util.List list = query.list();
System.out.println(list);
t.commit();
session.close();
}
}

I am guessing it is your dtd reference: http://hibernate.sourceforge.net/hibernate-mapping
3.0.dtd
This is down and obsolete You should use ttp://www.jboss.org/dtd/hibernate/hibernate-configuration-3.0.dtd

This means the hibernate.dtd cannot be resolved - its resolution is attempted on the server. The dtd is contained in the jars files - see here and here for how to resolve it.
Also try changing the path from: hibernate.org/dtd to hibernate.sourceforge.net
it means that the XML parser is having a problem trying to access your DTD definition. So first thing to do is to see if you can browse the url for it directly and also check your proxy settings. In my case, I changed it
from
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
to
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
I think you can also try using "http://www.jboss.org/dtd/hibernate/hibernate-configuration-3.0.dtd". It seems like hibernate.org fwds to the jboss location anyways.

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.

Java Hibernate invalid mapping exception

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.

Unable to create a table and insert data inside the table using Hibernate 3.x framework

I am new to Hibernate and I am learning myself. I've got a problem when I am executing a program. I have tried a lot to solve the error but got no luck. Here is my main and configuration file and hbm files.
package com.HibernateLearn;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Client {
public static void main(String[] args) {
// creating configuration object
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");// populates the data of the
// configuration file
// creating seession factory object
SessionFactory factory = cfg.buildSessionFactory();
// creating session object
Session session = factory.openSession();
// creating transaction object
Transaction t = session.beginTransaction();
Employee e1 = new Employee();
e1.setId(103);
e1.setF_name("Ahammed");
e1.setL_name("Naseer");
//e1.getId();
//e1.getF_name();
e1.getL_name();
session.persist(e1);// persisting the object
t.commit();// transaction is committed
session.close();
System.out.println("successfully saved");
}
}
my 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="show_sql">true</property>
<!-- <property name="hbm2ddl.auto">create-update</property> -->
<!--Below are other values for hbm2ddl.auto validate: validate the schema,
makes no changes to the database. update: update the schema. create: creates
the schema, destroying previous data. create-drop: drop the schema at the
end of the session. -->
<!--property name="hibernate.cache.use_query_cache">true</property -->
<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/joctopusdb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">abc123</property>
<mapping resource="hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
my hibernate.hbm.xml file
<?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="com.HibernateLearn.Employee" table="emp_table">
<id name="Id">
<generator class="increment"></generator>
</id>
<property name="F_name"></property>
<property name="L_name"></property>
</class>
</hibernate-mapping>
Error I am getting
Exception in thread "main" org.hibernate.PersistentObjectException: detached entity passed to persist: com.HibernateLearn.Employee
You do e1.setId(103); while the unique ids for new entities are generated for you:
<id name="Id">
<generator class="increment"></generator>
</id>
Hibernate is expecting your Employee instance is already in the database because it has its id set. In other words, Hibernate is expecting it is updating an existing instance that is attached to the session. Hence the detached error message.
So to fix this you should not set the id for new entities yourself.

I am getting error in: Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml

I am getting this Exception
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.jwt.hibernate.SimpleTest.main(SimpleTest.java:13)
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:1481)
My hibernate.cfg file is
<?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">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3006/mydb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/jwt/hibernate/student.hbm.xml" />
</session-factory>
</hibernate-configuration>
My Mapping File is:
<?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">
<hibernate-mapping>
<class name="com.jwt.hibernate.Student" table="STUDENT">
<id column="ID" name="id" type="int" />
<property column="STUDENT_NAME" name="name" type="string" />
<property column="DEGREE" name="degree" type="string" />
<property column="ROLL" name="roll" type="string" />
<property column="PHONE" name="phone" type="string" />
</class>
</hibernate-mapping>
My Bean class is:
public class Student {
private int id;
private String name;
private String degree;
private String roll;
private String phone;
/** Getters and setters omitted **/
}
My Tester class is:
public class SimpleTest {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Student student = new Student();
student.setName("Gourab");
student.setRoll("101");
student.setPhone("8888");
student.setDegree("B.E");
Transaction tx = session.beginTransaction();
session.save(student);
System.out.println("Object saved successfully.....!!");
tx.commit();
session.close();
factory.close();
}
}
My Folder Structure is
I have added all required jars for connecting to hibernate and mysql(e.g hibernate-core 3.8.9.Final.jar,mysql-connector-java-5.1.12-bin.jar)
But still I am getting the error.
Please Help me out.
Thanks in advance
Full Stack Trace:
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.jwt.hibernate.SimpleTest.main(SimpleTest.java:12)
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:1481)
... 2 more
My jar files are
Could you try to check your .jar again? I try to execute your project SimpleTest.java with my .jar. It is work. Please take a look
I copied every file from your page as above and created a new project. Please follow the below structure to avoid the above exception.
public class App
{
public static void main( String[] args )
{
System.out.println("Maven + Hibernate + MySQL");
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Student st1 = new Student();
st1.setName("srinivas");
st1.setPhone("99");
st1.setRoll("123");
session.save(st1);
session.getTransaction().commit();
}
}
Hibernate can't parse your hibernate.cfg.xml.
hibernate-core-3.6.9.Final.jar has a file with DTD hibernate-core-3.6.9.Final/org/hibernate/hibernate-configuration-3.0.dtd
Check this file inside hibernate-core-3.6.9.Final.jar.
You need to have the same DTD as hibernate-configuration-3.0.dtd has.
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
Looks like you already have it.
May be you have other hibernate-core jar in the class path with other 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

Categories