Here’s an example of a one-to-many relationship using Spring MVC and Hibernate with annotations. The Album class has many Photo s, and each Photo belongs to an Album.
/* * Album.java * * Created on November 22, 2007, 9:04 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */packagecom.recurser.gallery.model;importjava.util.ArrayList;importjava.util.Date;importjava.util.List;importjavax.persistence.OneToMany;importjavax.persistence.Temporal;importorg.apache.commons.lang.builder.EqualsBuilder;importorg.apache.commons.lang.builder.HashCodeBuilder;importorg.apache.commons.lang.builder.ToStringBuilder;importorg.apache.commons.lang.builder.ToStringStyle;importorg.appfuse.model.BaseObject;importjavax.persistence.Entity;importjavax.persistence.FetchType;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.GeneratedValue;importjavax.persistence.Column;importjavax.persistence.TemporalType;/** * * @author David Perrett */@EntitypublicclassAlbumextendsBaseObject{privateLongid;privateStringname;privatebooleanisPublic;privateDatedateCreated;privateList<Photo>photos=newArrayList<Photo>();/** * (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */@Overridepublicbooleanequals(finalObjectother){if(this==other)returntrue;AlbumcastOther=(Album)other;returnnewEqualsBuilder().append(id,castOther.id).append(name,castOther.name).append(isPublic,castOther.isPublic).isEquals();}/** * (non-Javadoc) * @see java.lang.Object#hashCode() */@OverridepublicinthashCode(){returnnewHashCodeBuilder().append(id).append(name).append(isPublic).toHashCode();}/** * (non-Javadoc) * @see java.lang.Object#toString() */@OverridepublicStringtoString(){returnnewToStringBuilder(this,ToStringStyle.DEFAULT_STYLE).append("id",id).append("name",name).append("isPublic",isPublic).toString();}@Id@GeneratedValue(strategy=GenerationType.AUTO)publicLonggetId(){returnid;}publicvoidsetId(Longid){this.id=id;}@Column(nullable=false,length=255)publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}@Column(name="is_public",nullable=false)publicbooleangetIsPublic(){returnisPublic;}publicvoidsetIsPublic(booleanisPublic){this.isPublic=isPublic;}@OneToMany(mappedBy="album",fetch=FetchType.LAZY)publicList<Photo>getPhotos(){returnphotos;}publicvoidaddPhoto(Photophoto){getPhotos().add(photo);}publicvoidsetPhotos(List<Photo>photos){this.photos=photos;}@Column(name="date_created")@Temporal(TemporalType.TIMESTAMP)publicDategetDateCreated(){returndateCreated;}publicvoidsetDateCreated(DatedateCreated){this.dateCreated=dateCreated;}}
/* * Photo.java * * Created on November 22, 2007, 9:04 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */packagecom.recurser.gallery.model;importjava.util.Date;importorg.apache.commons.lang.builder.EqualsBuilder;importorg.apache.commons.lang.builder.HashCodeBuilder;importorg.apache.commons.lang.builder.ToStringBuilder;importorg.apache.commons.lang.builder.ToStringStyle;importorg.appfuse.model.BaseObject;importjavax.persistence.Entity;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.GeneratedValue;importjavax.persistence.Column;importjavax.persistence.JoinColumn;importjavax.persistence.ManyToOne;importjavax.persistence.Temporal;importjavax.persistence.TemporalType;/** * * @author David Perrett */@EntitypublicclassPhotoextendsBaseObject{privateLongid;privateStringname;privateLongalbumId;privateAlbumalbum;privateDatedateCreated;/** * (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */@Overridepublicbooleanequals(finalObjectother){if(this==other)returntrue;PhotocastOther=(Photo)other;returnnewEqualsBuilder().append(id,castOther.id).append(name,castOther.name).isEquals();}/** * (non-Javadoc) * @see java.lang.Object#hashCode() */@OverridepublicinthashCode(){returnnewHashCodeBuilder().append(id).append(name).toHashCode();}/** * (non-Javadoc) * @see java.lang.Object#toString() */@OverridepublicStringtoString(){returnnewToStringBuilder(this,ToStringStyle.DEFAULT_STYLE).append("id",id).append("name",name).toString();}@Id@GeneratedValue(strategy=GenerationType.AUTO)publicLonggetId(){returnid;}publicvoidsetId(Longid){this.id=id;}@Column(nullable=false,length=255)publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}@ManyToOne@JoinColumn(name="album_id")publicAlbumgetAlbum(){returnalbum;}publicvoidsetAlbum(Albumalbum){this.album=album;}@Column(name="date_created")@Temporal(TemporalType.TIMESTAMP)publicDategetDateCreated(){returndateCreated;}publicvoidsetDateCreated(DatedateCreated){this.dateCreated=dateCreated;}}