English 中文(简体)
从Java清单中删除物品似乎并不恰当。
原标题:Hibernate inverse one to many mapping does not seem to properly handle item removal from Java list

两班:

 <class name="SpreadsheetImportTemplate" table="spreadsheetimport_template">
   <id name="id" type="int" column="id" unsaved-value="0">
     <generator class="native" />
   </id>
   <property name="name" type="java.lang.String" column="name" not-null="true" length="100" />
   <many-to-one name="creator" class="org.openmrs.User" not-null="true" />
   <property name="created" type="java.util.Date" column="date_created" not-null="true"
   length="19" />
   <many-to-one name="modifiedBy" column="changed_by" class="org.openmrs.User" not-null="false" />
   <property name="modified" type="java.util.Date" column="date_changed" not-null="false" length="19" />

  <!-- Associations -->
  <!-- bi-directional one-to-many association to SpreadsheetImportTemplateColumn -->
  <bag name="columns" cascade="all-delete-orphan" inverse="true">
    <key column="template_id" not-null="true" />
    <one-to-many class="SpreadsheetImportTemplateColumn" />
  </bag>
</class>

<class name="SpreadsheetImportTemplateColumn" table="spreadsheetimport_template_column">
  <id name="id" type="int" column="id" unsaved-value="0">
    <generator class="native" />
  </id>

  <many-to-one name="template" class="SpreadsheetImportTemplate" column="template_id" />

  <property name="columnName" type="java.lang.String" column="column_name" length="100" not-null="true" />
  <property name="dbTableDotColumn" type="java.lang.String" column="db_table_dot_column" length="100" not-null="true"/>
  <property name="extraData" type="java.lang.String" column="extra_data" length="100" not-null="false"/>

</class>

在java,两人都接过了各自的采集器和板块:

    public class SpreadsheetImportTemplate {
 Integer id;
 String name;
    Collection<SpreadsheetImportTemplateColumn> columns = new ArrayList<SpreadsheetImportTemplateColumn>();
 Date created;
 Date modified;
 User creator;
 User modifiedBy;

 public SpreadsheetImportTemplate() {
 }
     ...

public class SpreadsheetImportTemplateColumn {
 Integer id;
 SpreadsheetImportTemplate template;
 String columnName;
 String dbTableDotColumn;
 String extraData;

 public SpreadsheetImportTemplateColumn() {
 }
    ...

然而,如果我们有<条码>。 SpreadsheetImportTemplate model with somenels, and we do a template.remove (0) and subsequently a Hibernate saveOrUpdate, the relevant SpreadetImportTemplateColumn do >> > from the database :

感谢任何帮助。

Fyi, 这里是创建数据库的相关结构:

CREATE TABLE IF NOT EXISTS `spreadsheetimport_template` (
    `id` int(32) NOT NULL auto_increment,
    `name` varchar(100) NOT NULL,
    `creator` int(11) NOT NULL default  0 ,
    `date_created` datetime NOT NULL default  0000-00-00 00:00:00 ,
    `changed_by` int(11) default NULL,
    `date_changed` datetime default NULL,
    PRIMARY KEY  (`id`),
    KEY `User who wrote this spreadsheet template` (`creator`),     
    KEY `User who changed this spreadsheet template` (`changed_by`),
    CONSTRAINT `User who wrote this spreadsheet template` FOREIGN KEY (`creator`) REFERENCES `users` (`user_id`),
    CONSTRAINT `User who changed this spreadsheet template` FOREIGN KEY (`changed_by`) REFERENCES `users` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;  

CREATE TABLE IF NOT EXISTS `spreadsheetimport_template_column` (
    `id` int(32) NOT NULL auto_increment,
    `template_id` int(32) NOT NULL default  0 ,
    `column_name` varchar(100) NOT NULL,
    `db_table_dot_column` varchar(100) NOT NULL,
    `extra_data` varchar(100),
    PRIMARY KEY  (`id`),
    KEY `Spreadsheet template to which this column belongs` (`template_id`),     
    CONSTRAINT `Spreadsheet template to which this column belongs` FOREIGN KEY (`template_id`) REFERENCES `spreadsheetimport_template` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
最佳回答

相关部分

<SpreadsheetImportTemplate

public class SpreadsheetImportTemplate {

    private Integer id;
    public Integer getId() { return id; }
    public void setId(Integer id) { this.id = id; }

    private Collection<SpreadsheetImportTemplateColumn> columns = new ArrayList<SpreadsheetImportTemplateColumn>();
    public Collection<SpreadsheetImportTemplateColumn> getColumns() { return columns; }
    public void setColumns(Collection<SpreadsheetImportTemplateColumn> columns) { this.columns = columns; }

    /**
     * set up both sides
     */
    public void addColumn(SpreadsheetImportTemplateColumn column) {
        getColumns().add(column);

        column.setTemplate(this);
    }

}

<>SpreadsheetImportTemplateColumn

public class SpreadsheetImportTemplateColumn {

    private Integer id;
    public Integer getId() { return id; }
    public void setId(Integer id) { this.id = id; }

    private SpreadsheetImportTemplate template;
    public SpreadsheetImportTemplate getTemplate() { return template; }
    public void setTemplate(SpreadsheetImportTemplate template) { this.template = template; }

}

<><>>>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="br.com._3589013.model.domain">
     <class name="SpreadsheetImportTemplate">
         <id name="id">
             <generator class="native"/>
         </id>
         <bag name="columns" cascade="all,delete-orphan" inverse="true">
             <key/>
             <one-to-many class="SpreadsheetImportTemplateColumn"/>
         </bag>
    </class>
    <class name="SpreadsheetImportTemplateColumn">
         <id name="id">
             <generator class="native"/>
         </id>
         <many-to-one name="template" class="SpreadsheetImportTemplate"/>
    </class>
</hibernate-mapping>

<<> 试验>

public class PersistenceTest {

    private static SessionFactory sessionFactory;

    private Serializable id;

    @BeforeClass
    public static void setUpClass() {
        Configuration c = new Configuration();
        c.addResource("mapping.hbm.3589013.xml");

        sessionFactory = c.configure().buildSessionFactory();
    }

    @Before
    public void setUp() throws Exception {
        SpreadsheetImportTemplate sit = new SpreadsheetImportTemplate();
        sit.addColumn(new SpreadsheetImportTemplateColumn());

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        id = session.save(sit);

        session.getTransaction().commit();
    }

    @Test
    public void removedOrphan() throws Exception {
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        List<SpreadsheetImportTemplateColumn> sitcList = session.createQuery("from SpreadsheetImportTemplateColumn").list();

        assertTrue(sitcList.size() == 1);

        SpreadsheetImportTemplate sit = (SpreadsheetImportTemplate) session.get(SpreadsheetImportTemplate.class, id);
        sit.getColumns().remove(sitcList.get(0));

        session.getTransaction().commit();

        assertTrue(sit.getColumns().size() == 0);
    }

}

www.un.org/Depts/DGACM/index_spanish.htm 该公司做的是罚款!

问题回答

我无法看到这一点。

我围绕以下方面开展了工作:

public class SpreadsheetImportDAOImpl implements SpreadsheetImportDAO {

...

public SpreadsheetImportTemplate saveSpreadsheetImportTemplate(SpreadsheetImportTemplate template) {
    // TODO: Hack - how does this get specified properly in hbm.xml files???
    // Column processing: delete columns that must be deleted, make sure template field
    // points to template
    Iterator<SpreadsheetImportTemplateColumn> columnIterator = template.getColumns().iterator();
    while (columnIterator.hasNext()) {
        SpreadsheetImportTemplateColumn column = columnIterator.next();
        if (column.isDeleted()) {
            columnIterator.remove();
            sessionFactory.getCurrentSession().delete(column);
        } else {
            column.setTemplate(template);
        }
    }       
    sessionFactory.getCurrentSession().saveOrUpdate(template);
    return template;
}

...

}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...