-->
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: Help for a newbie about Mappings and Queries. Read please!!
PostPosted: Fri Mar 10, 2006 6:35 am 
Newbie

Joined: Fri Mar 10, 2006 6:15 am
Posts: 4
Location: Italy
Hi all guys,

I'm totally new to Hibernate world so I have a lot to learn. Actually, I'm tryin' to realize the following:

I have two tables: Cars table and Price table made up as follow:

Car:
Integer carId [PK]
String factory
String model
String color

Price
Integer priceId [PK]
Long price
Integer id_car [FK]

The id_auto field of Price table is a foreign key given by the Car table column autoId. So I think I have a one-to-many relation between Car and Price tables.

Now I'm tryin' to achieve three goals:

- First goal: to build up two .hbm.xml files compliant with my situation.
- Second goal: to write two persistent classes, one for each table
- Third goal: to realize a join query whose resultset contains, for each car, all the available information from the two tables: factory, model, color and price or set of prices.

Ok, so ... that's my problem. I'm still stuffed at first goal.
By now, my .hbm.xml files are:

Cars.hbm.xml


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="mapping.Cars" table="CARS">
<id name="id" type="integer">
<column name="ID" />
<generator class="increment" />
</id>
<property name="factory" type="string">
<column name="FACTORY" length="45" not-null="true" />
</property>
<property name="model" type="string">
<column name="MODEL" length="45" not-null="true" />
</property>
<property name="color" type="string">
<column name="COLOR" length="45" not-null="true" />
</property>
</class>
</hibernate-mapping>


Price.hbm.xml:


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="mapping.Price" table="PRICE">
<id name="id" type="integer">
<column name="ID" />
<generator class="increment" />
</id>
<property name="price" type="double">
<column name="PRICE" precision="0" scale="0" />
</property>
<property name="idCar" type="integer">
<column name="ID_CAR" not-null="true" />
</property>
</class>
</hibernate-mapping>



Thank you in advance to any who could help me!!

Best regards
tronky


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 8:01 am 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
Hi, the first thing is that you have to decide whether this asso. is supposed to be uni / or bidirectional. I'll give you a example for bidirectional.
But why does the Price Table has the freign key? In my understanding it would make sense to have multiple cars having the same price and not one car with multiple prices.

Anyway:
Code:
public class Car{
....
  private Set prices;
.... // getter a. setter

}


public class Price{
....
  private Car car;
.... // getter a. setter

}


Code:
car.hbm.xml:

<set name="prices" inverse="true">
     <key column="car_id"/>
     <one-to-many class="Price"/>
</set>

price.hbm.xml

<many-to-one name="car" column="carId" class="Car" />

-- simon --


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 10:06 am 
Newbie

Joined: Fri Mar 10, 2006 6:15 am
Posts: 4
Location: Italy
simonwillnauer wrote:
Hi, the first thing is that you have to decide whether this asso. is supposed to be uni / or bidirectional. I'll give you a example for bidirectional.
But why does the Price Table has the freign key? In my understanding it would make sense to have multiple cars having the same price and not one car with multiple prices.


Hi simonwillnauer, thank you for replying. In my meanings one car model could have multiple price according to various optional you may choose to install. On the other way, I didn't think on the inverse relationship, so I suppose the relation is uni-directional. BTW thank you very much for your aid. I'll try immediatly.

Greetings
tronky


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.