Hibernate: two many one to one relationship -


i have 3 tables: user->profile->profiledetails

user have 1 row in profile table

profile have 1 row in profiledetails table

create table if not exists mydb.user (   id int not null auto_increment,   name varchar(45) null,   primary key (id)) engine = innodb;  create table if not exists mydb.profile (   userid int not null,   profiledata varchar(45) null) engine = innodb;  create table if not exists mydb.profiledetails (   profileid int not null,   details varchar(45) null) engine = innodb; 

how can describe these relationships in hbm.xml files, using one-to-one relationships?

i resolve problem. use primary key relations:

create table if not exists mydb.user (   id int not null,   name varchar(45) null,   primary key (id)) engine = innodb;  create table if not exists mydb.profile (   user_id int not null,   profile_data varchar(45) null,   primary key (user_id),   constraint fk_profile_user1     foreign key (user_id)     references mydb.user (id)     on delete no action     on update no action) engine = innodb;  create table if not exists mydb.profiledetails (   profile_id int not null,   details int not null,   primary key (profile_id, details),   index fk_profiledetails_profile1_idx (profile_id asc),   constraint fk_profiledetails_profile1     foreign key (profile_id)     references mydb.profile (user_id)     on delete no action     on update no action) engine = innodb; 

and next hbm.xml mapping:

user.hbm.xml

<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-mapping public         "-//hibernate/hibernate mapping dtd//en"         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping>     <class name="ru.myapp.user" table="user">         <id name="id" column="id" type="long">             <generator class="native" />         </id>          <property name="name" column="name" type="string" />          <one-to-one name="profile" class="ru.myapp.profile" />     </class> </hibernate-mapping> 

profile.hbm.xml

<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-mapping public         "-//hibernate/hibernate mapping dtd//en"         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping>     <class name="ru.myapp.profile" table="profile">         <id name="id" column="user_id" type="long">             <generator class="foreign">                 <param name="property">user</param>             </generator>         </id>          <property name="profiledata" column="profile_data" type="string" />          <one-to-one name="user" class="ru.myapp.user" />         <one-to-one name="profiledetails" class="ru.myapp.profiledetails" />     </class> </hibernate-mapping> 

profiledetails.hbm.xml

<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-mapping public         "-//hibernate/hibernate mapping dtd//en"         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping>     <class name="ru.myapp.profiledetails" table="profiledetails">         <id name="id" column="profile_id" type="long">             <generator class="foreign">                 <param name="property">profile</param>             </generator>         </id>          <property name="details" column="details" type="string" />          <one-to-one name="profile" class="ru.myapp.profile" />     </class> </hibernate-mapping> 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -