-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Creating one to many relationship using class attributes
PostPosted: Wed Oct 20, 2010 12:23 pm 
Newbie

Joined: Wed Oct 20, 2010 12:05 pm
Posts: 1
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.