• Home
  • About
Blue Orange Green Pink Purple

Learning Java tutorial

Posted in code. on Friday, November 27th, 2009 by Chris Hulbert
Nov 27

Hi,
I've been recently trying to learn a bit of java - specifically web app development using struts 2 and hibernate. I'm up to version 3 of the tutorial so far.

Here's a screenshot of the resultant application:

Here's the project files to go with it:
javastrutshibernatetutorial

And here's the tutorial:

Java Struts Hibernate Tutorial

27 Comments

  1. Vo on February 25th, 2010

    very great tutorial, thanks so much…

    I hope u will have many usefull tutorials in the future…

    thanks again…

  2. inewie on March 31st, 2010

    I just don´t get it working with hibernate 3.5. Annotations are not recognized. I surely missing a jar for annotations. It is probably something stupid. Your tutorial is great, thanks

  3. Chris Hulbert on March 31st, 2010

    Inewie, don't give up, you'll figure it out. If you're stuck and need a hand, email me i'm glad to help.

  4. Deepjyoti on April 2nd, 2010

    great tutorial……….many thanks to you!!

  5. Alexander on April 6th, 2010

    Thank you for this!

  6. inewie on April 9th, 2010

    Hello,
    I get this error:
    HTTP Status 500 - Unable to show problem report: freemarker.template.TemplateModelException: Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on org.hibernate.exception.SQLGrammarException: could not execute query

    any ideas?

    This is the best tutorial I met.

  7. Sara on April 9th, 2010

    Does anyone habe the MySQL statements to complete the tutorial with this database server?
    Nice

  8. Chris Hulbert on April 9th, 2010

    Hi inewie,
    What are you doing for your database setup? Have you got a sql server, made your tables, etc?

  9. Chris Hulbert on April 9th, 2010

    Sara,
    No i do not have any details for how to get this to work with MySql. It'll be something along the lines of:
    * Get the MySql JDBC driver, put it's Jar into your lib folder,
    * Configure the hibernate xml to use the dialect: MySQLDialect
    * Configure the jdbc connection details for the database
    * Set up your mysql database and username/password
    That should do it.

  10. inewie on April 9th, 2010

    I am also using MySQL. Maybe, I just have a wrong table configuration or jar problem. I got it working at some stage, but now i have changed things and do not respond the same. Anyway, thank you very much for this tutorial, it took me forward …

  11. Sara on April 9th, 2010

    If someone have the equivalent table structure for MySQL, I would appreciate. I am beggining with this and do not know if I am doing it right at the database part.
    Great tutorial.

  12. Chris Hulbert on April 12th, 2010

    There seem to be a lot of people asking for how to make this work with MySql. Give me a few days and i'll update the tutorial to have a Mysql section for you all.
    I'll admit sql server and java isn't the most common pairing!

  13. Chris Hulbert on April 12th, 2010

    Okay i've updated the tutorial, there is now an index for how to connect it to MySql. The short version is:
    Download mysql-connector-java-X.zip from http://dev.mysql.com/downloads/connector/j/
    Extract mysql-connector-java-X-bin.jar, copy it to your app's lib folder
    Create an empty mysql database
    Edit your src\hibernate.cfg.xml and make the following changes:
    * The value of connection.driver_class is to be ‘com.mysql.jdbc.Driver’.
    * Value of connection.url is ‘jdbc:mysql://localhost/events’, assuming your database name is ‘events’ and your local machine is the mysql server.
    * Set the connection.username and connection.password appropriately.
    * Set the dialect to ‘org.hibernate.dialect.MySQLDialect’.

    Good luck!

  14. Varco on May 7th, 2010

    Hi Chris,

    You have a great tutorial. I worked it all the way till I started doing Hibernate.
    I can't seem to figure out this error. Same HTTP 500 error as inewie above and the description is:

    description The server encountered an internal error (Unable to show problem report: freemarker.template.TemplateModelException: Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on org.hibernate.exception.SQLGrammarException: could not execute query) that prevented it from fulfilling this request.

    Any help will be appreciated

  15. Varco on May 7th, 2010

    Chris, also code for Event.java and Person.java look very similar. Is that correct?

  16. Chris Hulbert on May 7th, 2010

    Yes, the event and person classes should look very similar, because they've both got id and name fields, and each of them has a collection of the other.

    As for your problem, please send me your console log and list of JARs in your lib folder.

  17. Chris Hulbert on May 7th, 2010

    Thanks for working through this with me Varco.
    I've updated the SQL in the project files to be correct, it was old.

  18. Antonio on May 20th, 2010

    Amazing tutorial, clear and woring. Very instructive. I also had problems with MySQL. The problem is ID field in Events must be auto-increment. That way Hibernate works well. Thanks a lot for this Tutorial!

  19. junjao on May 30th, 2010

    Easy to learn Tutorial. Thank you

  20. nish on July 11th, 2010

    Hey Chris,
    Great tutorial, here. As you, I also was trying to get full-hibernate-plugin to work, but session was always null. A day wasted there! Followed you tutorial, got yours to work. Now, creating my own from scratch.
    A question, how would I about validating in hibernate via annotations?

  21. Chris Hulbert on July 11th, 2010

    Hi nish,
    Yeah, i was disappointed in the full-hib-plugin, it simply doesn't work. It's a shame that you wasted your time on it.
    As for validation, i prefer to do mine manually (eg with javascript and code in the controller) but you can also use the hibernate annotations, i just don't know how.

  22. nish on July 12th, 2010

    Oh, i see. I have got mine to work now. Thanks to you.

    You say, you do your validations using javascript, are you using struts2-jquery plugin? The reason I ask is, I want to have client side validation(Without AJAX) for format. Such as, username and password cannot be empty. I wanted to use jqueryui, to display a modal if it is invalid. Do you know if that is achievable using struts2?

  23. Chris Hulbert on July 12th, 2010

    Nish: I haven't used that plugin. After all, javascript is just client-side, so i'd just put it in the view and be done with it. Having said that, if struts is generating your
    's, then maybe you need to do some trickery to get the correct id's and name's, but that shouldn't be a big issue. But then again, maybe the plugin makes things easier, so it may be worth looking at. I guess my point is, you don't always need to use plugins.

  24. Alkeshtech on August 4th, 2010

    Chirs,
    It's so nice of you to put this tutorial up. Very helpful for starters. With this setup, how are rollbacks setup? Do we have to obtain a transaction from session and rollback when an exception is thrown or does spring does this for us?

    Thanks.

  25. Chris Hulbert on August 5th, 2010

    Alkeshtech, I remember testing this once: I made a database change then threw an exception in the same action and it indeed seemed to roll it back. However you may want to ask on stackoverflow.com to find out more about how this works.
    Thanks for the kind words i hope this tutorial helps.

  26. Alkeshtech on August 8th, 2010

    Chris, I wanted to prepare queries using hibernate. But I am having problem doing so. I was wondering if you know how to. Inside Services class, I tried this: Query query = sess().createQuery("….."), but null exception is thrown as sess() is not injected. Do you know a way aroud this?

  27. Alkeshtech on August 8th, 2010

    Got it to work:
    http://stackoverflow.com/questions/3432136/hibernate-using-query-via-spring-injection



Leave a Reply

Chris' Babble

  • About
    Hi, i'm Chris Hulbert, a software guy from Sydney Australia.
  • Categories
    • code
    • Portfolio
    • Uncategorized
  • Recent Articles
    • How to implement DES and Triple DES from scratch
    • How to use Cookies in Struts 2 with ServletRequest and ServletResponse
    • How to use sessions with Struts 2
    • Using Quartz Scheduler in a Java web app (servlet)
    • Javascript date picker that Doesn't Suck!(tm)
    • Using Oracle XE with Hibernate


  • Home
  • About

© Copyright Chris' Babble. All rights reserved.
Designed by FTL Wordpress Themes brought to you by Smashing Magazine

Back to Top