Hi All
I am new to hibernate.
I am trying to do many-to-one mapping in hibernate.
I am using 2 tables :
1) UserTypes(usertypesid is primary key)
2) Users (userid is primary key and usertypesid is foreign key from usertypes table.)
Code:
The XML file for UserTypes table is as follows:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.useradmin.beans.admin.UserTypes" table="usertypes" >
<id name="id" column="typeid" unsaved-value="0">
<generator class="increment" />
</id>
<property name="typeName" column="typeName" not-null="true" length="50"/>
<set name="users" lazy="true" inverse="true" cascade="all-delete-orphan">
<key column="userid" />
<one-to-many class="com.useradmin.beans.admin.User" />
</set>
</class>
</hibernate-mapping>
And for the users table is as follows:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.useradmin.beans.admin.User" table="users">
<id name="id" column="userid" unsaved-value="0">
<generator class="increment" />
</id>
<many-to-one name="usertypes" column="typeid" class="com.useradmin.beans.admin.UserTypes" lazy="false" not-null="true">
</many-to-one>
<property name="firstname" column="firstname" not-null="false" length="50"/>
<property name="lastName" column="lastname" not-null="false" length="16"></property>
<property name="address" column="address" not-null="false" length="16"/>
<property name="email" column="email" not-null="false"/>
<property name="login" column="login" not-null="false" length="50"></property>
<property name="password" column="password" not-null="false" length="50"/>
</class>
</hibernate-mapping>
I have been able to insert the records into user tables by mapping successfully but the problem comes when i try to retrieve the records from userypes table..
It also retrieve the records from the user table i dnt knw y??
I have tried all the forums and articles now this is my last hope. Please help me if anybody has any idea.
The hql for usertypes is "
Code:
from usertypes"
And this is generating the sql like the followings:
Code:
Hibernate: select usertypes0_.typeid as typeid1_, usertypes0_.typeName as typeName1_ from usertypes usertypes0_
Hibernate: select user0_.userid as userid3_, user0_.typeid as typeid3_, user0_.firstname as firstname3_, user0_.lastname as lastname3_, user0_.address as address3_, user0_.email as email3_, user0_.login as login3_, user0_.password as password3_ from users user0_
I am not able to understand y its also showing the select sql from user table?
Anybody has any idea advise my..
Thanks a lot..