O'Reilly logo

Harnessing Hibernate by James Elliott, Ryan Fowler, Timothy M. O'Brien

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Annotating Model Objects

Annotations can be applied to a variety of Java elements. When working with Hibernate, you’ll most often be concerned with annotating classes and their fields, to specify how a model object is to be mapped to a database schema. This is analogous to the way our XML mapping documents were structured around mapped classes and their properties. That's enough background and explanation—let’s dive right in and make this concrete. Time to map the classes from the examples (as fully developed in Chapter 6) using annotations!

How do I do that?

Example 7-5 shows one way you could annotate the Artist class. This chapter will explain the basics of Hibernate Annotations, but if you would like a more thorough description of some of these annotations, please refer to the Hibernate Annotations project web site at http://annotations.hibernate.org[5]. To save pages, the listings of annotated classes are compressed slightly from what you’ll find in the source download—whitespace is condensed and the JavaDoc is omitted. Since these are hand-written classes, not code-generated ones as they were in prior chapters, there is room for much more comprehensive JavaDoc, so it’s worthwhile to take a look at the downloadable versions, too.

Example 7-5. Annotating the Artist class

package com.oreilly.hh.data;

import java.util.*;
import javax.persistence.*; 1 import org.hibernate.annotations.Index; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required