I am trying to correctly map the following scenario:

- A Paper can have many Authors.
- An Author can have many Papers.
- Every Author appears in the list of a Paper's authors in a given position.
- Positions are integer values, not necessarily consecutively ordered starting at 0 (this rules out using a <list>, I think).


I am using Hibernate 1.2.3.

Following the advice given at page 37 of the reference doc, I mapped my entities as follows:

<class name="eg.Paper" table="Paper">

...

<set role="Authors" table="Authorship" lazy="false">
<key column="Paper"/>
<composite-element class="eg.Authorship">
<property name="Position"/>
<many-to-one name="Author" column="Author" class="eg.Author" not-null="true"/>
</composite-element>
</set>


</class>

This works fine as long as creating papers and associating authors to them is concerned. But now I have a new requirement: I must be able to delete an author. Of course, if I just do a session.delete(theAuthor) and theAuthor has some papers in his name, the operation fails with a referential integrity violation error, trying to delete a row from the AUTHOR table that is referred to by one or more rows in the AUTHORSHIP table.

I didn't want to manually find and delete the dependent rows, so I thought about declaring the relationship between Authors and Papers in the Author.hbm.xml file, like the following:

<class name="eg.Author" table="Author">

...

  <set role="Papers" table="Authorship" lazy="false" cascade="delete">
    <key column="Author"/>
    <composite-element class="eg.Authorship">
      <property name="Position"/>
      <many-to-one name="Paper" column="Paper"
        class="eg.Paper" not-null="true"/>
    </composite-element>
  </set>

</class>

This seemed to work fine. Deleting an author automatically caused all the relevant Authorship rows to be deleted as well.

However, this caused some problems in the code where I add authors to papers. Sometimes, but not always, I get an exception after calling Paper.setAuthors(), stating that a paper with the given ID was already loaded in the current session. I think this is due to the fact that Hibernate follows the relationship from Paper to Author and back, trying to fetch twice the same paper.

If I don't declare the many-to-one relationship from Authors to Papers, everything is fine, were it not for the fact that I have to manually delete all the Authorships before deleting an Author (and I don't even know HOW!).

So, I'm asking if there's anyone who's ever found himself in the same situation, and what is the recommended way to implement this kind of relationship.

Thanks in Advance,

Ugo


-- Ugo Cei - Consorzio di Bioingegneria e Informatica Medica P.le Volontari del Sangue, 2 - 27100 Pavia - Italy Phone: +39.0382.525100 - E-mail: [EMAIL PROTECTED]



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel




Reply via email to