Hibernate - one-to-many problem/questions

I

Igor

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
 
J

J

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>

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
 
J

J

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

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
 
M

Mohun Biswas

J said:
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.

I've spent some (just a little) time looking at Hibernate and there are
two things that put me off it and other O/R tools. Maybe I'm
overworrying, I don't know. Here they are:

1. Thinking of the data as objects rather than tables/relations is nice
for the programmer. But what if I need to expose the underlying schema
to a non-developer (DBA, tech support, user, ...)? Isn't it likely to
much uglier than a handcrafted one? I think of what generated code
(think yacc, lex, antlr) looks like compared with handcrafted code ...
quite correct, no problem for the compiler, but woe betide the human who
has to read and fix it.

2. Hibernate claims to be within a few percent, performance-wise, of
hand-rolled solutions but it seems to me this could only be true if we
take a very naive view of hand-rolled. For instance: my current project
uses HSQLDB in its current release (1.71). In 1.71, HSQLDB is missing
support for batch processing, though it supplies addBatch() as a stub
which throws an exception if called. Version 1.72, currently approaching
beta, will have full batch support. The HSQLDB developers claim very
substantial performance gains from this. As soon as 1.72 is out I plan
to change my hand-rolled JDBC code to use batches.

Now, how can Hibernate know that 1.72 supports batches whereas 1.71 will
pretend to but throw a runtime exception? And if it doesn't, won't I
have missed a very large performance opportunity by not writing my own
JDBC code? In my experience, most DBMS have quirks like this - a query
that's way slow relative to alternatives, a constraint-ordering
requirement that's not in the SQL spec, etc.

MB
 
J

J

Now, how can Hibernate know that 1.72 supports batches whereas 1.71 will
pretend to but throw a runtime exception? And if it doesn't, won't I
have missed a very large performance opportunity by not writing my own
JDBC code? In my experience, most DBMS have quirks like this - a query
that's way slow relative to alternatives, a constraint-ordering
requirement that's not in the SQL spec, etc.

As everything in developement there will be trade offs. I mean we use Java
for portable code and other nice features. We could use Assembly, but we are
willing to take the speed decrease for the power we get from Java.

Same with OR tools. They are meant for power to the developer. When I need
to do batch queries, I would use a SQL statement, but very rarely need to do
this.

J
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top