I'm new to Hibernate and am having some difficulty understanding the relationship between my object, the mapping for that object , and the table for that object in the database.
The sample consists of a Superhero that has a Gadget and an Identity. They are simple and can be seen below.
My database tables for Gadget and Personal match their respective objects. The Superhero table is where I get stuck.
If I map the Superhero object to the Superhero table, I wind up with a table consisting of the following columns:
superheroID
superheroName
gadget (
nonsense)
identity (
nonsense)
It's nonsense because (a) the gadget & identity tables each have superheroID as a foreign key, (b) it's dumb, and (c) it's wrong.
What is the proper way to do this? Or, what am I missing?
Thank you very much.
Hibernate version: 3.0.5
Name and version of the database you are using: MySQL 5.0.11
Code:
public class Superhero
{
private int superheroID;
private String superheroName;
// Every Superhero has a gadget!
private Gadget gadget;
// Every Superhero has a real Identity.
private Identity identity;
public Superhero() {
gadget = new Gadget();
identity = new Identity();
}
// snipped getters and setters
Code:
public class Gadget{
private int gadgetID;
private int superheroID;
private String name;
public Gadget() {
}
// snipped getters and setters
Code:
public class Identity
{
private int identityID;
private int superheroID;
private String firstName;
private String lastName;
public Identity() {
}
// snipped getters and setters
Code:
public class UseSuperhero
{
private Superhero batman;
public UseSuperhero()
{
batman = new Superhero();
batman.setSuperheroName( "Batman" );
batman.getIdentity().setFirstName( "Bruce" );
batman.getIdentity().setLastName( "Wayne" );
batman.getGadget().setName( "Bat-a-rang" );
}
public void saveSuperhero()
{
//save batman to database
}
public void loadSuperhero()
{
// load batman from database
}
}