File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Problem" Watch "Hibernate Problem" New topic
Author

Hibernate Problem

Vu Pham
Ranch Hand

Joined: May 28, 2005
Posts: 100
Hi all,
I have created a website using Hibernate . But when I ran it , it had this error :

java.lang.NoClassDefFoundError: net/sf/hibernate/cfg/Configuration

I've already copied all .jar files of Hibernate into WEB-INF/lib directory ( also including hibernate3.jar ) . I dont know what happens with it , please help me
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10288
    
168

Since you mention you are using Hibernate3, it looks like some of your application classes are still referring to the classes of Hibernate2. The packaging of Hibernate has changed in Hibernate3 and the classes are in the org.hibernate package instead of net.sf.hibernate. Check your application classes and update them appropriately.


[My Blog] [JavaRanch Journal]
Vu Pham
Ranch Hand

Joined: May 28, 2005
Posts: 100
Thanks very much . I have solved that problem like you said . But I have another problem

When I used hibernate.properties like following

hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql:///products
hibernate.connection.username root
hibernate.connection.password test

My website runs correctly . But when I replaced hibernate.properties with hibernate.cfg.xml like following :

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">test</property>
<property name="connection.url">
jdbc:mysql:///products
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
<mapping resource="CD.hbm.xml"/>
</session-factory>
</hibernate-configuration>


My website cannot run now , and it has the following error :

java.lang.UnsupportedOperationException: The user must supply a JDBC connection
at net.sf.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:32)
at net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:292)
at net.sf.hibernate.impl.SessionImpl.connect(SessionImpl.java:3373)
at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3333)
at net.sf.hibernate.impl.BatcherImpl.prepareQueryStatement(BatcherImpl.java:67)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:784)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1063)
at net.sf.hibernate.loader.Loader.list(Loader.java:1054)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1554)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1531)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1523)
...



I dont know what happens , please help me

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Versioning

You DTD points to Hibernate 2.0 and you have Hibernate 3.0 jar files?

They have to be the same

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

is what you have in you hibernate.cfg.xml

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Vu Pham
Ranch Hand

Joined: May 28, 2005
Posts: 100
No , I have replaced hibernate3.jar with hibernate2.jar
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10288
    
168

A bit of googling, led me to posts which mention the reason behind this exception being - Hibernate is not able to find the configuration file(hibernate.cfg.xml) in your classpath. Check the hibernate logs when hibernate is initializing(when SessionFactory is being created), they might provide some clues.
Vu Pham
Ranch Hand

Joined: May 28, 2005
Posts: 100
>>Check the hibernate logs when hibernate is initializing(when SessionFactory >>is being created), they might provide some clues.

Where can I check hibernate logs

>>Hibernate is not able to find the configuration file(hibernate.cfg.xml) in >>your classpath

How can I let Hibernate can find "hibernate.cfg.xml" file ?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Make sure the hibernate.cfg.xml file is in the root of your classpath.

So, you are definitely only using Hibernate 2.x for everything, the DTD and the jar files?

Mark
Vu Pham
Ranch Hand

Joined: May 28, 2005
Posts: 100
I put hibernate.cfg.xml in C:\Tomcat5.5\webapps\cdviewer\WEB-INF\classes folder .

I have already used only hibernate 2.0 . Here is my file :

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">test</property>
<property name="connection.url">
jdbc:mysql:///products
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
<mapping resource="CD.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Vu Pham
Ranch Hand

Joined: May 28, 2005
Posts: 100
Here is my project ( very small )

http://download.yousendit.com/4C6693392E27E585
Vu Pham
Ranch Hand

Joined: May 28, 2005
Posts: 100
I still have this problem . Please help me
Hendy Setyo Mulyo
Ranch Hand

Joined: Dec 01, 2004
Posts: 219

Hi

I'm suspecting your problem is due to incorrect property name for connection
Currently this is your session factory configuration:
=========================================================
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">test</property>
<property name="connection.url">
jdbc:mysql:///products
</property>
<property name="connection.driver_class">
=========================================================
I think you should change "connection.xxx" to "hibernate.connection.xxx"
So it will be like following:
=========================================================
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">test</property>
<property name="hibernate.connection.url">
jdbc:mysql:///products
</property>
<property name="hibernate.connection.driver_class">


Hendy Setyo Mulyo
SCJP 1.4 (95%), SCWCD 1.4 (94%)
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10288
    
168

I put hibernate.cfg.xml in C:\Tomcat5.5\webapps\cdviewer\WEB-INF\classes folder .


Try placing the hibernate.cfg.xml in C:\Tomcat5.5\webapps\cdviewer instead of WEB-INF/classes.

By the way, you had asked where to find the hibernate log files. Since you are using Tomcat, i guess you will find it in the log files that get generated in the logs folder of Tomcat(i dont remember the names of those log files). Try getting hold of those logs and post the contents here so that we can figure out what the problem is.


Here is my project ( very small )

http://download.yousendit.com/4C6693392E27E585


I'm sorry, i am out of office this entire week and dont have the necessary setup to try out this application. Hopefully, someone else here will be able to try it out.
Vu Pham
Ranch Hand

Joined: May 28, 2005
Posts: 100
Here is the content of log file .

log4j:WARN No appenders could be found for logger (org.apache.catalina.session.ManagerBase).
log4j:WARN Please initialize the log4j system properly.
java.lang.UnsupportedOperationException: The user must supply a JDBC connection
at net.sf.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:32)
at net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:292)
at net.sf.hibernate.impl.SessionImpl.connect(SessionImpl.java:3373)
at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3333)
at net.sf.hibernate.impl.BatcherImpl.prepareQueryStatement(BatcherImpl.java:67)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:784)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1063)
at net.sf.hibernate.loader.Loader.list(Loader.java:1054)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1554)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1531)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1523)
at CDViewer.displayAll(CDViewer.java:29)
at CDViewer.doGet(CDViewer.java:66)
at CDViewer.doPost(CDViewer.java:82)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)


Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10288
    
168

Can you please post the contents of CDViewer.displayAll() method. I would be interested in seeing how you are getting a session from the session factory. Are you passing your own connection object?

And one more question, does this work if you replace the xml file with the properties file?

Also i would be more interested in the logs related to session factory initialization. Try searching for something like "Environment" in your log file. These log statements correspond to hibernate initialization. If you find such statements please post them(there will be a bunch of such statements if i remember correctly).

I am sorry, but i am helpless this week since i have no setup with me to test your application - so far its been only wild guesses from my side.
[ October 26, 2006: Message edited by: Jaikiran Pai ]
Vu Pham
Ranch Hand

Joined: May 28, 2005
Posts: 100
Can you please post the contents of CDViewer.displayAll() method. I would be interested in seeing how you are getting a session from the session factory. Are you passing your own connection object?


CDViewer.java


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import java.util.*;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

public class CDViewer extends HttpServlet {
private SessionFactory sessionFactory;

public void init(ServletConfig config) throws ServletException {
super.init(config);

try {
sessionFactory = new Configuration().addClass(CD.class).buildSessionFactory();
} catch(Exception e) {
e.printStackTrace();
}
}

private void displayAll(PrintWriter out, Session session) {
List cds;
try {
out.println("<html>");
out.println("<table border='1'>");
out.println("<tr><td>Title</td><td>Artist</td><td>cost</td></tr>");
cds = session.find("from CD");

Iterator iter = cds.iterator();
while (iter.hasNext()) {
CD cd = (CD)iter.next();
out.println("<tr><td>");
out.println(cd.getTitle());
out.println("</td><td>");
out.println(cd.getArtist());
out.println("</td><td>");
out.println(cd.getCost());
out.println("</td></tr>");
}
} catch(Exception e)
{
e.printStackTrace();
}

out.println("</table>");
out.println("</html>");
out.println("hello");
}

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

response.setContentType("text/html");
PrintWriter out = response. getWriter();

Session session = null;

try {
session = sessionFactory.openSession();

String action = request.getParameter("submit");
if (action.equals("Pull All CDs")) {
displayAll(out, session);
} else {
out.println("Bad Input");
}

session.flush();
session.close();

} catch (Exception e) {
e.printStackTrace();
}
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}

And one more question, does this work if you replace the xml file with the properties file?


Yes , it does .



hibernate.cfg.xml


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">test</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/products
</property>
<property name="hibernate.connection.driver">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
<mapping resource="CD.hbm.xml"/>
</session-factory>
</hibernate-configuration>




hibernate.properties


hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql:///products
hibernate.connection.username root
hibernate.connection.password test
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10288
    
168



1) Your xml configuration looks wrong. The properties marked in bold are incorrect. They should be connection.username , connection.password and so on. Have a look at "Section 3.8. XML Configuration File" at:

Session Factory Configuration

for more details.

2) As per the doc mentioned above (and as Mark already suggested):

The XML configuration file is by default expected to be in the root o your CLASSPATH.


So where did you place this hibernate.cfg.xml file in your application? Remember, i had asked you to do the following, in one of my posts:

Originally posted by : Jaikiran Pai
Try placing the hibernate.cfg.xml in C:\Tomcat5.5\webapps\cdviewer


Did you try that?

And yes, i dont see any problem in your code - as far as this issue is concerned.

BTW, the next time you post some code or logs please remember to use the "Code" button to wrap the contents in a code block. That will provide a neat indentation and formatting which will make the contents easily readable.
[ October 27, 2006: Message edited by: Jaikiran Pai ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10288
    
168

Originally posted by Anh Vu:
Here is my project ( very small )

http://download.yousendit.com/4C6693392E27E585


Downloaded and tried out a few things in your application. It's working now. You will have to do the following to get it working:

1) Place hibernate.cfg.xml in:
C:\Tomcat5.5\webapps\cdviewer\WEB-INF\classes folder

2) Your hibernate.cfg.xml should contain the following:


3) Your code in CDViewer.java, currently is :



Change it to:




With these changes the application worked on my setup.
Vu Pham
Ranch Hand

Joined: May 28, 2005
Posts: 100
OK , It's ok now Thanks Jaikiran Pai very much Uhm , Would you like to explain me more why when we change



to



It will be ok ???
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
because the call to "configure()" will read your config file and then hibernate knows which datasource to use and so on...

pascal
Sudhakar Reddy Kurakula
Ranch Hand

Joined: Aug 19, 2006
Posts: 42
Hi,

sessionFactory = new Configuration().configure().buildSessionFactory();
This statement causes , hibernate.cfg.xml file is loaded.
So that it is available.
sessionFactory = new Configuration().addClass (cd.class).buildSessionFactory();
This statement causes, only hibernate.properties file is loaded.

Cheers
Sudhakar(SCJP1.4)
Satyam.
M.C.A(2006)
9989223696
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate Problem