Here's a neat little tutorial to show you how to work with one to many relationships:
http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=18mappingonetomanyassociations
This example uses the simple, A Team Has Many Players idea. The annotations are straight forward:
Code:
@OneToMany(mappedBy="team",
targetEntity=Player.class,
fetch=FetchType.EAGER, cascade = CascadeType.ALL)
public List<Player> getPlayers() {return players;}
Code:
@ManyToOne
@JoinColumn(name="team_id")
public Team getTeam() {return team;}
Here's the code of how to create and save instances of Team and Player. Here I use setters, but getting the information would be as simple as doing a find or get with the primary key, and then calling the getter methods:
Code:
HibernateUtil.recreateDatabase();
Session session=HibernateUtil.beginTransaction();
Team team = new Team();
Player p1 = new Player();
Player p2 = new Player();
session.save(team);
session.save(p1);
session.save(p2);
team.setName("Pickering Atoms");
p1.setNickName("Lefty");
p1.setTeam(team);
p2.setNickName("Blinky");
p2.setTeam(team);
HibernateUtil.commitTransaction();
Check out my signature links for more Hibernate examples and JPA tutorials.