Where to put hibernate config file - java

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.

Related

Exception when running JavaFX app using Hibernate on Android

I have a JavaFX desktop application. It accesses a remote MySQL database via Hibernate and LAN/WiFi.
I now try to port it with an as small as possible effort to Android 8.1 using Gluon mobile/javafxports.
When running the app on Android I get the following exception:
Could not parse configuration: commonhibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2105)
EDIT: Content of commonhibernate.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>
<mapping resource="Gender.hbm.xml"/>
<mapping resource="Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
On the (Windows-)PC the application runs well without this exception.
From other posts here on stackoverflow I understand that there is doubt that Hibernate can/should be used on Android.
But since these posts are 5 years old or older, I like to ask whether this is still the case and if someone can help regarding abbove exception on Android ?
Looks like you are missing the connection parameters.
Here example of configuration:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "classpath://org/hibernate/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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">URL_CONNECTION</property>
<property name="connection.username">YOUR_USERNAME</property>
<property name="connection.password">YOUR_PASSWORD</property>
<mapping resource="Gender.hbm.xml"/>
<mapping resource="Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>

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.

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 with JPA, connection time very slow

I have a project where I would like to use Hibernate for database access. In the application, the JPA api is used.
The persistence.xml file is this
<persistence
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="hibernate.test"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.ejb.cfgfile" value="/hibernate/hibernate.cfg.xml"/>
</properties>
</persistence-unit>
</persistence>
The hibernate.cfg.xml file is the following
<?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.OracleDriver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#//server:1521/DBNAME</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- <property name="hibernate.hbm2ddl.auto">verify</property> -->
<property name="hibernate.default_catalog">SYS_SOMETHING</property>
<property name="hibernate.default_schema">SYS_SOMETHING</property>
</session-factory>
</hibernate-configuration>
The problem is with this setup, that the entityManagerFactory = Persistence.createEntityManagerFactory("hibernate.test");
call takes about 25 seconds to complete.
If I move the configuration directly to the persistence.xml file, the connection completes in 1 second.
The error occurs with both Oracle and MySQL databases.
I can see the delay in the log file, nothing else is happening during this time
525 [pool-2-thread-1] INFO org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist
21668 [pool-2-thread-1] DEBUG org.hibernate.service.internal.JaxbProcessor - cfg.xml document did not define namespaces; wrapping in custom event reader to introduce namespace information
Complete log: http://pastebin.com/4NjPpFPe
This delay is only about 200ms if the cfg.xml is not used.
During this time, there process memory does not change, the cpu use is at 0% and there are 0 interactions according to sysinternals Process Monitor.
I would like to keep this setup because hibernate.cfg.xml is used for reverse engineering and hibernate tools also.
Tools used:
java8
hibernate-4.3.8
ojdbc7
jpa-2.1
Thank you for any suggestions in advance.
Update:
Found the solution, now I'm a happy man!
The hibernate framework (or the xml parser, I'm not sure) would wait on a http request.
To fix the problem, replace
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
with
<!DOCTYPE hibernate-configuration SYSTEM "-//Hibernate/Hibernate Configuration DTD 3.0//EN">
Found the solution, now I'm a happy man!
The hibernate framework (or the xml parser, I'm not sure) would wait on a http request.
To fix the problem, replace
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
with
<!DOCTYPE hibernate-configuration SYSTEM "hibernate-configuration-3.0.dtd">
Edit: Replaced the "with" line, this will use the correct dtd file in release mode and in reverse engineering too (it was not possible to use reveng without specifying the actual file).
This issue could be related to a property which regulates Hibernate's internal behavior to handle JDBC metadata on first attempt to create connection.
Try adding this property
"hibernate.temp.use_jdbc_metadata_defaults"
to your hibernate.cfg.xml. This helped me in a similar problem in a PostgreSQL environment to bring a "lame" startup back to "life". By default (i.e., not defined explicitly) this property is set to true which causes a full load of ALL metadata of your database via JDBC (which might obviously be slow in certain situations...).
The hibernate.cfg.xml file should then be used as follows
<?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.OracleDriver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#//server:1521/DBNAME</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- <property name="hibernate.hbm2ddl.auto">verify</property> -->
<property name="hibernate.default_catalog">SYS_SOMETHING</property>
<property name="hibernate.default_schema">SYS_SOMETHING</property>
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
</session-factory>
</hibernate-configuration>
Alternatively you can load the dtd directly from the JAR file:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"classpath://org/hibernate/hibernate-mapping-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

Categories