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.
*/
package com.recurser.gallery.model;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.appfuse.model.BaseObject;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Column;
import javax.persistence.TemporalType;
/**
*
* @author David Perrett
*/
@Entity
public class Album extends BaseObject {
private Long id;
private String name;
private boolean isPublic;
private Date dateCreated;
private List<Photo> photos = new ArrayList<Photo>();
/**
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(final Object other) {
if (this == other)
return true;
Album castOther = (Album) other;
return new EqualsBuilder()
.append(id, castOther.id).append(name, castOther.name).append(isPublic, castOther.isPublic)
.isEquals();
}
/**
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return new HashCodeBuilder()
.append(id).append(name).append(isPublic).toHashCode();
}
/**
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
.append("id", id).append("name", name).append("isPublic", isPublic)
.toString();
}
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(nullable=false,length=255)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="is_public",nullable=false)
public boolean getIsPublic() {
return isPublic;
}
public void setIsPublic(boolean isPublic) {
this.isPublic = isPublic;
}
@OneToMany(mappedBy="album",fetch=FetchType.LAZY)
public List<Photo> getPhotos() {
return photos;
}
public void addPhoto(Photo photo) {
getPhotos().add(photo);
}
public void setPhotos(List<Photo> photos) {
this.photos = photos;
}
@Column(name="date_created")
@Temporal(TemporalType.TIMESTAMP)
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
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.
*/
package com.recurser.gallery.model;
import java.util.Date;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.appfuse.model.BaseObject;
import javax.persistence.Entity;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
*
* @author David Perrett
*/
@Entity
public class Photo extends BaseObject {
private Long id;
private String name;
private Long albumId;
private Album album;
private Date dateCreated;
/**
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(final Object other) {
if (this == other)
return true;
Photo castOther = (Photo) other;
return new EqualsBuilder()
.append(id, castOther.id).append(name, castOther.name).isEquals();
}
/**
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return new HashCodeBuilder()
.append(id).append(name).toHashCode();
}
/**
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
.append("id", id).append("name", name).toString();
}
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(nullable=false,length=255)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne
@JoinColumn(name="album_id")
public Album getAlbum() {
return album;
}
public void setAlbum(Album album) {
this.album = album;
}
@Column(name="date_created")
@Temporal(TemporalType.TIMESTAMP)
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
}