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!