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