Announcement Announcement Module
Collapse
No announcement yet.
Cascasde Delete in Hibernate does not work Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cascasde Delete in Hibernate does not work

    Hello,

    I have a object called user that has child objects called UserAttributes which are returned as a Map. When, I try to delete a user, I get the exception below. I would like to delete the attributes with the user. Any thoughts on how I can resolve this?



    Hibernate version:3.2

    Mapping documents:

    <hibernate-mapping>
    <class name="org.openiam.idm.srvc.user.dto.User" table="USERS" >
    <comment></comment>
    <id name="userId" type="string">
    <column name="USER_ID" length="20" />
    <generator class="assigned" />
    </id>

    <property name="firstName" type="string">
    <column name="FIRST_NAME" length="40">
    <comment></comment>
    </column>
    </property>
    <property name="lastName" type="string">
    <column name="LAST_NAME" length="40">
    <comment></comment>
    </column>
    </property>
    <property name="middleInit" type="java.lang.Character">
    <column name="MIDDLE_INIT" length="1">
    <comment></comment>
    </column>
    </property>



    <map name="userAttributes" cascade="all-delete-orphan">
    <key>
    <column name="USER_ID" length="20" />
    </key>
    <map-key column="ID" type="string" />
    <one-to-many class="org.openiam.idm.srvc.user.dto.UserAttribute " />
    </map>


    </class>
    </hibernate-mapping>


    <hibernate-mapping>
    <class name="org.openiam.idm.srvc.user.dto.UserAttribute" table="user_attributes">
    <comment></comment>
    <id name="id" type="string">
    <column name="ID" length="20" />
    <generator class="assigned" />
    </id>
    <many-to-one name="users" class="org.openiam.idm.srvc.user.dto.User" fetch="select">
    <column name="USER_ID" length="20">
    <comment></comment>
    </column>
    </many-to-one>
    <!--
    <many-to-one name="metadataElement" class="org.openiam.idm.srvc.meta.dto.MetadataEleme nt" fetch="select">
    <column name="METADATA_ID" length="20">
    <comment></comment>
    </column>
    </many-to-one>
    -->
    <property name="name" type="string">
    <column name="NAME" length="20">
    <comment></comment>
    </column>
    </property>
    <property name="value" type="string">
    <column name="VALUE">
    <comment></comment>
    </column>
    </property>
    </class>
    </hibernate-mapping>



    Code:

    public void removeUser(String id) {
    if (id == null)
    throw new NullPointerException("user id is null");

    User user = new User(id);
    userDao.delete(user);
    }

    DAO code:

    public void delete(User persistentInstance) {
    log.debug("deleting User instance");
    try {
    sessionFactory.getCurrentSession().delete(persiste ntInstance);
    log.debug("delete successful");
    } catch (RuntimeException re) {
    log.error("delete failed", re);
    throw re;
    }
    }

    Full stack trace of any exception that occurs:


    org.springframework.jdbc.UncategorizedSQLException : Hibernate flushing: Could not execute JDBC batch update; uncategorized SQLException for SQL [update user_attributes set USER_ID=null, ID=null where USER_ID=?]; SQL state [01004]; error code [0]; Data truncation: Column was set to data type implicit default; NULL supplied for NOT NULL column 'ID' at row 1; nested exception is java.sql.BatchUpdateException: Data truncation: Column was set to data type implicit default; NULL supplied for NOT NULL column 'ID' at row 1
    Caused by: java.sql.BatchUpdateException: Data truncation: Column was set to data type implicit default; NULL supplied for NOT NULL column 'ID' at row 1
    at com.mysql.jdbc.ServerPreparedStatement.executeBatc h(ServerPreparedStatement.java:665)
    at org.apache.commons.dbcp.DelegatingPreparedStatemen t.executeBatch(DelegatingPreparedStatement.java:23 1)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch( BatchingBatcher.java:48)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(Ab stractBatcher.java:246)
    at org.hibernate.engine.ActionQueue.executeActions(Ac tionQueue.java:235)
    at org.hibernate.engine.ActionQueue.executeActions(Ac tionQueue.java:141)
    at org.hibernate.event.def.AbstractFlushingEventListe ner.performExecutions(AbstractFlushingEventListene r.java:298)
    at org.hibernate.event.def.DefaultFlushEventListener. onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.j ava:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(Sessio nImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(J DBCTransaction.java:106)
    at org.springframework.orm.hibernate3.HibernateTransa ctionManager.doCommit(HibernateTransactionManager. java:575)
    at org.springframework.transaction.support.AbstractPl atformTransactionManager.processCommit(AbstractPla tformTransactionManager.java:662)
    at org.springframework.transaction.support.AbstractPl atformTransactionManager.commit(AbstractPlatformTr ansactionManager.java:632)
    at org.springframework.transaction.interceptor.Transa ctionAspectSupport.commitTransactionAfterReturning (TransactionAspectSupport.java:314)
    at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:117)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :161)
    at org.springframework.aop.framework.Cglib2AopProxy$D ynamicAdvisedInterceptor.intercept(Cglib2AopProxy. java:630)
    at org.openiam.idm.srvc.user.service.UserMgr$$Enhance rByCGLIB$$fe8f55ba.removeUser(<generated>)
    at org.openiam.idm.srvc.user.UserMgrTest.testRemoveUs erWithChildren(UserMgrTest.java:97)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:164 )
    at junit.framework.TestCase.runBare(TestCase.java:130 )
    at org.springframework.test.ConditionalTestCase.runBa re(ConditionalTestCase.java:69)
    at junit.framework.TestResult$1.protect(TestResult.ja va:110)
    at junit.framework.TestResult.runProtected(TestResult .java:128)
    at junit.framework.TestResult.run(TestResult.java:113 )
    at junit.framework.TestCase.run(TestCase.java:120)
    at junit.framework.TestSuite.runTest(TestSuite.java:2 28)
    at junit.framework.TestSuite.run(TestSuite.java:223)
    at org.junit.internal.runners.OldTestClassRunner.run( OldTestClassRunner.java:35)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.run(JUnit4TestReference.java:38)
    at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)

  • #2
    Can you make this change:
    [code]
    <many-to-one name="users" class="org.openiam.idm.srvc.user.dto.User" fetch="select" inverse="true">
    ...
    [code]
    and re-test it? Can you also provide User and UserAttribute sources (at least userAttributes and users properties? For me is strange that UserAttribute.users property has [b] suffix for many-to-one relation...

    Comment


    • #3
      Hi,

      Thanks for helping. I have pasted below the source for the two classes. It was generated using the hibernate tools.

      Thanks


      Code:
      public class UserAttribute implements java.io.Serializable{
      
      	// Fields    
      
      	private String id;
      
      	private User users;
      
      	private MetadataElement metadataElement;
      
      	private String name;
      
      	private String value;
      
      	// Constructors
      
      	/** default constructor */
      	public UserAttribute() {
      	}
      
      	/** minimal constructor */
      	public UserAttribute(String id) {
      		this.id = id;
      	}
      
      	/** full constructor */
      	public UserAttribute(String id, User users,
      			MetadataElement metadataElement, String name, String value) {
      		this.id = id;
      		this.users = users;
      		this.metadataElement = metadataElement;
      		this.name = name;
      		this.value = value;
      	}
      
      	// Property accessors
      	public String getId() {
      		return this.id;
      	}
      
      	public void setId(String id) {
      		this.id = id;
      	}
      
      	public User getUsers() {
      		return this.users;
      	}
      
      	public void setUsers(User users) {
      		this.users = users;
      	}
      
      	public MetadataElement getMetadataElement() {
      		return this.metadataElement;
      	}
      
      	public void setMetadataElement(MetadataElement metadataElement) {
      		this.metadataElement = metadataElement;
      	}
      
      	public String getName() {
      		return this.name;
      	}
      
      	public void setName(String name) {
      		this.name = name;
      	}
      
      	public String getValue() {
      		return this.value;
      	}
      
      	public void setValue(String value) {
      		this.value = value;
      	}
      
      }
      Code:
      public class User extends org.openiam.base.BaseObject {
      
      	// Fields    
      
      	private String userId;
      
      	private MetadataType metadataType;
      
      	private String firstName;
      
      	private String lastName;
      
      	private Character middleInit;
      
      	private String title;
      
      	private String dept;
      
      	private String status;
      
      	private Date birthdate;
      
      	private Character sex;
      
      	private Date createDate;
      
      	private String createdBy;
      
      	private Date lastUpdate;
      
      	private String lastUpdatedBy;
      
      	private String prefix;
      
      	private String suffix;
      
      	private String userTypeInd;
      
      	private String employeeId;
      
      	private String employeeType;
      
      	private String locationId;
      
      	private String companyId;
      
      	private String companyOwnerId;
      
      	private Date expirationDate;
      
      	private String managerId;
      
      	private String jobCode;
      
      
      	private Map userAttributes = new HashMap();
      
      	// Constructors
      
      	/** default constructor */
      	public User() {
      	}
      
      	/** minimal constructor */
      	public User(String userId) {
      		this.userId = userId;
      	}
      
      	/** full constructor */
      	public User(String userId, MetadataType metadataType, String firstName,
      			String lastName, Character middleInit, String title, String dept,
      			String status, Date birthdate, Character sex, Date createDate,
      			String createdBy, Date lastUpdate, String lastUpdatedBy,
      			String prefix, String suffix, String userTypeInd,
      			String employeeId, String employeeType, String locationId,
      			String companyId, String companyOwnerId, Date expirationDate,
      			String managerId, String jobCode, Set userGrps, Set userNotes,
      			Set credentialses, Set authStates, Set userIdentityAnses,
      			Set resourceUsers, Set logins, Set roles, Map userAttributeses) {
      		this.userId = userId;
      		this.metadataType = metadataType;
      		this.firstName = firstName;
      		this.lastName = lastName;
      		this.middleInit = middleInit;
      		this.title = title;
      		this.dept = dept;
      		this.status = status;
      		this.birthdate = birthdate;
      		this.sex = sex;
      		this.createDate = createDate;
      		this.createdBy = createdBy;
      		this.lastUpdate = lastUpdate;
      		this.lastUpdatedBy = lastUpdatedBy;
      		this.prefix = prefix;
      		this.suffix = suffix;
      		this.userTypeInd = userTypeInd;
      		this.employeeId = employeeId;
      		this.employeeType = employeeType;
      		this.locationId = locationId;
      		this.companyId = companyId;
      		this.companyOwnerId = companyOwnerId;
      		this.expirationDate = expirationDate;
      		this.managerId = managerId;
      		this.jobCode = jobCode;
      		this.userGrps = userGrps;
      		this.userNotes = userNotes;
      		this.credentialses = credentialses;
      		this.authStates = authStates;
      		this.userIdentityAnses = userIdentityAnses;
      		this.resourceUsers = resourceUsers;
      		this.logins = logins;
      		this.roles = roles;
      		this.userAttributes = userAttributeses;
      	}
      
      	// Property accessors
      	public String getUserId() {
      		return this.userId;
      	}
      
      	public void setUserId(String userId) {
      		this.userId = userId;
      	}
      
      	public MetadataType getMetadataType() {
      		return this.metadataType;
      	}
      
      	public void setMetadataType(MetadataType metadataType) {
      		this.metadataType = metadataType;
      	}
      
      	public String getFirstName() {
      		return this.firstName;
      	}
      
      	public void setFirstName(String firstName) {
      		this.firstName = firstName;
      	}
      
      	public String getLastName() {
      		return this.lastName;
      	}
      
      	public void setLastName(String lastName) {
      		this.lastName = lastName;
      	}
      
      	public Character getMiddleInit() {
      		return this.middleInit;
      	}
      
      	public void setMiddleInit(Character middleInit) {
      		this.middleInit = middleInit;
      	}
      
      	public String getTitle() {
      		return this.title;
      	}
      
      	public void setTitle(String title) {
      		this.title = title;
      	}
      
      	public String getDept() {
      		return this.dept;
      	}
      
      	public void setDept(String dept) {
      		this.dept = dept;
      	}
      
      	public String getStatus() {
      		return this.status;
      	}
      
      	public void setStatus(String status) {
      		this.status = status;
      	}
      
      	public Date getBirthdate() {
      		return this.birthdate;
      	}
      
      	public void setBirthdate(Date birthdate) {
      		this.birthdate = birthdate;
      	}
      
      	public Character getSex() {
      		return this.sex;
      	}
      
      	public void setSex(Character sex) {
      		this.sex = sex;
      	}
      
      	public Date getCreateDate() {
      		return this.createDate;
      	}
      
      	public void setCreateDate(Date createDate) {
      		this.createDate = createDate;
      	}
      
      	public String getCreatedBy() {
      		return this.createdBy;
      	}
      
      	public void setCreatedBy(String createdBy) {
      		this.createdBy = createdBy;
      	}
      
      	public Date getLastUpdate() {
      		return this.lastUpdate;
      	}
      
      	public void setLastUpdate(Date lastUpdate) {
      		this.lastUpdate = lastUpdate;
      	}
      
      	public String getLastUpdatedBy() {
      		return this.lastUpdatedBy;
      	}
      
      	public void setLastUpdatedBy(String lastUpdatedBy) {
      		this.lastUpdatedBy = lastUpdatedBy;
      	}
      
      	public String getPrefix() {
      		return this.prefix;
      	}
      
      	public void setPrefix(String prefix) {
      		this.prefix = prefix;
      	}
      
      	public String getSuffix() {
      		return this.suffix;
      	}
      
      	public void setSuffix(String suffix) {
      		this.suffix = suffix;
      	}
      
      	public String getUserTypeInd() {
      		return this.userTypeInd;
      	}
      
      	public void setUserTypeInd(String userTypeInd) {
      		this.userTypeInd = userTypeInd;
      	}
      
      	public String getEmployeeId() {
      		return this.employeeId;
      	}
      
      	public void setEmployeeId(String employeeId) {
      		this.employeeId = employeeId;
      	}
      
      	public String getEmployeeType() {
      		return this.employeeType;
      	}
      
      	public void setEmployeeType(String employeeType) {
      		this.employeeType = employeeType;
      	}
      
      	public String getLocationId() {
      		return this.locationId;
      	}
      
      	public void setLocationId(String locationId) {
      		this.locationId = locationId;
      	}
      
      	public String getCompanyId() {
      		return this.companyId;
      	}
      
      	public void setCompanyId(String companyId) {
      		this.companyId = companyId;
      	}
      
      	public String getCompanyOwnerId() {
      		return this.companyOwnerId;
      	}
      
      	public void setCompanyOwnerId(String companyOwnerId) {
      		this.companyOwnerId = companyOwnerId;
      	}
      
      	public Date getExpirationDate() {
      		return this.expirationDate;
      	}
      
      	public void setExpirationDate(Date expirationDate) {
      		this.expirationDate = expirationDate;
      	}
      
      	public String getManagerId() {
      		return this.managerId;
      	}
      
      	public void setManagerId(String managerId) {
      		this.managerId = managerId;
      	}
      
      	public String getJobCode() {
      		return this.jobCode;
      	}
      
      	public void setJobCode(String jobCode) {
      		this.jobCode = jobCode;
      	}
      
      
      	public Map getUserAttributes() {
      		return this.userAttributes;
      	}
      
      	public void setUserAttributes(Map userAttributeses) {
      		this.userAttributes = userAttributeses;
      	}
      
      }

      Comment

      Working...
      X