-->
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: Cross-reference with attributes in table
PostPosted: Wed Sep 23, 2009 6:08 pm 
Newbie

Joined: Wed Sep 23, 2009 5:49 pm
Posts: 3
Hi,

I have been running into a problem with a table structure and trying to get it mapped properly to do cascading deletes. I have been reading over some web sites about how to deal with a many-to-many relationship and working with a cross-reference table. I am fairly comfortable on the concept and have my domain objects setup and working. My problem is that when I delete one side of the many-to-many I want the record in the cross-reference table to be deleted as well. The unique situation that I am dealing with is that the cross-reference table is actually a domain object for the application I am working on. For simplicity sake I am going to use the example data setup as described at http://docs.jboss.org/hibernate/stable/ ... ollections . Where this application varies is that we have a date on the cross-referenced table. So using the example the PERSON_EVENT table would have a SIGN_UP_DATE attribute. To support this the mapping the Person class would look something like this:

Code:
<class name="Person" table="PERSON">
    <id name="id" column="PERSON_ID">
        <generator class="native"/>
    </id>
    <property name="age"/>
    <property name="firstname"/>
    <property name="lastname"/>

    <set name="events" table="PERSON_EVENT" inverse="true" cascade="all">
        <key column="PERSON_ID"/>
        <one-to-many class="PersonEvent"/>
    </set>

</class>


Then there is a new class to allow for the retrieval of the extra attribute on the PERSON_EVENT table.

Code:
<class name="PersonEvent" table="PERSON_EVENT">
    <composite-id>
        <key-many-to-one name="person" class="Person" column="PERSON_ID" />
        <key-many-to-one name="event" class="Event" column="EVENT_ID" />
    </composite-id>
    <property name="signUpDate" column="SIGN_UP_DATE" />
</class>


The code I am using to add events is as follows:

Code:
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Person aPerson = (Person) session.load(Person.class, personId);
        Event anEvent = (Event) session.load(Event.class, eventId);
        PersonEvent personEvent = new PersonEvent(aPerson, anEvent);
        aPerson.getEvents().add(personEvent);

        session.getTransaction().commit();


The problem comes when trying to delete:

Code:
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Person aPerson = (Person) session.load(Person.class, personId);
        session.delete(aPerson);
        session.getTransaction().commit();


This deletes the person record from the PERSON table but does not end up deleting the PERSON_EVENT record. I have played around with changing the inverse attribute as well as the cascade attribute on the <set> element without any success.

Anyone able to offer any insight?

Thanks


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.