• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem configuring Hibernate and JPA in Eclipse

 
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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):


Wrote a TestApp (containing main() method - located under /examscam/src):


hibernate.cfg.xml (located under /examscam/src):


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:




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


  • What am I possibly doing wrong?
  • Which jar file does org.hibernate.MappingException belong in?
  • Am I placing the config file in the right place (right under /examscam/src)?
  •  
    author and cow tipper
    Posts: 5009
    1
    Hibernate Spring Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Do you have the actual hibernate3.jar file?

    On a vacation right now with limited net access. I'll get deeper into it if when I'm back on Tuesday.

    -Cameron McKenzie
     
    James Dekker
    Ranch Hand
    Posts: 231
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes, it was the hibernate3.jar which was missing...

    Thank you! But, however, when Eclipse runs the program, it states this in the console:



    I have the sl4j-api-1.5.8.jar in my Eclipse's build path... I wonder what I am doing wrong?
     
    Ranch Hand
    Posts: 86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi
    if you go to webpage , you can see you need to one of the jars having the missing class.
    I faced the same issue , i downloaded the jar slf4j-simple-1.5.8 and it worked for me afterwards
     
    James Dekker
    Ranch Hand
    Posts: 231
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Ravi,

    I got it working last night (sorry for not posting an update to this thread earlier)...

    The new problem I am having is that whereas this code does work and generate a table named User in the database, the logging isn't properly set up:



    I wrote this log4j.properties file and placed it under $PROJECT_DIR_ROOT and also put it under Eclipse's build path (and it still doesn't work):



    Any assistance is most appreciated...
     
    Bartender
    Posts: 4116
    72
    Mac TypeScript Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    James Dekker wrote:The new problem I am having is that whereas this code does work and generate a table named User in the database, the logging isn't properly set up:
    I wrote this log4j.properties file and placed it under $PROJECT_DIR_ROOT and also put it under Eclipse's build path (and it still doesn't work):



    It should be at the root of the classpath when the application is running so log4j can be initialized. Is it (don't know what you mean by $PROJECT_DIR_ROOT) ?
     
    James Dekker
    Ranch Hand
    Posts: 231
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It is at the root of my project directory (/devprojects/examscam/)... However, I am using Eclipse and am not transferring it to PROJECT_DIR_ROOT/classes (output dir) where the code actually resides when compiled using Ant or Maven.

    I even edited my CLASSPATH in .bash_profile to include this (in a separate folder):

    export CLASSPATH=.:/devtools/jarlib/log4j.properties

    And it still doesn't initialize the logging settings...



     
    Vijitha Kumara
    Bartender
    Posts: 4116
    72
    Mac TypeScript Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Which ever way you run the app, as I said before log4j.properties should be at the root of the classpath (you will have to copy the file in to the directory where you compile the other class files in to because that's where (probably) you set the classpath in order to run the application).
     
    James Dekker
    Ranch Hand
    Posts: 231
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Is there a way to have Eclipse move the log4j.properties file to classes dir (Output dir) when one compiles the source files?
     
    James Dekker
    Ranch Hand
    Posts: 231
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    When I manually placed the log4j.properties files under classes dir, it actually loaded but it stated this error:

    log4j.properties file:

    I'll try again:

  • Does anyone know how to make Eclipse manually place the log4j.properties file into the output dir's top level root folder without using a build script?
  • Is there a jar file that I am not using which is making the logging errors (located above) occur?
  •  
    Cameron Wallace McKenzie
    author and cow tipper
    Posts: 5009
    1
    Hibernate Spring Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This line:

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

    Should be broken into two lines:

    log4j.rootLogger=warn, stdout
    log4j.logger.org.hibernate=info


    That little pearl of wisdom is included in the "Hibernate Made Easy" book errata locate here:

    http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=26errorfreecodechallenge
    http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=26errorfreecodechallenge

    Here's the source code for the book Hibernate Made Easy too, which might help:

    http://www.scja.com/hiberbook_source_beta.zip

    -Cameron McKenzie
     
    James Dekker
    Ranch Hand
    Posts: 231
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Cameron,

    Thanks for the direct response! I didn't know that the Comments section under your Error Free Code Challenge page is where the errata exists...

    Followed your suggestion (by breaking it up into two lines) and now I have new log4 related errors:



    Either way, your book rocks! Since the pages are a smaller size, I've been able to read through most of it very quickly!

    Makes me want to buy your other books!

    -James
     
    Cameron Wallace McKenzie
    author and cow tipper
    Posts: 5009
    1
    Hibernate Spring Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    So, going back to my book, this was my little blurb on log4j:

    Setting up log4J

    You will also notice a little log4J error message in my output:

    log4j:WARN No appenders could be found for logger
    log4j:WARN Please initialize the log4j system properly.

    This error is due to the fact that Hibernate is using Log4J, and a properties file that Log4J uses to initialize itself cannot be found on the classpath. The Hibernate core download contains a sample log4J.properties file you can place on your classpath that will eliminate this error message and subsequently direct logging entries to the System.out log. Alternatively, you can create your own log4J.properties file with the following settings:

    ### direct log messages to stdout ###
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout= org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.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=info
    ### log schema export/update ###
    log4j.logger.org.hibernate.tool.hbm2ddl=debug

    Make sure the file is named log4j.properties, and is placed on the classpath of the JVM. Then you'll get all sorts of crazy log messages!




    But these messages are not normal:

    log4j:ERROR Could not find value for key log4j.appender.stdout
    log4j:ERROR Could not instantiate appender named "stdout".


    Do you have a comment, extra comment, or missing ### marks in your log4j property file?

    You know, you just might want to get the darn thing working first! Why not comment out each line in the log4j file and let Hibernate use its own defaults. You may get a warning message, but at least everything will run. Warnings are just that -warning:



    Let's get the Hibernate stuff working. I'd hate to see you fighting with log4j and not enjoying the real pleasure of Hibernate!

    By the way, I just got back from my short vacation, so I should be more accessible here for help and book related questions.

    -Cameron McKenzie
     
    James Dekker
    Ranch Hand
    Posts: 231
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Cameron,

    Its now working!

    If you scroll up (this thread) you'll notice that the first four lines were missing the "g" for log4j

    Before:


    After (fixed):


    I am almost done with the 7th chapter of your book - its not like I am stuck with the first chapter because of log4j config problems!

    Already have most of the code running...

    Its interesting that the the Session.load() and Session.get() methods automatically associate the instance with the session.

    Here's my fixed log4j.properties file:


    One last try, if anyone has read this thread, can they assist me in how to place the log4j.properties files automatically into PROJECT_DIR/classes using Eclipse (I've been manually moving it from the filesystem)?

    -James
     
    Cameron Wallace McKenzie
    author and cow tipper
    Posts: 5009
    1
    Hibernate Spring Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    into PROJECT_DIR/classes using Eclipse (



    Well, if it's in your source directory, it SHOULD be copied over automatically to the corresponding classes directory. Maybe if you do a build and clean?

    -Cameron McKenzie
     
    James Dekker
    Ranch Hand
    Posts: 231
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well, it was outside my <src> dir, but directly underneath my PROJECT_DIR_ROOT...

    e.g.

    /examscam
    |
    ------------- log4j.properties
    |
    -------------- /src
    |
    --------------/classes

    By moving it under the /src directory, it worked!

    I never knew about this because I always rely on an Ant build script which compiles, zips into a WAR file, and then deploys.

    Many thanks to you, now I have it just like I want it!
     
    Cameron Wallace McKenzie
    author and cow tipper
    Posts: 5009
    1
    Hibernate Spring Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    I never knew about this because I always rely on an Ant build script which compiles, zips into a WAR file, and then deploys.



    It's great when you're learning something like Hibernate, and as a side product, you get a deeper understanding of how peripheral technologies like ANT and Eclipse work. Good stuff!

    -Cameron McKenzie
     
    On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic