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