-->
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.  [ 7 posts ] 
Author Message
 Post subject: newbie + mapping lists of objects = help
PostPosted: Wed Mar 10, 2004 3:23 am 
Newbie

Joined: Sun Mar 07, 2004 12:27 am
Posts: 5
As the title subject says, I need help with mapping lists.

I have an object that has in it a list of many other objects. I have read ch 5 and ch 6 of the tutorial many times, but nothing I find seems to clarify the problem any more. Can someone write me a quick mock up of how to map this? I think I just need to see a working example to understand fully.

pretend that these are my classes

public class Foo {

private string name;
private List bars;
...
gets and sets
...
}

public class Bar {

private Foo parent;
private string otherStuff;

...
gets and sets
...
}

Also if you could help me store a single object in the Bar class, that would be awesome too. I am using a many-to-one, but I dont think that is the proper way.

Thanks for all your help


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 3:43 am 
Newbie

Joined: Sat Jan 03, 2004 11:35 am
Posts: 9
Location: Austria
mapping of Foo:

<class table="tfoo" name="Foo">
<property name="name" type="string"/>
.....
<list name="bars" cascade="all" inverse="true">
<key column="fooid"/>
<index column="fooindex"/>
<one-to-many class="Bar"/>
</list>
</class>

mapping of Bar:

<class table="tbar" name="Bar">
<many-to-one class="Foo" column="fooid" name="parent" not-null="true"/>
<!-- other objects -->
<many-to-one class="OtherClass" column="otherId" name="otherObject"/>
</class>

code:
Code:
Foo foo = new Foo();
Bar bar = new Bar();
foo.getBars().add(bar);
session.save(foo);
session.flush();


using a many-to-one is ok for storing a single object.

guenz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 3:56 am 
Newbie

Joined: Sun Mar 07, 2004 12:27 am
Posts: 5
Awesome, at least it compiled this time.

Quick question though, when I look up the tables on a website (phpMyAdmin if that means anything), the list in foo says it is being stores in the bar table. Is this what it is supposed to do??? This is what was confusing me before. If you want, I can show you a printscreen with the actual classes I am using.

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 3:57 am 
Newbie

Joined: Sun Mar 07, 2004 12:27 am
Posts: 5
Wheres the edit button...

What I meant to say was that the list of bars is being stored in the bar table, not the foo table like I thought it would...

:)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 6:07 am 
Newbie

Joined: Sat Jan 03, 2004 11:35 am
Posts: 9
Location: Austria
Quote:
Quick question though, when I look up the tables on a website (phpMyAdmin if that means anything), the list in foo says it is being stores in the bar table. Is this what it is supposed to do???

yes.
the bar table has a column fooid. see the mapping.
show me your classes.

guenz


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2004 12:17 am 
Newbie

Joined: Sun Mar 07, 2004 12:27 am
Posts: 5
Here my classes... They are both really big, so I shorented them in the getters and setters.

public class Activity {

// #### CONSTANTS #################

// #### FIELDS #####################

//course identity
private String id;
private String name;

//list of courses
private List courses;

//course dates and times
private TimeFrame timeFrame;
private GregorianCalendar startTime;
private GregorianCalendar endTime;

//course attributes

private Category category;
private AgeGroup ageGroup;
private SkillLevel skillLevel;
private double cost;
private double courseCost;
private int maxRegistered;
private String location;
private String instructor;
private String description;
private boolean posted;

// #### CONSTRUCTORS ###############

public Activity () {

}

// #### GETS ######################

//course id and name
/**
* @return String id
*/
public String getId() {
return id;
}

/**
* @return String name
*/
public String getName() {
return name;
}

//list of course

public List getCourses() {
return courses;
}


//course dates and times

/**
* @return Calendar startDate
*/
public TimeFrame getTimeFrame() {
return timeFrame;
}


//There are a lot more getters, figure you know what is going on


// #### SETS ######################

//course id and name
/**
* @param string
*/
public void setId(String string) {
id = string;
}

/**
* @param string
*/
public void setName(String string) {
name = string;
}

//list of courses

public void setCourses(List l) {
courses = l;
}

//course dates and times

/**
* @param date
*/
public void setTimeFrame(TimeFrame tf) {
timeFrame = tf;
}

//more setters...
}

public class Course {
// #### FIELDS ##########################################################

private long id;

private List bills; //Dont worry about this...
private Activity activity;

private double courseCost;
private GregorianCalendar courseDate;


// #### CONSTRUCTORS ####################################################

public Course() {
}

// #### UTILITITES ######################################################

// #### GETS ############################################################

/**
* @return
*/
public long getId() {
return id;
}

public List getBills() {
return bills;
}

public Activity getActivity() {
return activity;
}

public double getCourseCost() {
return courseCost;
}

public GregorianCalendar getCourseDate() {
return courseDate;
}

// #### SETS ############################################################


/**
* @param long
*/
public void setId(long l) {
id = l;
}

public void setBills(List l) {
bills = l;
}

public void setActivity(Activity a) {
activity = a;
}

public void setCourseCost(double d) {
courseCost = d;
}

public void setCourseDate(GregorianCalendar g) {
courseDate = g;
}

}


What I would love is to store the List of courses in the Activity table. From the looks of my tables right now, it is being stored in the Course table, which doesn't make sense to me

Thanks for your help, sorry about the long code.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2004 8:16 am 
Newbie

Joined: Sat Jan 03, 2004 11:35 am
Posts: 9
Location: Austria
but that is how it works. courses belong to the course table and activities to the activity table. in my opinion it don't make sense to store courses to the activity table (that's why the table is named activity and not course).

hibernate is clever. it knows that the courses are in the course table. you do not have to worry about it. that's what the mapping is for.

if you load an activity from database, all courses, which belong to this activity and are stored in the course table, are loaded too. that is the great thing about hibernate!

hope could help you a little.


guenz


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

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.