Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / JavaBeans / July 2003

Tip: Looking for answers? Try searching our database.

Hibernate - one-to-many problem/questions

Thread view: 
Igor - 25 Jul 2003 04:53 GMT
Hi all,

DB: MySQL
Hibernate 2.0.1

I have a very simple (one-to-many) relationship between two tables

QUESTIONS        ANSWERS
------------------        ----------------------
id: long                    id: long
text: string               qid: long
                               text: string

qid is foreign key.

In my Question.hbm.xml I have the following:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
   <class name="my.Question" table="questions">
       <id column="id" name="id" type="long">
           <generator class="identity"/>
       </id>
       <property column="text" length="200" name="text" not-null="true"
type="string"/>
       <set name="answers" table="answers" lazy="false">
           <key column="qid"/>
           <one-to-many class="my.Answer" />
       </set>
   </class>
</hibernate-mapping>

There is more than one answer for a question always.
What happens is when I load a question with answers I only get one answer
back, though
in the hibernate trace I see the result set returned more answers and
objects were initialized.

What is wrong?

I tried to use list instead of set with id as index column, but result was
N+1 entry. Additional entry was null.
I still don't understand what should I use as an index column in this case.
Please help.

thanks in advance,
   Igor
VisionSet - 25 Jul 2003 10:16 GMT
> Hi all,
>
> DB: MySQL
> Hibernate 2.0.1
>
> I have a very simple (one-to-many) relationship between two tables

Have you tried the forum on sourceforge?

https://sourceforge.net/forum/forum.php?forum_id=128638

--
Mike W
J - 26 Jul 2003 01:36 GMT
> Hi all,
>
[quoted text clipped - 29 lines]
>     </class>
> </hibernate-mapping>

First why do you use the attribute "tables" in the set defination?  tables
should be defined when you define the class Answers.  This is what my xml
defination looks like:

<hibernate-mapping>

       <class name="com.fluidic.beans.Customer" table="service_customer" >
               <id name="id" column="customer_id" type="long"
unsaved-value="null">
                       <generator class="hilo"/>
               </id>
               <property name="firstname" column="firstname" type="string"
not-null="true"/>
               <property name="lastname" column="lastname" type="string"
not-null="true"/>
               <property name="phone" column="phone" type="string"
not-null="false "/>

               <property name="modifiedTime" column="date_modified"
type="date"  />
               <property name="createTime" column="date_created" type="date"
/>

               <set name="customerCars" lazy="true">
                   <key column="customer_id" />
                   <one-to-many class="com.fluidic.beans.CustomerCar" />
               </set>
       </class>

</hibernate-mapping>

and

<hibernate-mapping>

       <class name="com.fluidic.beans.CustomerCar" table="service_auto" >
               <id name="id" column="auto_id" type="long"
unsaved-value="null">
                       <generator class="hilo"/>
               </id>
               <property name="make" column="make" type="string"
not-null="true"/>
               <property name="model" column="model" type="string"
not-null="true"/>
               <property name="year" column="year" type="string"
not-null="false "/>

               <property name="modifiedTime" column="date_modified"
type="date"  />
               <property name="createTime" column="date_created" type="date"
/>

               <many-to-one name="customer" column="customer_id"
class="com.fluidic.beans.Customer" />
       
               <set name="serviceRequests" lazy="true">
                   <key column="auto_id" />
                   <one-to-many class="com.fluidic.beans.ServiceRequest" />
               </set>
       
       </class>

</hibernate-mapping>

I hope it helps

J
Roedy Green - 30 Jul 2003 03:45 GMT
>In my Question.hbm.xml I have the following:

What is the advantage of XML over a plain SQL query?

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
J - 31 Jul 2003 00:11 GMT
>> In my Question.hbm.xml I have the following:
>
[quoted text clipped - 4 lines]
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

The Xml file describes the object and relational mapping.  You have to create
a single XML file for each object that should map to the database.  Then when
you lookup a object, the framework creates all the sql and loads all the data
into the object.  When you save the object, the framework creates the
insert/update statements.  All without me the developer doing anything.

J


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.