Well, you've certainly come to the right place for answers! We'll do our best to make your Hibernate and JPA experience as seamless as possible. You'll do great on your assignments!
Take a look at the following object model:
This includes all of your basic one-to-one, one-to-many and many-to-many associations, not to mention a Client class with a whole whack of associations in it. But taken one at a time, it's incredibly easy to map all of these with JPA annotations. While it may look a little intimidating, here's the whole Client class annotated with JPA:
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;}
}
***Java indentation is highly overrated. ;)***
As you can see, it's easy to annotated very complicated relationships. And from here, the whole Hiberante arsenal is available to you. You can do HQL, Criteria Queries, even throw in some Hibernate specific annotation from time to time. Plus, you don't have to worry too much about xml files getting out of sync.
You can find more on this in some of the Hibernate tutorials and JPA examples on my website:
http://www.thebookonhibernate.com/HiberBookWeb/learn.jsp?tutorial=20advancedentitymapping
Good luck!