Facilitating the spread of knowledge and innovation in professional software development



Choose your language

InfoQ Homepage News Configuring Hibernate with Annotations

Configuring Hibernate with Annotations

A new article on takes a look at configuring Hibernate via annotations. Traditionally developers have either configured Hibernate with XML files separate from Java classes or with XDoclet comments in the Java code with in turn generate XML. The article first introduces the concept of annotations mentioning that they support the new EJB3 persistence specification as well as providing a few Hibernate specific extensions. It then walks you through providing annotations to a basic class that a developer would want to persist via Hibernate:

query="select p from PlaneType p left join fetch p.modelPlanes where id=:id"
query="select p from PlaneType p"
query="delete from PlaneType where id=:id"

public class ModelPlane {

private Long id;
private String name;

public Long getId() {
return id;

public void setId(Long id) { = id;

public String getName() {
return name;

public void setName(String name) { = name;

@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
public PlaneType getPlaneType() {
return planeType;


Important elements such a primary key generate, many to one relationships, and named queries are also covered.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p


Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.