Results 1 to 17 of 17

Thread: Hibernate

  1. #1
    Freaky123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default 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?
    Attached Thumbnails Attached Thumbnails Hibernate-screen.jpg  

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    I guess you don't add this library in class-patch in Eclipse.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    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.
    Last edited by Petr; 04-17-2011 at 03:03 PM.
    Skype: petrarsentev
    http://TrackStudio.com

  4. #4
    Freaky123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    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...

    Java 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();
    	}
    }
    Java 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:
    Java 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...

  5. #5
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    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
    Skype: petrarsentev
    http://TrackStudio.com

  6. #6
    Freaky123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    I'm sorry but where do I have to edit that?

  7. #7
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    There are around way either move your configuration in root folder src or point full path look like
    Java Code:
    SessionFactory sf = new Configuration().configure("./pachage/../catdb.cfg.xml").buildSessionFactory();
    Skype: petrarsentev
    http://TrackStudio.com

  8. #8
    Freaky123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    I tried both but neither worked, still get the error:
    Java Code:
    Initial sessionFactory creation failed
    java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

  9. #9
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    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.
    Skype: petrarsentev
    http://TrackStudio.com

  10. #10
    Freaky123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    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...

  11. #11
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    So what manual do you use when you use Hibernate?
    Skype: petrarsentev
    http://TrackStudio.com

  12. #12
    Freaky123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Google....

  13. #13
    Freaky123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Was able to fix the problem but now I got an other one:
    Java 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

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  15. #15
    Freaky123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Here the whole stack trace:
    Java 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

  16. #16
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    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.
    Skype: petrarsentev
    http://TrackStudio.com

  17. #17
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Your hibernate configuration file was not found. The hibernate tutorial at the hibernate says where you should put it.

Similar Threads

  1. Should I be using hibernate?
    By sephiroth2212 in forum JDBC
    Replies: 4
    Last Post: 10-11-2013, 09:57 AM
  2. Hibernate Use
    By sireesha in forum JDBC
    Replies: 2
    Last Post: 05-21-2008, 04:05 AM
  3. Hibernate DAO
    By sundarjothi in forum Eclipse
    Replies: 0
    Last Post: 04-26-2008, 09:29 AM
  4. hibernate
    By vipinpvijayan in forum Web Frameworks
    Replies: 1
    Last Post: 04-21-2008, 06:41 PM
  5. Replies: 2
    Last Post: 06-30-2007, 04:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •