Hello. I'm pretty new to java development, and even more new to Hibernate, and could use some help figuring out a thing or two. I have had some difficulty finding info because I am using attributes in my java class to do the hibernate mapping, rather than doing the mapping in a mapping config file. This would appear to not be the norm since I've had a lot of trouble finding info for this method. One of the things I need to do and am not sure how to do is to create a one to many relationship. As an example, assume i have two tables, one called box, and one called items. For simplicity, Box has the following fields: BoxID, BoxName. Items table has the following fields: ItemID, BoxID, ItemName. The BoxID field is the foreign key linking the items to the box they are associated with. That in mind, I'd like to create my Box java class with hibernate attributes so that i can define the relationship and retrieve an object of type box that has a property for the collection of all items associated. Something like this:
@Entity @Table(name="Box") public class Box { private Integer boxID; private String boxName;
private List<Item> boxItems;
public Box() {}
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="boxID", unique=true, nullable=false) public Integer getBoxID() { return boxID; }
public void setBoxID(Integer boxID) { this.boxID = boxID; }
@Column(name="boxName", nullable=false, length=100) public String getBoxName() { return boxName; }
public void setBoxName(String boxName) { this.boxName = boxName; }
public List<Item> getBoxItems() { return boxItems; } }
So, as you can see, what i'm not sure about is how to set the attribute on the getter for collection that should be populated from the joined table, and i'm having trouble finding the answer. I appreciate any help.
|