-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Problem running Hibernate and JPA sample code in Eclipse
PostPosted: Sat Jul 11, 2009 12:42 am 
Newbie

Joined: Sat Jul 11, 2009 12:31 am
Posts: 5
I am having difficulty running sample code from Hibernate Made Easy by Cameron McKenzie.

Tools / Libraries:

(1) Eclipse 3.5

(2) MySQL 5

(3) Hibernate 3.3.2.GA

(4) Hibernate Annotations 3.4.0.GA

Wrote a simple User class (located under /examscam/src):
Code:
package com.examscam.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.cfg.AnnotationConfiguration;

@Entity
public class User {

   private long id;
   private String password;

   @Id
   @GeneratedValue
   public long getId() {
      return id;
   }

   public void setId(long id) {
      this.id = id;
   }

   public String getPassword() {
      return password;
   }

   public void setPassword(String password) {
      this.password = password;
   }
}


Wrote a TestApp (containing main() method - located under /examscam/src):
Code:
package com.examscam.app;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import com.examscam.model.User;

public class TestApp {

   public static void main(String[] args) {
      AnnotationConfiguration config = new AnnotationConfiguration();
      config.addAnnotatedClass(User.class);
      config.configure();
      new SchemaExport(config).create(true, true);
   }
}


hibernate.cfg.xml (located under /examscam/src):
Code:
<?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>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/examscam</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
   </session-factory>
</hibernate-configuration>


Eclipse Build Path:

ant-contrib-1.0b2.jar, ant-junit-1.6.5.jar, antlr-2.7.6.jar, asm-attrs.jar, asm.jar, c3p0-0.9.0.jar, cglib-2.1.3.jar, commons-collections-2.1.1.jar, commons-collections-3.1.jar, commons-logging-1.0.4.jar, dom4j-1.6.1.jar, ejb3-persistence.jar, hibernate-annotations.jar, hibernate-commons-annotations.jar, hibernate-tools.jar, javassist-3.9.0.GA.jar, jta-1.1.jar, junit-3.8.1.jar, mysql-connector-java-5.1.7-bin.jar, slf4j-api-1.5.8.jar

Problem:

Eclipse states there's a problem starting in User.java, line number 12:

Code:
config.addAnnotatedClass(User.class);



"The type org.hibernate.MappingException cannot be resolved. It is indirectly referenced from required .class files"

"The project was not built since its build path is incomplete. Cannot find the class file for org.hibernate.MappingException. Fix the build path then try building this project examscam"


Question(s):

(1) What am I possibly doing wrong?

(2) Which jar file does org.hibernate.MappingException belong in?

(3) Am I placing the config file in the right place (right under /examscam/src)?


Top
 Profile  
 
 Post subject: Re: Problem running Hibernate and JPA sample code in Eclipse
PostPosted: Sat Jul 11, 2009 7:57 pm 
Newbie

Joined: Sat Jul 11, 2009 12:31 am
Posts: 5
Fixed this by placing the hibernate3.jar into Eclipse's build path...

However, when Eclipse runs the program, it states this in the console:

Code:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
   at org.slf4j.LoggerFactory.getSingleton(LoggerFactory.java:223)
   at org.slf4j.LoggerFactory.bind(LoggerFactory.java:120)
   at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:111)
   at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:269)
   at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
   at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:255)
   at org.hibernate.cfg.Configuration.&lt;clinit&gt;(Configuration.java:152)
   at com.examscam.app.TestApp.main(TestApp.java:11)
Caused by: java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder
   at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
   at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:254)
   at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:402)
   ... 8 more


I have the sl4j-api-1.5.8.jar in my Eclipse's build path... I wonder what I am doing wrong?


Top
 Profile  
 
 Post subject: Re: Problem running Hibernate and JPA sample code in Eclipse
PostPosted: Sat Jul 11, 2009 10:48 pm 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
Hi,

try to put slf4j-log4j12-1.x.x.jar in your classpath.


Top
 Profile  
 
 Post subject: Re: Problem running Hibernate and JPA sample code in Eclipse
PostPosted: Mon Jul 13, 2009 3:39 am 
Newbie

Joined: Sat Jul 11, 2009 12:31 am
Posts: 5
I did that and then I created a log4j.properties file:

Code:
### Direct log4j properties to STDOUT ###
lo4j.appender.stdout=org.apache.log4j.ConsoleAppender
lo4j.appender.stdout.Target=System.out
lo4j.appender.stdout.layout=org.apache.log4j.PatternLayout
lo4j.appender.stdout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### Set Log Levels - ###
log4j.rootLogger=warn, stdout log4j.logger.org.hibernate=info

### Log JDBC Bind Parameters ###
log4j.logger.org.hibernate.type=debug

### Log schema/export update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug


However, when I run TestApp.java, I get the following error message in Eclipse:

Code:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.


Don't understand why its so hard to configure all of these things just to run one simple app.

Any help would be most appreciated....

-James


Top
 Profile  
 
 Post subject: Re: Problem running Hibernate and JPA sample code in Eclipse
PostPosted: Sun Aug 30, 2009 11:30 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Just so you know, those are just warnings. They won't stand in the way of your code compiling and running.

I'm glad you got it working. I think the annotations 3.4 is just a version up from what the book uses, which causes the slf4j to be required as well.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: Problem running Hibernate and JPA sample code in Eclipse
PostPosted: Tue Oct 20, 2009 6:22 am 
Newbie

Joined: Tue Oct 20, 2009 6:20 am
Posts: 1
you have a typo in your log4j.properties file:
lo4j => log4j


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.