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>
Related
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.
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.
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">
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">
I am currently trying to get through this netbeans + hibernate + JavaSE tutorial ( http://netbeans.org/kb/docs/java/hibernate-java-se.html). All is pretty fine, but after the creation of the hibernate.cfg.xml when it comes to the part where reverse engineering should be applied it comes to some weird message that the reverse-engineering wizard tells me:
"The database drivers are not added to the project classpath."
"Go to project properties to add database library.".
Well that's kind of weird, because the hibernate.cfg.xml was generated by netbeans. I've checked my database connection with the connection data from the hibernate.cfg.xml and it seems to be all okay, so connecting manually works pretty fine. Anyone knows what is going on here? Am I doing something wrong?
<?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/sakila</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">apassword</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
</session-factory>
</hibernate-configuration>
Add these line of code in hibernate.cfg.xml
<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/DATABASE_NAME</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"/>
</session-factory>
Besides missing jar files, here is another possible way that could cause this error in netbeans: wrong configuration file.
So make sure you have the correct configuration file (.cfg.xml file) to build .reveng
Attached an example configuration:
<?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.password">1234</property>
<property name="hibernate.connection.url">jdbc:mysql://161.58.103.144:3306/exampleDatabase?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">JasonGlez</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
Just change the ip, the name of the database, your username and password
You are missing the JDBC Driver. At the Project View right click the Libraries node under your project and choose Add Library... -> MySQL JDBC Driver.
As mentioned by hello, make sure the hibernate.cfg.xml is good. Try regenerating it though from Netbeans Hibernate Configuration Wizard as opposed to crafting it yourself. Also try recreating the project and make sure the new project wizard populates the hibernate.cfg.xml with database connection settings. It came out blank for me the first time; not sure why. Before it started working, I had also restarted Netbeans and the computer to boot, which might not hurt to try. (No pun intended.)
This required MySQL driver, Add My sql Driver in Library to resolve the issue