BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Configuring Hibernate with Annotations

| by Scott Delap on Feb 10, 2007. Estimated reading time: 1 minute |
A new article on OnJava.com 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:

@NamedQueries(
{
@NamedQuery(
name="planeType.findById",
query="select p from PlaneType p left join fetch p.modelPlanes where id=:id"
),
@NamedQuery(
name="planeType.findAll",
query="select p from PlaneType p"
),
@NamedQuery(
name="planeType.delete",
query="delete from PlaneType where id=:id"
)
}
)

@Entity
@Table(name="T_MODEL_PLANE")
public class ModelPlane {

private Long id;
private String name;

@Id
@Column(name="PLANE_ID")
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@Column(name="PLANE_NAME")
public String getName() {
return name;
}

public void setName(String name) {
this.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.

Rate this Article

Adoption Stage
Style

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.

Tell us what you think

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

Email me replies to any of my messages in this thread
Community comments

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

Email me replies to any of my messages in this thread

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

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT