-->
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.  [ 1 post ] 
Author Message
 Post subject: Delete one-to-many
PostPosted: Mon Jul 26, 2010 10:44 pm 
Newbie

Joined: Sun Nov 01, 2009 9:24 pm
Posts: 12
hi.

i have three tables
User, TodoList, Todo

i need to delete all todolists and the todos for a certain logged user
Code:
<hibernate-mapping package="com.dynwar.todoList">
    <class name="TodoListBean" table="TODOLIST" >
        <id column="TODO_LIST_ID" name="todoListID">
            <generator class="hilo"/>
        </id>
       <set cascade="all-delete-orphan" inverse="true" name="todos" lazy="false">
            <key column="TODO_LIST_ID"/>
            <one-to-many class="com.dynwar.todoList.TodoBean"/>
        </set>
        <many-to-one name="user" column="USER_ID" not-null="true"/>
    </class>
</hibernate-mapping>

Code:
<hibernate-mapping package="com.dynwar.todoList">
    <class name="TodoBean" table="TODO">
        <id column="TODO_ID" name="todoID">
            <generator class="native"/>
        </id>
    <many-to-one column="TODO_LIST_ID" name="todoList"/>
    </class>
</hibernate-mapping>

Code:
<hibernate-mapping package="com.dynwar.user">
    <class name="com.dynwar.user.UserBean" table="user">
        <id column="USER_ID" name="userID">
            <generator class="native"/>
        </id>
        <bag name="todoLists" cascade="all-delete-orphan" lazy="false" inverse="true">
            <key column="USER_ID"/>
            <one-to-many class="com.dynwar.todoList.TodoListBean"/>
        </bag>
    </class>
</hibernate-mapping>

when i am executing this code
Code:
public void deleteAllTodos() {
        UserBean user = userService.getLoggedInUser();
         Query query = sessionFactory.getCurrentSession().createQuery("delete from TodoListBean l where l.user=?");
        query.setParameter(0, user);
      query.executeUpdate();
   }


i am getting this error message

Quote:
Jul 26, 2010 10:41:30 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1451, SQLState: 23000
Jul 26, 2010 10:41:30 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Cannot delete or update a parent row: a foreign key constraint fails (`gmorcos_dynwar`.`todo`, CONSTRAINT `FK276046C365AB6F` FOREIGN KEY (`TODO_LIST_ID`) REFERENCES `todolist` (`TODO_LIST_ID`))


how can i delete the subtable contents ( todo table) and the table (todolist) for a certain user

if i dont have any todos under todolist --> i dont have any exception


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

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.