org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found - java

I have been trying my hand to implement hibernate using a small example.
Below is my hibernate.config.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="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/abc
</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<mapping
resource="HibernateExample/src/HibernateExposed/Resource/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
When running the code I am getting error org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found.
I tried replacing mapping as below
<mapping
resource="Resource/Person.hbm.xml"/>
and also tried to keep the mapping xml at same location as hibernate.config.xml.
<mapping
resource="Person.hbm.xml"/>
Under none of above mentioned cases, code can find my Person.hbm.xml.
My folder structure looks as below
I looked at all other answers for this error on Stackoverflow but none of the approaches resolved this issue. Any help is highly appreciated. Also, is there any approach to debug this further to granular level?

Please, put hibernate.cfg.xml in the root of src folder.
And use
<mapping resource="HibernateExposed/Person.hbm.xml"/>
Hibernate loads all those files using ClassLoader.getResourceAsStream(resourcePath) resourcePath — is the path to the file
ClassLoader tries to get access to the files in the root of bin or build folder in the IDE, or root of jar, or root of war/WEB-INF/classes/ for web-applications. Those all are the root of the class path.
bin is a folder where Eclipse compiles your files. The root of src folder is compiled to the root of bin folder. You can check it.
For an example
configure("hibernate.cfg.xml") — bin/hibernate.cfg.xml configure("xxx/hibernate.cfg.xml") — bin/xxx/hibernate.cfg.xml
<mapping resource="HibernateExposed/Person.hbm.xml"/>
corresponds bin/HibernateExposed/Person.hbm.xml
A path should be without the leading / for a ClassLoader. Hibernate tries to delete the leading /.
A path like this is valid too
<mapping resource="/HibernateExposed/Person.hbm.xml"/>
Update
You can specify path to the hibernate.cfg.xml, if you don't want to have it in the root
new Configuration().configure("HibernateExposed/hibernate.cfg.xml")
if you use
new Configuration().configure()
it should be in the root of the class path.

none of this worked for me, then I tried this:-
(replace this method in FactoryConfiguration singleton Class)
private FactoryConfiguration() {
Configuration configuration = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(***Class Name***.class);
sessionFactory = configuration.buildSessionFactory();
}

Related

Connecting to mysql database using IntelliJ [duplicate]

first of all I couldn't find an answer from related questions in this forum. I am new to Hibernate technology and I am trying to run a basic example from this page: http://www.roseindia.net/hibernate/hibernate4/firstHibernateApplication.shtml
Although, in eclipse it worked fine, I've got a problem with Intellij IDEA. Specifically, I have an error in the hibernate.cfg.xml file which says:
" 'com.mysql.jdbc.Driver' is not assignable to 'java.sql.Driver' ".
And when I run the program a message appears saying:
"ERROR: No suitable driver found for jdbc:mysql://127.0.0.1:3306/person Inserting Record Could not open connection"
('person' is my database) I have added the necessary jar files (or at least I think so) and generally I have followed all the steps from the webpage I pasted in the second line of this message. Please look at it and help me if you can. All the files and code I've created can be seen from the website I pasted. I've almost copied-pasted the example. Thank you.
UPDATED
hibernate.cfg.xml:
<?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>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/person
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"/>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
I' ve placed the connector jar file at the directory IdeaProjects\coreHibernateExample\lib. No, I am not running Tomcat, I guess I am running through Intellij. The import of the Mysql database has been made through Wampservers' phpmyadmin.
You have to make the JDBC library jar available to your project. Just adding jar to your project directory does not accomplish that.
Select jar in your project, right click and add as a library.

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 - MySQL - Connection to DB

I'm trying to connect to a MySQL database using Hibernate 3.6.10 with NetBeans 7.4, but when I try to create a new "Hibernate Mapping Wizard" I reply this error message:
Unable to connect: Cannot establish a connection jdbc:mysql://localhost:3306/mydbname using apache.org.derby.jdbc.ClientDriver(Unable to find a suitable driver)
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://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/database</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
</session-factory>
</hibernate-configuration>
I've found another similar problem here: Hibernate - Cannot connect to DB but I still not understand what's wrong.
Best regards.
Andrea
I have never used Hibernate Mapping Wizard but as far as I know this is useful to create a hibernate mapping file with an extension .hbm.xml: see this link as reference.
So the result is a .hbm.xml file that is the mapping of your class to your table.
The point is the in your hibernate.cfg.xml there is no explicit reference to this .hbm.xml file.
Try to add:
<mapping resource="<your_generated_file>.hbm.xml"/>
in your hibernate.cfg.xml and be sure to give all the data needed when using the Hibernate Mapping Wizard tool.
Hope this will be useful!
Ciao!
Nambari and Paolo,
I had to put my config.prop's and mysql driver's "Path" library into CLASSPATH variable. Now it works.
Thankyou all!
Ciao Paolo, grazie!

Netbeans 7.1 Hibernate Reverse Engineering wizard does not find database driver

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

Categories