Tagged: Hibernate

Add Hibernate 3.3.2 as a dependency in your Maven POM

Recently I had to update a Maven POM to get the latest release of Hibernate (3.3.2) and if you are in this boat as well, here is what you have to do:

1) Ensure that you add the JBoss repository to your <code>repositories</code> section as the latest releases are not in the standard maven repository.

<repositories>
....
<repository>
<!-- for hibernate -->
<id>jboss</id>
<url>http://repository.jboss.org/maven2</url>
</repository>
</repositories>

2) Add the latest version of hibernate to your <code>dependencies</code> section as such

<dependencies>
...
...

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
</dependencies>

3) Do an mvn clean then compile on your project and all the appropriate dependencies will be downloaded for use in your project.

Giddyup

AbstractTransactionalJUnit4SpringContextTests Failing to Rollback in MySQL?

Ok so today I was working on some JUnit tests within Spring using AbstractTransactionalJUnit4SpringContextTests (yes, wow what a long class name). For those of you unfamiliar with this class, basically if you extend your unit test class from this class, every @Test annotated test method will run within a transaction, with the default behavior being that a rollback is called after each @Test method’s execution (unless you declare so otherwise). Pretty convenient.

My simple test case was leveraging a DAO that derived from HibernateDaoSupport and the test inserted a few records. When the test was complete, another method annotated with @AfterTransaction would verify that the data did NOT exist in the table, which was expected due to the rollback that occurs after each test case…… well my asserts were failing because Hibernate created the MySQL tables using MyISAM, which does not support transactions. If you encounter this kind of issue, all you have to do is change your Hibernate dialect to use the MySQL5InnoDBDialect (for InnoDB storage which does transactions) within your session factory configuration as such:

...
<property name="hibernateProperties">
 <props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
...

Review: Java Persistence with Hibernate

javaperhiber This is a review of “Java Persistence with Hibernate” by Christian Bauer and Gavin King

This is the second Manning book on Hibernate to find a place on my bookshelf. The first one being the older 1st edition of “Hibernate in Action”, however this book is a revised edition of the 1st edition and takes the cake as it is one of the most comprehensive books to cover this standard in the Java world of ORMs. Having personally written an ORM from the ground up during a previous project I can appreciate Hibernate’s popularity and the work the developers put into the project.

Now on to the book. At roughly 800 pages and published in 2007, this book basically covers version 3.2 of Hibernate so at its core, it is still very relevant for the majority of the things you will be doing with the latest version available today.

This book progressively takes the reader from the basics of understanding object persistence and object mapping all the way through optimizing fetching strategies and advanced queries. The book covers every aspect of Hibernate in a guided approach, driven by clearly laid out examples targeted toward the specific feature/context at hand. The book is not a quick reference guide, but caters more towards the individual who wants to get a full grasp of the major features in an example driven approach. I have used this book many times before implementing a particular feature, as a way to get a good refresh of the principles behind a feature and how it works within the larger context of the Hibernate environment.

I would recommend this book for anyone who wants to learn about or uses Hibernate on a daily basis. Any beginner who is competent in Java and has worked with a minimum of JDBC before, should be able to pick this book up and understand Hibernate with ease. Likewise, for those who are intermediate to advanced with ORMs will also be able to learn quite a bit by having this book handy when you need to better understand one of the many, many features that Hibernate provides.

Negatives: The only negative about this book is that they dedicate an entire chapter (~80 pages) to JBoss Seam, which seemed like a plug.

Recommended: Yes

Skill Levels: Beginner to Advanced. Beginner’s can read cover to cover over a solid period of time to fully understand the product. Intermediate to advanced can use the detailed table of contents to jump to a particular subject quickly and read excellent coverage of the feature of interest.

Side Note: Why is it that Oreilly only has one Hibernate book out on the market? It would seem that they should have more and Manning has the corner on the Hibernate titles. P.S. The Oreilly book is pretty good, more recent, but not nearly the extensive coverage contained in this title.