Monday, 20 August 2012

Creating a SessionFactory in Hibernate 4

I'm working on the first major chapter of the Hibernate course for VirtualPairProgrammers and I'm working through the process of writing our first object to the database.

I've been using Hibernate for years, so I'm well used to the process of creating the SessionFactory, but in Hibernate 4 they've changed the process around - but the documentation on what to do is terrible. (Nb - this is at the time of writing, it may have improved by the time you read this).

Essentially, the buildSessionFactory() method on the Configuration class has been deprecated.

Instead, you have to pass in a "ServiceRegistry" into the buildSessionFactory method. The reasons for this aren't clear to me (I can't find any documentation explaining why, so if you know please tell me and I'll give you a credit on the course!)

After a few attempts, my code kept crashing with the following warning:

WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections

Which I think means "unable to get a connection"

The exception given was:

Exception in thread "main" org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set

This is a terrible error because it implies I haven't set the Hibernate Dialect (the dialect is essentially the name of the database vendor). Actually, Hibernate will automatically work out the database dialect, but it needs a connection to the database to do this. So the error thrown is a symptom of the problem, rather than the problem itself.

Anyway, I got it working in the end, here's the code I've used on the course:

private static SessionFactory sessionFactory;

public static SessionFactory getSessionFactory()
   if (sessionFactory == null)
      Configuration configuration = new Configuration();

      ServiceRegistry serviceRegistry = new

      sessionFactory = configuration.buildSessionFactory(serviceRegistry);
   return sessionFactory;

This will be in some kind of helper class.

I must admit that I've struggled to get this first chapter done as I've found it hard to pick my way through all the steps needed to get to your first running Hibernate application. Hopefully I'll speed up now we're up and running - progress will be reported on the blog.

Friday, 22 June 2012

My Next Course: Hibernate and JPA

This course has now been released (on Feb 14 2013) - full details here I've been working on projects for the last few months, so it's been a while since I created a new course.

I've agonised what to go for - there are so many vibrant and interesting topics I can cover, but I've decided to create a course covering both Hibernate and JPA in full detail.

We have already covered Hibernate in a little detail on our Spring Fundamentals course, and we do some JPA on the JavaEE course, but really we just provide overviews on those courses and we don't get to a full professional standard.

Hibernate and JPA need a course in their own right, and it's probably the most asked-for course from our customers, so I'm into the studio on Monday morning to start work.

As always, our video courses take a while to record and edit, we work hard to get them right, so it will be at least 2 months before we're ready to release. In the meantime, here's the draft running order - please contact me if there are additional topics that you want to see on the course!

  1. Getting Started
  2. Persisting objects
  3. Relationships in Hibernate/JPA
  4. Bidirectional Relationships
  5. Implementing equals() and hashCode() correctly
  6. Mapping Inheritance Hierarchies
  7. Embedding Objects
  8. Queries using HQL / JPQL
  9. Using the Criteria API
  10. Detached Objects and Merging
  11. Versioning Objects and Locking
  12. Understanding Lazy Initialization
  13. Hibernate/JPA in Web Applications (OpenSessionInView/OpenEntityManagerInView)
  14. Extended Sessions and EntityManagers
  15. Hibernate Caching (and the second level cache)
  16. Hibernate Validators
  17. Introducing Hibernate Search
  18. Auditing with Envers

Wednesday, 20 June 2012

Using Apache ActiveMQ in Glassfish

In our recent Java Messaging with JMS and MDB training course, we used Glassfish to send and receive messages using JMS.

The course covers the important topics of messaging, but one restriction was that the default message broker in Glassfish is lacking some of the features that many of the leading message brokers offer.

The big example is that in the chapter on MDB transactions, we have a message return to the queue if the MDB throws an exception. This allows us to "retry" an operation, say if an external system is unreliable.

That's great, and we get that working in the course, but the trouble is that the message immediately gets redelivered. So we end up retrying the operation straight away. What I really wanted was to delay the message redelivery by say an hour, so that the external system has time to recover.

Not possible in the default message broker in Glassfish. So it would be a good exercise to change the default broker. A good choice is Apache ActiveMQ, one of the most feature rich brokers in existence.

This excellent blog post from Sven Hafner describes how to do this - if you've followed our course and want to try this out, do let me know how you get on!

Wednesday, 22 February 2012

New Course - Messaging with JMS and MDB

One topic missing from last year's JavaEE course was JMS (the Java Messaging Service). The reason we omitted this was that we felt that JMS needs special treatment. It's quite a deep library, and a single chapter or two would not have been enough.

We discovered that we needed a full 10 chapters (and around 5 hours) to really cover JMS properly, so we've made it a full course in its own right - and it's finally released today!

It was hard work - and amazingly this course took longer to record than the full JavaEE course!

For full details, see the course page at the site. There is a full preview on the page, and if you just need a quick overview of JMS, the preview should be enough for you.

From today, the course will be available as part of a bundle along with the JavaEE course, and everyone who has already bought JavaEE will be contacted today with a course discount. If you've bought JavaEE and don't hear from us, send us a mail via the contact link on the site.

The topics covered are:

  • Introduction
  • Introducing Messaging
  • Receiving Messages
  • Tuning Message Sending
  • Transactions and Batching
  • Sending Messages on the Server
  • Message Driven Beans
  • Handling Problems
  • Message Redelivery
  • Course Summary

As always, full code is provided. Here's an extract from the first full chapter: