Hibernate,更新包含集合的对象而不加载集合

时间:2016-02-18 15:13:01

标签: java xml hibernate hbm hibernate-session

我有一个包含一组列的项目,其中包含一组包含一组TaskAssigness的任务。

我正在尝试更新列标题而不必加载其任务集,但是我收到以下错误:

[AssertionFailure] - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
    org.hibernate.AssertionFailure: collection [com.example.tasks.Task.taskAssignees] was not processed by flush() 

我有以下hbm.xml:

Project.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="com.exemple.tasks.project.Project" table="P_TASKS_PROJECT">
    <cache usage="read-write" />
    <id name="rowId" column="rowId">
      <generator class="native"/>
    </id>
    ...
    <set name="columns" table="P_TASK_PROJECT_COLUMN" inverse="true" cascade="all" sort="com.exemple.tasks.project.ColumnIndexComparator">
      <cache usage="read-write" />
      <key column="projectId" not-null="true" />
      <one-to-many class="com.exemple.tasks.project.Column" />
    </set>

  </class>
</hibernate-mapping>

Column.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="com.example.tasks.project.Column" table="P_TASK_PROJECT_COLUMN">
    <cache usage="read-write" />
    <id name="rowId" column="rowId">
      <generator class="native"/>
    </id>
    ...
    <property name="title" />
    <many-to-one name="project" class="com.example.tasks.project.Project">
      <column name="projectId" not-null="false" />
    </many-to-one>

    <set name="tasks" table="P_TASKS_JOIN_COLUMNS">
      <cache usage="read-write" />
      <key column="columnId" not-null="true" />
      <many-to-many column="taskId" class="com.example.tasks.Task" unique="false" />
    </set>
  </class>
</hibernate-mapping>

Task.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="com.example.tasks.Task" table="P_TASK">
    <cache usage="read-write" />
    <id name="rowId" column="rowId">
      <generator class="native"/>
    </id>
    ...
    <set name="taskAssignees" table="P_TASK_ASSIGNEE" cascade="all-delete-orphan">
      <cache usage="read-write" />
      <key column="taskId" not-null="true" />
      <one-to-many class="com.example.tasks.TaskAssignee" />
    </set>

  </class>
</hibernate-mapping>

TaskAsignee.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="com.example.tasks.TaskAssignee" table="P_TASK_ASSIGNEE">
    <cache usage="read-write" />
    <id name="rowId" column="rowId">
      <generator class="native"/>
    </id>
    ...
    <many-to-one name="task" class="com.example.tasks.Task" insert="false" update="false">
      <column name="taskId" not-null="true" />
    </many-to-one>

  </class>
</hibernate-mapping>

我正试图以这种方式更新专栏的标题:

//Updating column title
Column column = project.getColumn(index); // return a column from the project. Project contains a SortedSet of Column
column.setTitle("NewTitle");
session.saveOrUpdate(project);

如果列不包含任何任务,则此方法正常。 如果我尝试更新包含任务的列,则会收到以下错误消息:

[AssertionFailure] - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: collection [com.example.tasks.Task.taskAssignees] was not processed by flush() 

如果我在更新项目之前获得任务和任务,我没有错误:

Set<Task> tasks = col.getTasks();
  if (tasks != null) {
    for (Task task : tasks) {
      task.getTaskAssignees();
     }
   }
session.saveOrUpdate(project);

有没有办法更改我的列标题,然后更新项目而无需获取任务和任务受理人?我可以告诉hibernate只更新列而不是试图进一步。

0 个答案:

没有答案