-->
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: collection doesn't fetch all matching entries, but just one
PostPosted: Mon Jun 11, 2007 7:23 am 
Newbie

Joined: Mon Jun 11, 2007 6:22 am
Posts: 4
Hi Hibernate users,

since a few days I am puzzling about a problem with a one-to-many collection mapping.
The collection does just fetch one single element and not all matching entries.

A 'student' has many 'exams'. There are 2 tables STUDENT and EXAM. They are connected with the primary key of a student named 'matrinumber'. I have written a student mapping which contains a set with exams.

Here is the mapping code:

Code:
<class name="beans.Student" table="Student" lazy="false">
  <id name="matrinumber" type="long"></id>
  <property name="sex" column="sex" type="string"/>
  <property name="academictitle" column="academictitle" type="string"/>
  <property name="surname" column="surname" type="string"/>
  <property name="firstname" column="firstname" type="string"/>
  <property name="beginnpractyear" column="beginnpractyear" type="integer"/>
  <property name="email" column="email" type="string"/>
  <property name="telephone" column="telephone" type="string"/>
  <property name="mobile" column="mobile" type="string"/>
  <property name="notice" column="notice" type="text"/>
  <one-to-one name="marks" class="beans.Mark" cascade="all" constrained="true"/>
  <set name="exams" lazy="false">
     <key column="matrinumber"/>
     <one-to-many class="beans.Exam"/>
  </set>
</class>
<class name="beans.Exam" table="Exam" lazy="false">
  <id name="matrinumber" type="long"></id>
  <property name="datum" column="datum" type="calendar"/>
  <property name="subject" column="subject" type="string"/>
  <property name="participated" column="participated" type="boolean"/>
  <property name="excused" column="excused" type="boolean"/>
  <property name="mark" column="mark" type="float"/>
  <property name="comment" column="comment" type="string"/>
</class>


The sql code is following:

Code:
CREATE TABLE `exam` (
  `matrinumber` bigint(20) unsigned NOT NULL,
  `datum` date NOT NULL,
  `subject` varchar(2) NOT NULL,
  `participated` tinyint(1) default NULL,
  `excused` tinyint(1) default NULL,
  `mark` float unsigned NOT NULL,
  `comment` varchar(500) default NULL,
  KEY `matrinumber` (`matrinumber`),
  CONSTRAINT `exam_ibfk_1` FOREIGN KEY (`matrinumber`) REFERENCES `student` (`matrinumber`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `student` (
  `matrinumber` bigint(20) unsigned NOT NULL,
  `sex` varchar(1) default NULL,
  `academictitle` varchar(50) default NULL,
  `surname` varchar(50) NOT NULL,
  `firstname` varchar(30) NOT NULL,
  `beginnpractyear` int(11) NOT NULL,
  `email` varchar(60) NOT NULL,
  `telephone` varchar(20) default NULL,
  `mobile` varchar(20) default NULL,
  `notice` text,
  PRIMARY KEY  (`matrinumber`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



How already written above, I always get just one single element in the collection exams. But in the database are 6 exam entries.

I have no more ideas concerning this. Perhaps there is someone out who can give me some hints or debugging ideas. Thanks all.

Anja


Top
 Profile  
 
 Post subject: solved
PostPosted: Mon Jun 11, 2007 8:05 am 
Newbie

Joined: Mon Jun 11, 2007 6:22 am
Posts: 4
Hi Hibernators,

it is solved. I gave the exam table a own primary key. This works, but I do not really know, why the previous does not.

Bye
Anja


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 11, 2007 9:26 am 
Regular
Regular

Joined: Thu Jan 27, 2005 8:58 am
Posts: 80
Quote:
<class name="beans.Exam" table="Exam" lazy="false">
<id name="matrinumber" type="long"></id>


In this line you tell hibernate, that the matrinumber is unique!
Even if there is no such constraint in your database!

_________________
Weise ist nicht, wer viele Erfahrungen macht, sondern wer aus wenigen lernt, viele nicht machen zu müssen. (Karlheinz Deschner)


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.