-->
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.  [ 3 posts ] 
Author Message
 Post subject: one to many mapping
PostPosted: Sun May 11, 2008 4:19 pm 
Newbie

Joined: Sun May 11, 2008 3:52 pm
Posts: 2
Hibernate 3.2;
Database: mysql 5.1

[b]mysql tables::[/b]

student;

student_id int(10)
utid varchar(20)
name varchar(20)
-------------------------
essay;

essay_id int(10)
title varchar(100)
assign_id int(10)
grade_letter varchar(2)
grade_deci float(4,2)
corrected varchar(3)
-------------------------------
essay_x_student;

essay_x_student_id int(10)
essay_id int(10)
student_id int(10)



[b]Mapping documents: essay.hdb.xml[/b]
.....
<hibernate-mapping package="marker.test">
<class name="EssayTest" table="essay">
<id name="essayId" type="int" column="essay_id">
<generator class="increment" />
</id>

<set name="students" table="essay_x_student">
<key column="STUDENT_ID" not-null="true"/>
<one-to-many class="marker.bean.Student" />
</set>

<property name="title" ......
......
</class>
</hibernate-mapping>


[b]Problem[/b]
this should be a most routine problem but I can't find a answer through
several books and tutorials.

above there is a table for students, one for essays and a cross_table
to link essays and students as in my case there might be more than student for a given essay.

I've a bean TestEssay and I'd to select all TestEssay objects from the database
and have the Student field which is a set also populated with the Set of Students for each TestEssay. ( private Set students = new HashSet () ;)

query in Hibernate:
HibUtil.getCurrentSession().createQuery("from EssayTest").list();

does yield TestEssay objects; but the TestObject with essay_id = 0
also has a Student object whose student_id = 0;

which is incorrect!

mysql query:
select e.title, e.essay_id, s.student_id, s.name
from essay_x_student x join essay e on e.essay_id = x.essay_id
join student s on x.student_id = s.student_id where e.essay_id = 0

from the mysql command line yields the correct result;

but this same query in Hibernate yields the same result as the query above.

the mapping file is taken directly from tutorials I've been reading
as have the queries;

is there another setting in the mapping file to correctly associate student objects with their essay object, or perhaps a different query?

any assistance would be greatly appreciated.

thank you


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 12:45 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Quote:
does yield TestEssay objects; but the TestObject with essay_id = 0
also has a Student object whose student_id = 0;

Can you be more clear on the quoted text?

Also from your post you say ur mapping file has
Code:
<set name="students" table="essay_x_student">
<key column="STUDENT_ID" not-null="true"/>
<one-to-many class="marker.bean.Student" />
</set>


The above mapping snippet says - "relate essay with essay_x_student using the student_id"... But I guess it should be
"relate essay with essay_x_student using the essay_id column"
i.e.,

Code:
<set name="students" table="essay_x_student">
<key column="ESSAY_ID" not-null="true"/>
<one-to-many class="marker.bean.Student" />
</set>

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 9:32 am 
Newbie

Joined: Sun May 11, 2008 3:52 pm
Posts: 2
I found the problem:

the mapping is actually many-to-many: hence the cross table;
so essay.hbm.xml should include:

<set name="students" table="essay_x_student">
<key column="ESSAY_ID" not-null="true"/>
<many-to-many column="STUDENT_ID" class="marker.bean.Student" />
</set>

thank you,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.