views:

43

answers:

2

Two classes:

 <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>

In java both have following with respective getters and setters:

    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() {
 }
    ...

However, if we have a SpreadsheetImportTemplate template with some columns, and we do a template.remove(0) and then a Hibernate saveOrUpdate, the relevant SpreadsheetImportTemplateColumn does not get deleted from the database :(

Any help appreciated.

Fyi, here is the relevant SQL that creates the databases:

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;
A: 

I could not figure it out.

I hacked a work around:

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;
}

...

}
Misha Koshelev
@Misha Koshelev See added answer
Arthur Ronald F D Garcia
+1  A: 

Just relevant part

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; }

}

mapping

<?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"&gt;
<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>

Test

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);
    }

}

It works fine!

Arthur Ronald F D Garcia
+1 for working code and a test
Pascal Thivent