Hello,
I am using a one to many relation between two tables - users and cars, as every user can have more than one car.
The user table has fields: (UserID, username, password, email...), the cars table has fields (CarID, CarName, CarOwner, dateOfProduction). I have the folloding mapping classes:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="User" table="Users">
<id name="UserID">
<generator class="sequence">
<param name="sequence">UserID_SEQ</param>
</generator>
</id>
<property name="Username"/>
<property name="Password"/>
<property name="FirstName"/>
<property name="LastName"/>
<property name="Birthdate"/>
<property name="City"/>
<property name="Sex"/>
<bag name="UsersCars" table="Cars">
<key column="CarOwner"/>
<one-to-many class="Car"/>
</bag>
</class>
</hibernate-mapping>
-------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Car" table="Cars">
<id name="CarID">
<generator class="sequence">
<param name="sequence">CarID_SEQ</param>
</generator>
</id>
<many-to-one name="CarOwner" class="User" column="CarOwner"/>
<property name="CarName"/>
<property name="DateOfProduction"/>
</class>
</hibernate-mapping>
There is a unique index for column username.
I have the followind question:
How does the hibernate understands which columns to use to make the ralation between the two tables, as in thiese mapping files I am writing only the foreign key column - CarOwner - in both the bag definition and the many-to-one-name.
I mean how does the hibernate understands that the relation between the two tables is between columns Users.Username and Cars.Owner?
Thanks,
Alex
|