Here are a number of tutorials on how to map associations with Hibernate and Java Persistence API Annotations (JPA).
http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=19mappingmanytomanyrelationships
You can do very complex things with annotations. I map this complex relationship to show just what can be done:
Code:
package com.examscam.model;
import java.util.*;import javax.persistence.*;
@Entity
@Table(name = "client", schema = "examscam")
public class Client {
private List<Address> addresses = new Vector<Address>();
private List<Skill> skills = new Vector<Skill>();
private ClientDetail clientDetail;
private Long id;private String username;
private String password;private Boolean verified;
@Id
@GeneratedValue
@Column(name = "id")
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
@ManyToMany
@JoinTable(name = "client_skill",
joinColumns = { @JoinColumn(name = "client_id") },
inverseJoinColumns = { @JoinColumn(name = "skill_id") })
public List<Skill> getSkills() {return skills;}
public void setSkills(List<Skill> skills){this.skills=skills;}
@OneToMany(mappedBy="client", targetEntity=Address.class,
fetch=FetchType.EAGER, cascade = CascadeType.ALL)
public List<Address> getAddresses() {return addresses;}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JoinColumn(name="detail_id")
public ClientDetail getClientDetail(){return clientDetail;}
public void setClientDetail(ClientDetail clientDetail) {
this.clientDetail = clientDetail;
}
public String getPassword() {return password;}
public void setPassword(String password){this.password = password;}
public String getUsername() {return username;}
public void setUsername(String username) {
this.username = username;
}
public Boolean getVerified() {return verified;}
public void setVerified(Boolean verified){this.verified=verified;}
}
