
Originally Posted by
clem585
I'm not sure how complex types are handled. Let's say you have:
Code:
public class Player extends Entity {
private Inventory inventory;
}
How does this get mapped internally in the DB with hibernate? Wouldn't it be 2 tables?
No, you have to annotate the fields that map to a column in a table.
e.g.
Code:
@Entity
@Table(name = "players")
public class Player {
@Id
@Column(name = "id")
private long id;
@Column(name = "username")
private String username;
/* your normal code that isn't persistence */
private Inventory inventory;
public Player() { /* empty constructor for hibernate */ }
/* your normal code */
public Inventory getInventory() { return inventory; }
public void setInventory(Inventory inventory) { this.inventory = inventory; }
/* setters and getters are required by hibernate */
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
}
Now if you want to store your items in a table you can...
Code:
@Entity
@Table(name = "inventory")
public class Inventory {
// ... code similar to above but for inventory ...
}
and then construct everything:
Code:
Player player = HibernateUtil.getPlayer(id);
player.setInventory(HibernateUtil.getInventory(player));
But you don't need to have a separate table for your inventory. If you want to, you can, you should.