-->
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: Can someone verify my simple mapping configuration?
PostPosted: Tue Feb 28, 2006 4:00 pm 
Newbie

Joined: Thu Jan 12, 2006 1:44 pm
Posts: 3
I'm still experimenting with Hibernate, and I would really appreciate if someone could provide me some feedbacks on my mapping configuration.

My test app has 3 database tables: PERSON, CAR and CARTYPE. The relationship between PERSON and CAR is one-to-many, and CARTYPE to CAR is one-to-many.

These are my JAVA bean classes:-

Code:

Person Table
----------------
Long   personId   
String   personName   
Set   cars   

Car Table
----------------
Long   carId
CarType   carType
Person   person

CarType Table
----------------
Long   carTypeId
String   carName



These are my database tables:-


Code:
dbo.person
----------------
personId   int (PK)
personName varchar

dbo.car
----------------
carId      int (PK)
carTypeId  int (FK)
personId   int (FK)

dbo.carType
----------------
carTypeId  int (PK)
carName    varchar



Basically, a person can have many cars. The CARTYPE table is more like a dictionary where it has a list of available car names, like Mustang, Ford, Honda, etc. So, CARTYPE is totally independent from other tables.


This is my mapping configuration:-
=========================================

Code:
<hibernate-mapping package="person">
   <class name="Person" table="Person">
      <id name="personId">
         <generator class="native" />
      </id>
      <property name="personName" />
      <set name="cars" inverse="true" cascade="all-delete-orphan">
         <key column="personId" />
         <one-to-many class="Car" />
      </set>
   </class>
   <class name="Car" table="Car">
      <id name="carId">
         <generator class="native" />
      </id>
      <many-to-one name="carType" class="CarType" column="carTypeId" not-null="true" />
      <many-to-one name="person" class="Person" column="personId" not-null="true" />
   </class>
   <class name="CarType" table="CarType">
      <id name="carTypeId">
         <generator class="native" />
      </id>
      <property name="carName" />
   </class>
</hibernate-mapping>



May I know if my mapping configuration above is set up correctly? I saw a Hibernate example (Order, LineItem, Product) which is very similar to what I have here, but it pretty much consolidates the CAR mapping into PERSON using COMPOSITE-ELEMENT tag and I wasn't quite sure what's going on there.

Thanks much in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 4:40 pm 
Beginner
Beginner

Joined: Fri Jul 30, 2004 2:53 pm
Posts: 33
Location: Washington, DC
Your mappings look good. The best way to verify that your mappings look good is to create unit tests that verify that your mappings do what you expect. For example, you might have a Car DAO with a method like this:

Code:
public List<Car> findByPerson(Long personId)


If so, write a unit test like this:

Code:
public void testfindCarsByPerson() throws Exception {
    List<Car> cars = carDao.findByPerson(1L);
    assertNotNull("Cars is null",cars);
    assertEquals(5,cars.size());
}


This assume that you would have 5 cars associated to person 1 in your test database. Once you have code that makes this test pass, then you know your mappings are correct.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 5:16 pm 
Newbie

Joined: Thu Jan 12, 2006 1:44 pm
Posts: 3
Thanks Paul. It's good to have another pair of eyes to validate my prototype mappings.

Since CARTYPE is supposed to be an independent database that contains all the car descriptions, I have made the association from CAR to CARTYPE as unidirectional assocation. Thus:

Code:
<class name="Car" table="Car">
      ...
      <many-to-one name="carType" class="CarType" column="carTypeId" not-null="true" />
      ...
   </class>


... and CARTYPE mapping doesn't have any reference back to CAR, but I can't be absolutely sure until now.

Thanks.


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.