-->
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.  [ 2 posts ] 
Author Message
 Post subject: Record from an association table does not get deleted.
PostPosted: Thu Feb 24, 2005 6:59 pm 
Beginner
Beginner

Joined: Sun Feb 20, 2005 12:14 am
Posts: 49
Hi,

Am using hibernate 2 and am a new user.

I have two persistent classes Event.java and User.java. I have a - Set favouriteEvents defined in my User class. This keeps track of the users favourite events.

I have one event and 2 users in the database currently. I have one record in the favouriteEvents table that relates user 2 to event 1.

The Event.hbm.xml and User.hbm.xml files are listed below.

(I have added a favourite event to the second users).

I can persist everything fine. Problem is when i want to delete a favouriteEvent from one of the user. This is the java code i run to delete the favourite event:


events = instance.listEvents();
Event event = (Event) events.get(0);
User user = instance.getUser(new Long(2));
user.removeFavouriteEvents(event);
instance.updateUser(user);

I check my data in the database and the favouriteEvent record does not get deleted.????? There are no exceptions throwns. Does anybody have an idea what I am doing wrong?????

Thanks.

The listEvents and updateUser functions are listed here:

private List listEvents()
{
try
{
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

List result = session.find("from Event");

tx.commit();
session.close();

return result;
}
catch (HibernateException e)
{
throw new RuntimeException(e.getMessage());
}
}

private void updateUser(User user)
{
try
{
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

session.update(user);
session.flush();

tx.commit();
session.close();
}
catch (HibernateException e)
{
e.printStackTrace();
}
}


Listings:

Event.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="med.allegro.events.Event" table="EVENT">
<id name="id" column="uid" type="long">
<generator class="increment"/>
</id>
<property name="date" type="timestamp"/>
<property name="title" column="eventtitle"/>

<set name="participatingUsers" table="participations">
<key column="event_uid"/>
<many-to-many column="user_uid" class="med.allegro.events.User"/>
</set>

</class>
</hibernate-mapping>


User.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="med.allegro.events.User" table="USER">
<id name="id" column="uid" type="long">
<generator class="increment"/>
</id>
<property name="age"/>
<property name="firstName"/>
<property name="lastName"/>

<set name="favouriteEvents" table="favourite_events">
<key column="user_uid"/>
<many-to-many
column="event_uid"
class="med.allegro.events.Event"/>
</set>

<set name="emails" table="user_emails">
<key column="user_uid"/>
<element column="email" type="string"/>
</set>

<set name="eventsJoined" table="participations" inverse="true">
<key column="user_uid"/>
<many-to-many column="event_uid" class="med.allegro.events.Event"/>
</set>

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 24, 2005 9:13 pm 
Beginner
Beginner

Joined: Sun Feb 20, 2005 12:14 am
Posts: 49
Just after I sent my previous post I found a way to accomplish what I was doing, by using the function listed below.

I have another question though, am trying to understand this. Users can have many favourite events and events could be a part of many users as their favourite events , therefore I define this:

Code:
            <set name="favouriteEvents" table="favourite_events">

                  <key column="user_uid"/>

                  <many-to-many

                        column="event_uid"                 

                        class="med.allegro.events.Event"/>
            </set>


Now, how do I tell hibernate to delete all associations from the favourite_events table when I delete a favourite event from the favouriteEvents Set in the User ?

i.e. If I were to do this:

User user1 = (User) users.get(0);

instance.deleteUser(user1);



QUESTION 1: I want the user1 and the associated records from the favourite_events table deleted. When I delete the user1. Is there a way to get this accomplished?

QUESTION 2: Have I written the removeFavouriteEvents function optimally or is there an easier way to do this?


Thanks.

Listing:

Code:
    public void removeFavouriteEvents(User user, Event event)

    {       

        try

        {

            Session session = sessionFactory.openSession();

            Transaction tx = session.beginTransaction();



            User u1 = getUser(user.getId());

            Set favouriteEvents = u1.getFavouriteEvents();

           

            if(favouriteEvents != null && favouriteEvents.isEmpty() == false)

            {

                Event evt = null;

                Iterator it = favouriteEvents.iterator();

                boolean f = false;

                while(it.hasNext() && f == false)

                {

                    evt = (Event)it.next();

                   

                    if( evt.getId().longValue() == event.getId().longValue())

                    {

                        f = true;

                    }

                }

               

                if(f == true)

                {

                    favouriteEvents.remove(evt);

                }

            }



            u1.setFavouriteEvents(favouriteEvents);

           

            session.update(u1);

            session.flush();

           

            tx.commit();

            session.close();

        }

        catch (HibernateException e)

        {

            e.printStackTrace();

        }

    }


Code:
    private void deleteUser(User user)
    {
        try
        {
            Session session = sessionFactory.openSession();
            Transaction tx = session.beginTransaction();
           
            session.delete(user);
           
            tx.commit();           
            session.close();
        }
        catch(HibernateException ex)
        {
            ex.printStackTrace();
        }       
    }


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

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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.