Slingshot Yourself Into Hibernate 3.5 and JPA 2.0 with this Speedy Tutorial


News: Slingshot Yourself Into Hibernate 3.5 and JPA 2.0 with this Speedy Tutorial

  1. Hibernate 3.5, the first version of Hibernate to fully support JPA 2.0, has been out for a few months now. So, we thought it was high time to deliver a few simple tutorials that show you how to configure a Hibernate 3.5 environment, and start doing some database persistence with Hibernate 3.5 and JPA 2.0 annotations.

    The first tutorial shows you how to get set up with all of the required libraries that are needed by Hibernate 3.5. That includes the standard ones that come with the distribution, along with the extra slf4j file you need to download as well. (People always seem to forget that one.) Once that is done, we configure out database (MySQL, although you can use your vendor of choice), and then use Hibernate to create the underlying database tables that are needed by out Java components.

    The second tutorial shows you how to do the basic CRUD, (create read update delete) operations. At the end of the tutorial, you've got a neat little class that can do queries, delete records, and save the state of your POJOs to the database.

    And of course, it's all Hibernate 3.5 and JPA 2.0 compliant stuff, so it's really worth taking a look at. And the tutorial is easy to follow. If you want to learn Hibernate 3.5 quickly, this is the right place to start.

    Hibernate 3.5 Tutorial Part I: Getting Started with Hibernate & JPA 2.0
    Hibernate 3.5 Tutorial Part II: CRUD Operations with Hibernate & JPA 2.0

    *****Some Other Hibernate Related Stuff You Might Like*****

    Video Tutorials on Learning Hibernate
    Hibernate Made Easy WebSite
    Recommended Books for Learning Hibernate
    My Similarly Coloured Book on The Simpsons

    Check out these updated Tutorials for Spring 3 as well.

    Threaded Messages (5)

  2. Well, I don't want to be rude with my title, but apart from the annotations on the GameSummary entity class (which are JPA 1.0 annotations btw), this tutorial is Hibernate specific, and cannot be used with another JPA provider (be it EclipseLink or OpenJPA).

    Maybe the hibernate.cfg.xml file is needed, but where is the persistence.xml file (in which you can configure your database access with non-vendor properties like javax.persistence.jdbc.url) ? You also use the Hibernate Session API to CRUD your object, and not the EntityManager API, and afaik nothing from the 2.0 release of the JPA specification.

    Cameron, don't get me wrong, this tutorial is great for someone who want to discover the Hibernate framework, but I think you shouldn't advertise with the JPA 2.0 keyword.

    Anyway, I really like and respect people who contribute to the community with those kind of tutorial (I'm an avid user of these) or with forum answers like you do on coderanch. Thanks for that.



  3. JPA ? huh?[ Go to top ]

    Strikes me that the majority of these tutorials could have been written as JPA and not Hibernate specific and then would be applicable to a far greater range of software (e.g EclipseLink, OpenJPA, and all of the other JPA implementations).


    Also seems to me that the menus on this site are becoming so specific to limited pieces of software (Hibernate, Spring) that the editors are pushing people towards thinking that those are the only pieces of software to use. Back in 2003 when I started reading this, it presented all software and allowed opinions to develop. Surely such a website should present a balanced view of the J2EE playing field and let users decide the direction they should go in.

    Discuss :-)

  4. Both Comments Are Correct[ Go to top ]

    So, about the JPA stuff.

    Indeed. this is done in Hibernate. The tutorials that will follow this in the next week or so will do the exact same persistence, but not use the Hibernate API at all. They'll be done simply using the JPA API and without any reference to the implementation provider. I thought that would be a nice little comparison. Many people have only used Hibernate. I think there will be a bit of an "a-ha' moment when people see how easy, and similar it is, to simply use the Java Persistence API. So, that's coming.

    The next tutorial deals with creating DAOs. So, it'll be DAOs, JPA without Hibernate, JPA without Hibernate with the same DAO, and finally, the exact same example except showing how to use Spring to inject the SessionFactory into the DAO, so it will be Spring with Hibernate and Spring with JPA, which is what everyone seems to be asking for.

    About the bend towards Spring and Hibernate. Yeah, there's a bit right now.

    I'm interested in building readership, and one thing is clear, many of the hits that come into this site are for older Spring articles. I mean ALOT. Something Rod Johnson wrote in 2005 sometimes gets twice as many hits as any other page on the site. The problem is, people read it and leave. If we can get those people to stay by reading an extra tutorial, and then maybe poking around the site a bit more, then hopefully we'll start winning over more readers. So, yeah, I have taken the last month to try and put some interesting and pertinent content together regarding the latest versions of Hibernate and Spring. Oh, and everything we do with Spring, I plan on demonstrating with Google Guice as well, which really seems to be popular.

    I haven't quite finished my third month as Editor in Chief, so many of my thoughts and ideas are just taking shape. But my vision was to do almost a theme each month at TSS. Every topic is welcome, and I'm always soliciting reports and tutorials, but I'd like to see a new topic as the focus each month, and during that month we put out some great content on that one topic.


    As you have noticed, Spring and Hibernate were the first topics I wanted to cover like that, as they are the most popular and I know they'll bring in traffic. I've got some great content for JSF 2.0 and Wicket lined up for next month. After that I'd like to time the release of Seam 3 with some articles on the same. Then a month just on managing application servers. 

    So, that's a bit of the plan. But by no means do we plan on becoming a Spring/Hibernate shop. It just happens that they're pretty popular, and I know them fairly well, and in my first little while here, it's been easier for me, with my background, to scope out tutorials on the subject.


  5. Just as an update to this thread, I added a new tutorial that goes through exactly the same coding steps here, except we use the Java Persistence API exclusively, without referencing any Hibernate APIs. It show a nice comparison between Hibernate vs. JPA, with a good little example that does all of the basic CRUD operations, and the required transaction stuff as well:

    We're always aiming to please here at

    -Cameron McKenzie

  6. A note and a request[ Go to top ]

    First, the request.  As long as you're talking about tutorials to put on TSS, I'd love to see a soup-to-nuts Spring Web Flow article.  A wizened old developer like me gets tired of having to learn everything on his own, and I've yet to see an article that didn't start near the middle of what you need to know in order to understand the art of implementing it.

    Now, the note.  I know Hibernate and JPA are all the rage, but after a good dozen projects and near a decade now of trying again and again to use it, I still find ORM to be nearly useless when your project gets any more complicated than a few persisted objects.  I've watched data mining go out the window, normalised schemas get demolished, and otherwise productive developers get hung by caching and stored proceedure problems and having to learn yet another SQL dialect to the point where I've avoided projects which use Hibernate.

    I'm not just summerily dismissing ORM - it's a great idea.  iBatis is the closest I've come to liking it, though.  My problem is that my experience with ORM always showed it to be more disruptive than helpful.  Spring DAO is what I've used for a long while now, and it's actually never failed me.  Yeah, it's a little more work in the beginning setting up your query and dao objects, but after the initial setup, I've nary had a "mysterious problem" arise, and any developer that knows SQL can easily dive into the persistance layer without the Cliffs of Insanity Hibernate learning curve.

    All that said - if ORM tools ever make that magical "jump" from detrimental fad to "can't work without it" - sorta like Maven did when version 2.0.9 came out, I'll be first in line.

    In the tradition of TSS, I'm really curious to hear other people's experiences...