Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Saving the fields of a ternary relationship with hibernate?

Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a calendar application in which I have a ternary relationship between the meetings, the dates in which these meetings occur and the place in which they take place, so I have a table for meetings, another one for dates and another one for places and a table to model the relationship (meeting_date_place) which has as primary key a meeting id, a place id and a date id, but it also has as fields for the relationship the user who modified it and the time in which this modification took place.

For the application what I did was to have a set of meetingPlace in Date objects and for this in the mapping file for Date I have:
<set name="meetingPlace" table="meeting_date_place" lazy="false">
<key column="id_date" />
<composite-element class="MeetingPlace">
<many-to-one name="meeting" class="Meeting" column="id_meeting"lazy="false" cascade="none"/>
<many-to-one name="place" class="Place" column="id_place"lazy="false" />

Now I'm trying to be able to write and read the user and the date of modification from this table. For this I have introduced a new class MeetingDatePlace (I don't like to have to do this too much, because it's not something natural from the application, but instead this class exists because I don't know of a better way of doing this using Hibernate, I'm kind of a noob here). This is its mapping file:

<key-property name="id_meeting" type="java.lang.Long"column="id_meeting" />
<key-property name="id_place" type="java.lang.Long" column="id_place" />
<key-property name="id_date" type="java.lang.Long" column="id_date" />

<property name="userMod" type="java.lang.String">
<column name="user_mod" not-null="false" />

<property name="lastMod" type="timestamp">
<column name="last_mod" not-null="false" />

I'm using DAO objects to save the objects to the database with:

so when the user creates a new meeting a method is called which takes as parameters a Date object, the name of the user and the current timestamp and I just have to call the save method from the DAO for the date, the meeting and place which I get from the date and a new MeetingDatePlace object I create with new MeetingDatePlace(meeting.getIdMeeting(), place.getIdPlace(), date.getIdDate(), user, timestamp);

But this doesn't work as it should. Ok, it does write the user and the current date for the first time (the save call gets the current state for MeetingDatePlace and then it updates it with the user and the date), but when I restart the server when I call save for the meeting object, it does an insert to the meeting table, BUT IT ALSO MAKES A DELETE!!:
Hibernate: /* delete collection Date.meetingPlace */ delete from meeting_date_place where id_date=?

and then it makes an insert to meeting_date_place again with the same values for the ids, but as the user and the date is written when saving the MeetingDatePlace object, this info is lost.

Does this make any sense? What is happening?
    Bookmark Topic Watch Topic
  • New Topic