Results 1 to 4 of 4
  1. #1
    series0 is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Talking Confusing statement behavior / lib problem?

    Hello.

    I am relatively new to java but a 25+ year C programmer.

    I am writing an application for a small group of users that links a MySQL database to java code using hibernate in the Eclipse IDE.

    I found some easy examples on most of this information and followed them as best as I could given the limited scope of the instructions.

    My Eclipse is Version: Helios Release
    Build id: 20100617-1415

    My MySQL is workbench revision 6485

    Hibernate is 3.3.2.GA

    Part 1 of my question:

    There may be an issue with the way I added the external hibernate jar/etc to my project. In Package Explorer (java perspective) the hibernate jar is listed as a referenced library. I went into that reference's properties and tried to point the Java Source Attachment, Javadoc Location, and Native Library properties to the right locations within the hibernate folder although I admit I have no clue what I am doing. I am relatively certain that the hibernate download contained no source code. Still I dont need to debug it theoretically. Is anything wrong with what I did to add the jar? Is there some better way to add a jar/modify my classpath in Eclipse?

    Part 2 of my question:

    The following snippet of code is the Event Handler for the save button press on the form. Pretty standard stuff. When I run the application in debug mode it gets to the buildSessionFactory statement within the try block. As far as I understand java's explanation of its rules for try/catch blocks the code has 2 choices at this point. Choice 1) The code succeeds and no throwable object is thrown in which case the next statement in the try block should execute. Choice 2) The code fails and a thowable object is thrown. The code resumes execution within the appropriate catch block. It is also my understanding that if I create a catch block with a completely unspecified Exception declaration, then ALL POSSIBLE exceptions will be caught by that catch block. What I see in the debugger is the code resumes execution in the finally block, skipping the catch block entirely and clearly taking a (theoretically impossible) Choice 3 route that gives me no information about what failed. What am I doing wrong?

    public static class btnSaveAction implements ActionListener{
    public void actionPerformed (ActionEvent e){
    System.out.println("Saving "+txtFirstName.getText());
    Session session = null;
    try{
    System.out.println("Here 1");
    // This step will read hibernate.cfg.xml
    SessionFactory sessionFactory = new
    Configuration().configure().buildSessionFactory();
    session = sessionFactory.openSession();
    // Create new instance of Database a record map and set
    // values in it by reading them from the form object.
    System.out.println("Inserting Record");

    CharacterSQL character = new CharacterSQL();
    character.setTitle(txtTitle.toString());
    character.setFirstname(txtFirstName.toString());

    session.save(character);
    System.out.println("Done");
    }catch(Exception ex){
    System.out.println(ex.getMessage());
    }finally{
    // Actual insertion/cleanup will happen now.
    System.out.println("finally ...");
    session.flush();
    session.close();
    }
    }
    }

    Thanks for any help anyone can give!

    Robert

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    The finally is always executed after the try..catch block.
    In your example, it is possible if the try block fails before it is able to set up the "session" object, such as a typo or misconfiguration in Hibernate's XML files, this should throw an exception, odd how catch is not printing that out.

    Maybe try in the catch:
    Java Code:
    catch (Exception ex) {
      System.out.println(ex.getMessage());
      ex.printStackTrace();
    }
    Though, in the finally block, the session reference would be null at the time and should fail with a null pointer exception. So instead,
    Java Code:
    finally {
      if (session != null) {
        session.flush();
        session.close();
      }
    }

  3. #3
    series0 is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default Still unsure but better

    Well I actually discoved that Exception is not a generic enough type to catch all Throwables. I had to use the type Throwable instead to get the catch all behavior I was after.

    Then I got some hideous document object model source code reference not found error.

    So now I am trying getting hibernate 3.5.6 which just was released. I will try to make sure the download contains all the source although I am a little peeved at downloader sets that don't include all the necessary stuff. I mean what the heck is the point?

    Yeah, i saw the fiddling with the session and realized the code example I copied wasn't the cleanest. Thanks for your help!

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    For managing jar files in the project class path with their sources, there is the "maven2" project, which has a nice eclipse plugin. They impose project structure and convention over creating build configuration and include a facility for dependency management. One of their features is you can set up a .pom file in your project to indicate it depends on the (in this case Hibernate) external API. It uses public maven2 repositories (where these projects like Hibernate) have published their artifacts into, to fetch the .jar files, their transitive dependent project jar files, and usually also their sources files. It helps to have it just go fetch all of these and set them up in the eclipse class path for you. Though getting your head wrapped around into maven is one of those "off the deep end" things to do. I am still getting into using it myself.

Similar Threads

  1. Problem with if statement - Char
    By KardKaper in forum New To Java
    Replies: 8
    Last Post: 12-22-2009, 01:14 PM
  2. Help please, stuck on for statement problem.
    By shroomiin in forum New To Java
    Replies: 3
    Last Post: 10-30-2009, 01:27 PM
  3. problem while using return statement
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 06:29 PM
  4. If-Else statement problem
    By MomenT in forum New To Java
    Replies: 9
    Last Post: 10-23-2008, 08:06 AM
  5. Please help me with this Problem Statement
    By KMS in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 08:44 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
  •