Announcement Announcement Module
Collapse
No announcement yet.
delete object fail,Duplicate key or integrity constraint Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • delete object fail,Duplicate key or integrity constraint

    Hi everyone:

    I want to delete an Object from database.My code is:
    Code:
     public void deleteObj(final Class clazz,final Serializable objID) throws DataAccessException {
            // TODO Auto-generated method stub
            log.info("Delete object : "+objID);
            HibernateTemplate hibernateTemplate = this.getHibernateTemplate();
            hibernateTemplate.execute(
               new HibernateCallback() {
                 public Object doInHibernate(Session session) throws HibernateException {
                  Object obj=session.get(clazz,objID);
                  session.delete(obj);
                  return null;
                 }
               });
        }
    But I get an exception:
    Code:
    1) testDeleteObj(com.developcentry.building.test.TestBaseDAOImpl)org.springframework.dao.DataIntegrityViolationException: (Hibernate operation): data integrity violated by SQL ''; nested exception is java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Cannot delete a parent row: a foreign key constraint fails"
    java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Cannot delete a parent row: a foreign key constraint fails"
    	at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1492)
    	at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
    	at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
    	at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:126)
    	at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2421)
    	at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2376)
    	at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
    	at org.springframework.orm.hibernate.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:214)
    	at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:177)
    	at com.developcentry.building.service.BaseDAOImpl.deleteObj(BaseDAOImpl.java:55)
    	at com.developcentry.building.test.TestBaseDAOImpl.testDeleteObj(TestBaseDAOImpl.java:62)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at com.developcentry.building.test.TestBaseDAOImpl.main(TestBaseDAOImpl.java:32)
    
    FAILURES!!!
    Tests run: 5,  Failures: 0,  Errors: 1
    Test code :
    Code:
     public final void testDeleteObj() {
            //TODO Implement deleteObj().
            udao.deleteObj(User.class,"1");
        }
    My config file is:
    Code:
    <class name="User" table="user">
    		<id
    			column="user_id"
    			name="id"
    			type="string"
    		>
    			<generator class="uuid.hex" />
    		</id>
    .................................................................
    <set
    			inverse="true"
    			lazy="true"
    			name="infoSet"
    			&#91;b&#93;cascade="all"&#91;/b&#93;		>
    			<key column="user_id" />
    			<one-to-many class="UserInfo" />
    		</set>
    .................................................................
    UserInfo.hbm.xml
    Code:
    <class name="UserInfo" table="user_info">
    ....................................................
    <many-to-one
    			class="User"
    			name="user"
    			not-null="true"
    		>
    			<column name="user_id" />
    		</many-to-one>
    I already set cascade="all".why I can't delete user with Userinfo object?
    What 's wrong with my code? Thks![/code]

  • #2
    are you mapping all user dependant tables into your Hibernate XML? do you use cascade="all" for all children?
    HTH

    Comment


    • #3
      Originally posted by irbouho
      are you mapping all user dependant tables into your Hibernate XML? do you use cascade="all" for all children?
      HTH
      Yes. I set all children to "cascade=all".
      The Userinfo is children and User is parent. The config file is:
      Code:
      <set 
               inverse="true" 
               lazy="true" 
               name="infoSet" 
               &#91;b&#93;cascade="all"&#91;/b&#93;      > 
               <key column="user_id" /> 
               <one-to-many class="UserInfo" /> 
            </set>
      Why I can't delete parent object ?

      Comment

      Working...
      X