-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Many to many with additional attribute and without mapping
PostPosted: Sun May 19, 2013 7:15 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 6:55 am
Posts: 41
Hi, the below example works fine, hovewer I would like you to check whether this is good approach, as I haven't had such requirement before and I am curious how it should be done.

I have three tables: EMPLOYEE, MEETING, EMPLOYEE_MEETING and only TWO mappings for MANY-TO-MANY relationship betweene EMPLOYEE and MEETING. It seems I am able to insert, load and update everything. What I wanted to achieve was not to create additional mapping, DAO, and separate model for the relation.
Code:

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$$


Code:
<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>
</hibernate-mapping>


Code:
<hibernate-mapping package="net.viralpatel.hibernate">

   <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>
</hibernate-mapping>



Code:
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());
         



From meeting class I dont need to acess employees.. I only need to access meetings from employees level - Having employee I need to insert, update, delete meetings.

I've made some tests, it seems to be working. One strange thing is when I run this code - I am only loading data from database:
Code:
 
    Employee e2 = (Employee) session.load(Employee.class, new Long(18));
        Employee e = (Employee) session.load(Employee.class, new Long(17));
        Iterator i = e.getMeetings().iterator();
        while (i.hasNext()){
           System.out.println(((EmployeeMeeting) i.next()).getMeeting().getMeetingId());
        }
        System.out.println(e2.getMeetings().iterator().next().getMeeting().getMeetingId());

        session.getTransaction().commit();
        session.close();



Why HIbernate DELETES and INSERTS meetings when I am only loading data from database???

Code:
     Hibernate: select employee0_.EMPLOYEE_ID as EMPLOYEE1_0_0_, employee0_.firstname as     firstname0_0_, employee0_.lastname as lastname0_0_ from EMPLOYEE employee0_ where     employee0_.EMPLOYEE_ID=?
     Hibernate: select meetings0_.EMPLOYEE_ID as EMPLOYEE1_0_, meetings0_.MEETING_ID as      MEETING2_0_, meetings0_.OPINION as OPINION0_ from EMPLOYEE_MEETING meetings0_           where meetings0_.EMPLOYEE_ID=?
     Hibernate: select employee0_.EMPLOYEE_ID as EMPLOYEE1_0_0_, employee0_.firstname as firstname0_0_, employee0_.lastname as lastname0_0_ from EMPLOYEE employee0_ where employee0_.EMPLOYEE_ID=?
     Hibernate: select meetings0_.EMPLOYEE_ID as EMPLOYEE1_0_, meetings0_.MEETING_ID as MEETING2_0_, meetings0_.OPINION as OPINION0_ from EMPLOYEE_MEETING meetings0_ where meetings0_.EMPLOYEE_ID=?
    12
    11
    7
    13
    7
    Hibernate: delete from EMPLOYEE_MEETING where EMPLOYEE_ID=? and MEETING_ID=?
    Hibernate: insert into EMPLOYEE_MEETING (EMPLOYEE_ID, MEETING_ID, OPINION) values (?, ?, ?)
    Hibernate: delete from EMPLOYEE_MEETING where EMPLOYEE_ID=? and MEETING_ID=?
    Hibernate: delete from EMPLOYEE_MEETING where EMPLOYEE_ID=? and MEETING_ID=?
    Hibernate: delete from EMPLOYEE_MEETING where EMPLOYEE_ID=? and MEETING_ID=?
    Hibernate: delete from EMPLOYEE_MEETING where EMPLOYEE_ID=? and MEETING_ID=?
    Hibernate: insert into EMPLOYEE_MEETING (EMPLOYEE_ID, MEETING_ID, OPINION) values (?,   ?, ?)
    Hibernate: insert into EMPLOYEE_MEETING (EMPLOYEE_ID, MEETING_ID, OPINION) values (?, ?, ?)
    Hibernate: insert into EMPLOYEE_MEETING (EMPLOYEE_ID, MEETING_ID, OPINION) values (?, ?, ?)
    Hibernate: insert into EMPLOYEE_MEETING (EMPLOYEE_ID, MEETING_ID, OPINION) values (?, ?, ?)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.