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

very great tutorial, thanks so much…
I hope u will have many usefull tutorials in the future…
thanks again…
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
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.
great tutorial……….many thanks to you!!
Thank you for this!
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.
Does anyone habe the MySQL statements to complete the tutorial with this database server?
Nice
Hi inewie,
What are you doing for your database setup? Have you got a sql server, made your tables, etc?
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.
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 …
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.
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!
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!
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
Chris, also code for Event.java and Person.java look very similar. Is that correct?
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.
Thanks for working through this with me Varco.
I've updated the SQL in the project files to be correct, it was old.
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!
Easy to learn Tutorial. Thank you
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?
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.
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?
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.
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.
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.
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?
Got it to work:
http://stackoverflow.com/questions/3432136/hibernate-using-query-via-spring-injection