java - Many-to-many with additional attribute and without mapping many-to-many as separate entity -


the below example works fine, hovewer check whether approach, haven't had such requirement before , curious how should done.

i have 3 tables: employee, meeting, employee_meeting , 2 mappings many-to-many relationship betweene employee , meeting. seems able insert, load , update everything. wanted achieve not create additional mapping, dao, , separate model relation.

create table "employee_meeting" (   "employee_id" bigint(20) not null,   "meeting_id" bigint(20) not null,   "opinion" varchar(50) default null,   primary key ("employee_id","meeting_id"),  key "fk_meeting" ("meeting_id"), constraint "fk_employee" foreign key ("employee_id") references "employee"      ("employee_id"),  constraint "fk_meeting" foreign key ("meeting_id") references "meeting" ("meeting_id") ) engine=innodb default charset=latin1$$   <hibernate-mapping package="net.viralpatel.hibernate">  <class name="employee" table="employee">     <id name="employeeid" column="employee_id">         <generator class="native" />     </id>      <property name="firstname" />     <property name="lastname" column="lastname" />   <set name="meetings" table="employee_meeting" inverse="false" lazy="false" fetch="select" cascade="all" >     <key column="employee_id" />     <composite-element class="employeemeeting">         <parent name="employee" />         <many-to-one name="meeting" column="meeting_id" not-null="true" cascade="all"             class="meeting" />         <property name="opinion" column="opinion" />     </composite-element> </set>    </class> 

<class name="meeting" table="meeting">      <id name="meetingid" type="java.lang.long"         column="meeting_id">         <generator class="native" />     </id>      <property name="subject" column="subject" />     <property name="meetingdate" type="date" column="meeting_date" />   </class> 

 public static void main(string[] args) {      sessionfactory sf = hibernateutil.getsessionfactory();     session session = sf.opensession();     session.begintransaction();      meeting meeting1 = new meeting("quaterly sales meeting");     meeting meeting2 = new meeting("weekly status meeting");      employee employee1 = new employee("sergey", "brin");     employee employee2 = new employee("larry", "page");      session.save(meeting1);      employeemeeting em = new employeemeeting(employee1, meeting1, "bad");      employee1.getmeetings().add(em);      session.save(employee1);     session.save(employee2);      session.gettransaction().commit();      session.evict(employee1);     session.begintransaction();      employee e1 = (employee) session.load(employee.class, employee1.getemployeeid());      employeemeeting em2 = e1.getmeetings().iterator().next();     system.out.println("test1:" + em2.getopinion());      em2.setopinion("better");     session.save(e1);      session.gettransaction().commit();      session.evict(e1);     session.begintransaction();      employee e2 = (employee) session.load(employee.class, employee1.getemployeeid());      system.out.println("test2:" + e2.getmeetings().iterator().next().getopinion());     system.out.println("test2:" + e2.getmeetings().iterator().next().getmeeting().getmeetingid());     system.out.println("test2:" + e2.getmeetings().iterator().next().getemployee().getemployeeid());      [/code] 

reduce manty-to-many collection in these way, without bi-directional, equal onetomany collections.

i suggest respect many-to-many collection, create under meeting class array of employee , define bi-directional mapping.

what's version of hibernate used ?


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -