java - Hibernate: mapping nested objects -EDITED- -
for example, have following tables:
user:
| userid | username | usertypeid |
usertype:
| usertypeid | usertypename |
so usertypeid foreign key user use refer usertype. java implementation such:
i have class user , class usertype, looks like:
public class user { private int id; private usertype ut; .... } public class usertype { ... }
in user.hbm.xml:
<hibernate-mapping> <class name="com.pretech.user" table="user"> <meta attribute="class-description"> class contains employee detail. </meta> <id name="id" type="int" column="userid"> <generator class="native"/> </id> </class> </hibernate-mapping>
so questions follows:
1) should put map usertype
here , indicate usertype foreign key?
2) in case of user includes list of usertype
(may not conceptually true want use example), such as:
public class user { private int id; private list<usertype> uts; }
what shoud hibernate mapping?
edit: added explanation foreign key stuff.
there many examples available in net example, can check hibernate documentation:
for example if want have user
entity set of usertype's
can use one-to-many
relationship , mapping file be:
<class name="user"> <id name="id" column="id"> <generator class="native"/> </id> <set name="uts"> <key column="userid" not-null="true"/> <one-to-many class="usertype"/> </set> </class> <class name="usertype"> <id name="id" column="id"> <generator class="native"/> </id> </class>
and here example documentation uses annotations , list instead of set:
Comments
Post a Comment