-->
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.  [ 5 posts ] 
Author Message
 Post subject: Probleme with an update in cascade
PostPosted: Thu Aug 26, 2010 7:48 am 
Newbie

Joined: Thu Aug 26, 2010 5:11 am
Posts: 4
Hi everybody,

I've got a trouble with hibernate and I really dont know why ..

I've got 2 entities in my database : MOVIE and ACTOR
There is a foreign key of a MOVIE in the base ACTOR

In my movie.hbm.xml, I've got a set of actors with cascade=all-delete-orphan.

My problem is that actually when I update a movie inwhich I delete an actor from its list of actors .. the update work fine EXCEPT for the actors .. no error but the deleted actor always is in the database with the id of the movie.
When I update a movie in which I add a new actor in its list .. this time it works fine, the new actor is created ..
I really dont understand why the delete dont work .. and no error in the log and no DELETE instruction ..


Here is the source code .. thanks for the help

Actor.hbm.xml
Code:
<hibernate-mapping package="com.company.project.daos" >
<class name="Actor" table="T_ACTOR" >
   <id name="id" type="long" column="ID" >
     <generator class="sequence">
        <param name="sequence">S_ACTOR</param>
     </generator>         
   </id>

  <property name="name" type="string"   column="NAME"/>
  <many-to-one  name="movie" class="Movie" column="MOVIE_ID" not-null="true"  />

</class>
</hibernate-mapping>   


Movie.hbm.xml
Code:
<hibernate-mapping package="com.company.project.daos" >
<class name="Movie" table="T_MOVIE" >

<id name="id" type="long" column="ID" >
    <generator class="sequence">
        <param name="sequence">S_MOVIE</param>
    </generator>         
</id>

<property name="name" type="string" column="NAME"/>
<set name="actors" cascade="all-delete-orphan" inverse="true">
   <key column="MOVIE_ID">
   </key>
   <one-to-many class="Actor"/>
</set>

</class>
</hibernate-mapping>



Mon abstract DAO
Code:
public abstract class AbstractDaoImpl extends HibernateDaoSupport implements IAbstractDao {
    public abstract String getDomainObjectName();
    public abstract String getCamelCaseDomainObjectName();
    public abstract Class getDomainObjectClass();

    public AbstractDomainObject findById(Long id) throws DaoException, ObjectNotFoundException, CheckException {
        return findById(id, true);
    }

    public AbstractDomainObject findById(Long id, boolean checkSoftDelete) throws DaoException, ObjectNotFoundException, CheckException {
        if (id == null) {
            throw new CheckException("no id");
        }

        try {
            AbstractDomainObject object = (AbstractDomainObject) getHibernateTemplate().load(getDomainObjectClass(), id);
            if (object == null) {
                throw new ObjectNotFoundException();
            }
            return object;
        } catch (Exception e) {
            throw new DaoException("cant get the object ", e);
        }
    }


    /**
     * generic count method used to retrieve the total object entities count
     *
     * @return int count
     */
    public int count() throws DaoException, CheckException  {
        LOG.debug("entering "+getDomainObjectName()+"DAO.count()");
        Criteria criteria = getSession().createCriteria(getDomainObjectClass());
        criteria.add(Restrictions.gt("id", 0L));
        criteria.setProjection(Projections.rowCount());
        List result = criteria.list();
        return ((Integer) result.get(0)).intValue();
      }


    /**
     * generic count method used to retrieve the total object entities count with search criterias
     *
     * @param String[] criterias
     * @param String[] values
     * @return int count
     */
    public int count(String[] criterias, String[] values) throws DaoException, CheckException  {
        LOG.debug("entering "+getDomainObjectName()+"DAO.count()");
        Criteria criteria = getSession().createCriteria(getDomainObjectClass());
        criteria.add(Restrictions.gt("id", 0L));
        criteria.setProjection(Projections.rowCount());
        if(criterias!=null){
            for(int i=0;i<criterias.length;i++)
            {
                criteria.add(Restrictions.eq(criterias[i],values[i]));
            }
        }
        List result = criteria.list();
        return ((Integer) result.get(0)).intValue();
      }


    /**
     * generic insert method used to insert a single object entity
     *
     * @param AbstractDomainObject object
     * @return AbstractDomainObject
     */
    public AbstractDomainObject insert(AbstractDomainObject object) throws  DaoException, DuplicateKeyException, CheckException {
        LOG.debug("entering "+getDomainObjectName()+"DAO.insert");

        try {
            // set creation values
            object.setId((Long)getHibernateTemplate().save(object));
            getHibernateTemplate().flush();
            return object;
        }
        catch (GenericJDBCException ge) {
            if ((ge.getCause() != null)
                    && (ge.getCause() instanceof SQLException)) {
                SQLException sqlException = (SQLException) ge.getCause();
                if (sqlException.getErrorCode() == TpmConstantes.DATA_ALREADY_EXIST) {
                    throw new DuplicateKeyException();
                } else {
                    throw new DaoException(ge);
                }
            } else {
                throw new DaoException(ge);
            }
        } catch (DataAccessException e) {
            throw new DaoException(e);
        }catch (ConstraintViolationException cve) {
            throw new DaoException(cve);
        }

    }

    public AbstractDomainObject insertAndEvict(AbstractDomainObject value) throws Exception
    {
        AbstractDomainObject object = insert(value);
        getHibernateTemplate().evict(object);
        return object;
    }

    /**
     * generic insert method used to delete a single object entity
     *
     * @param AbstractDomainObject object
     * @return AbstractDomainObject
     */
    public long delete(long id) throws  DaoException,ObjectNotFoundException,CheckException {

        //LOG.debug("entering "+getDomainObjectName()+"DAO.delete("+id)+")");

        try {

            AbstractDomainObject entity = (AbstractDomainObject)getSession().load(getDomainObjectClass(), id);
            getSession().delete(entity);
            //getHibernateTemplate().delete(object);
            return entity.getId();
        }
        catch (DataAccessException e)
        {
            throw new DaoException(e);
        }
    }

    /**
     * generic update method used to delete a single object entity
     *
     * @param AbstractDomainObject object
     * @return AbstractDomainObject
     */
    public AbstractDomainObject update(AbstractDomainObject object) throws CheckException, DaoException, ObjectNotFoundException {

        LOG.debug("entering "+getDomainObjectName()+"DAO.update("+object.getId()+")");

        try {
            getHibernateTemplate().saveOrUpdate(object);
            getHibernateTemplate().flush();
            return object;
        }
        catch (DataAccessException e)
        {
            throw new DaoException(e);
        }

    }


When Im in debug mode and I do a breakpoint on "getHibernateTemplate().saveOrUpdate(object)" my object is well formed without the deleted actor.
But when I look in the database or when I do a find after .. the actor already exists.

To have all the details : connexion is on DB2 database, and the frameworks version are : spring 2.5.6 and hibernate 3.2.7.ga

I need your help .. thanx a lot


Last edited by theleek on Thu Aug 26, 2010 11:19 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Probleme with an update in cascade
PostPosted: Thu Aug 26, 2010 9:54 am 
Newbie

Joined: Fri Aug 20, 2010 7:57 am
Posts: 7
Hi, whatever i got from u r problem is this that u r unable to perform the delete opertion while u r deleting an actor individually because it has a reference of movie(FK), when u r deleting or inserting a new movie with actor its working fine.
is it ????

and i think there is a small thing u have not mention in the mapping file

<class="Movie">
<set name="actors" cascade="all-delete-orphan" inverse="true">
<key column="MOVIE_ID">
</key>
<one-to-many class="Actor"/>
</set>
</class>

you have to mention inverse="true" becase you are using the reference of movie_id in both of the table so how the hibernate will relate both of these.

According to me :) , let us know if its working or not :)


Top
 Profile  
 
 Post subject: Re: Probleme with an update in cascade
PostPosted: Thu Aug 26, 2010 11:18 am 
Newbie

Joined: Thu Aug 26, 2010 5:11 am
Posts: 4
Sorry for my bad english I think it was'nt really understandable.
And sorry I've forgot to change the code. I already have inverse="true" on the actor list.

My problem is that I never manage an Actor directly.
I always update a movie and change its actor collection.
But I don't know why when I delete an actor from its list, it doesnt work .. but when I add one it works perfectly ..
I hope it was understandable this time ..


Top
 Profile  
 
 Post subject: Re: Probleme with an update in cascade
PostPosted: Fri Aug 27, 2010 3:12 am 
Newbie

Joined: Fri Aug 20, 2010 7:57 am
Posts: 7
Hi,
u r making a delete operation on the Actor (getSession().delete(entity);) and u r saving the Movie object getHibernateTemplate().saveOrUpdate(object); and u want that saveorupdate on movie should update u r actor table with the deleted desired entity.

I have done the same thing on my pc and working fine. i hav taken the example of ITEM and BIDs in your case which is Movie and Actor accordingly.
what i did is item.getBids().remove(bid); where i m able to delete the bid entity through item entity successfully.

and there is one other way, we can use actor as value type instead of entity type. By this Actor will have implicit lifecyle of Movie.

let us abt the above. :)


Top
 Profile  
 
 Post subject: Re: Probleme with an update in cascade
PostPosted: Fri Aug 27, 2010 3:46 am 
Newbie

Joined: Thu Aug 26, 2010 5:11 am
Posts: 4
I think it's maybe cause I work with DTO (Data Transfert Object).
With this new object .. Hibernate must do like a merge between this entity and the old one ..
So maybe I've to load the old object to do what you say .. item.getBids().remove(bid);
Will try some solutions ..
thx a lot for your help


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.