-
1 Attachment(s)
Hibernate
I need to use Hibernate for my connection to a database. I tried to use it with eclipse but I get some error messages that the import "org.hibernate" can't be resolved. I included the following jar's:
antlr-2.7.6.jar
cglib-2.2.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate3.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
Does anybody know what I need to do?
-
I guess you don't add this library in class-patch in Eclipse.
-
What do you mean about "build path"?
By the way in this case very useful to use maven. You just create projects for Hibernate and It downloads needs libraries itself.
-
Ok I fixed the problem with the jar-Files, but know I always get an error when I try to create a new session with the configuration xml-File. I'm not that into xml so I don't really know where my problem is...
Code:
package com.sample;
import java.util.List;
import java.util.Iterator;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.sample.HibernateUtil;
public class Test {
public static void main(String[] args) {
SessionFactory session = HibernateUtil.getSessionFactory();
}
}
Code:
package com.sample;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory = null;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial sessionFactory creation failed\n" + ex);
System.exit(1);
}
}
return sessionFactory;
}
}
I put the hibernate.cfg.xml file in the same folder where the .java files are
Here the code:
Code:
<?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>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory name="java:hibernate/SessionFactory">
<!-- properties -->
<property name="dialect">org.hibernate.dialect.DB2Dialect</property>
<property name="hibernate.connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
<property name="hibernate.connection.password">*****</property>
<property name="hibernate.connection.url">*****</property>
<property name="hibernate.connection.username">****</property>
</session-factory>
</hibernate-configuration>
But I always jump in the catch-clause in the HibernateUtil.java...
-
The hibernate.hbm.xml is located in root src or you must point full path where it is located.
src/hibernate.hbm.xml
src/com/sample/HibernateUtil.java
-
I'm sorry but where do I have to edit that?
-
There are around way either move your configuration in root folder src or point full path look like
Code:
SessionFactory sf = new Configuration().configure("./pachage/../catdb.cfg.xml").buildSessionFactory();
-
I tried both but neither worked, still get the error:
Code:
Initial sessionFactory creation failed
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
-
That is OK. you forgot to point logger in class-path. So I think it is bad idea to debug your code through this forum, I should read a book about Hibernate for example Hibernate in action. It is a very useful book.
I hope it helps.
-
I forgot to add the commons-logging-1.1.1.jar in the build path, did it know but still get the same error message, also tried to clean the project and to add the whole .jar Files again but did't work...
-
So what manual do you use when you use Hibernate?
-
-
Was able to fix the problem but now I got an other one:
Code:
Apr 18, 2011 12:57:45 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.1
Initial sessionFactory creation failed
java.lang.ExceptionInInitializerError
-
Is that the full stack trace?
Also, this trial and error is not really required since there are manuals that tell all the things you need to do get hibernate working.
-
Here the whole stack trace:
Code:
Apr 18, 2011 1:22:40 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.1
java.lang.ExceptionInInitializerError
at org.hibernate.cfg.Configuration.reset(Configuration.java:168)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:187)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:191)
at com.sample.HibernateUtil.getSessionFactory(HibernateUtil.java:13)
at com.sample.Test.main(Test.java:110)
Caused by: java.lang.NullPointerException
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:144)
at org.hibernate.cfg.Environment.<clinit>(Environment.java:515)
... 5 more
-
Can you show snippet a ConfigHelper.java:144?
So can you tell what aim you will get?
I mean it is commercial projects or you just education task.
I ask about it because you have bad approach in studying.
-
Your hibernate configuration file was not found. The hibernate tutorial at the hibernate says where you should put it.