Hello I've the following class:
public class Movimenti implements java.io.Serializable {
private Integer id = null;
private Integer idCommessa = null;
private String nomemovimento = null;
private Movimento preventivato = null;
private Movimento effettivo = null;
public Movimento getEffettivo() {
return effettivo;
}
public void setEffettivo(Movimento effettivo) {
this.effettivo = effettivo;
}
public Movimento getPreventivato() {
return preventivato;
}
public void setPreventivato(Movimento preventivato) {
this.preventivato = preventivato;
}
public Movimenti() {
}
public Movimenti(Integer idCommessa, String nomemovimento) {
this.idCommessa = idCommessa;
this.nomemovimento = nomemovimento;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getIdCommessa() {
return this.idCommessa;
}
public void setIdCommessa(Integer idCommessa) {
this.idCommessa = idCommessa;
}
public String getNomemovimento() {
return this.nomemovimento;
}
public void setNomemovimento(String nomemovimento) {
this.nomemovimento = nomemovimento;
}
}
As you can see there are two Movimento references. Movimento looks like this:
public class Movimento {
Integer id = null;
Movimenti movimenti;
String descrizione = null;
public Movimenti getMovimenti() {
return movimenti;
}
public void setMovimenti(Movimenti movimenti) {
this.movimenti = movimenti;
}
public String getDescrizione() {
return descrizione;
}
public void setDescrizione(String descrizione) {
this.descrizione = descrizione;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
there is a reference to Movimenti in movimento. Now movimento is a base class for other classes and that gives no problems as SchemaExport does his job correctly. What I want is to have a one-to-one relationship between Movimento and Movimenti, so that I can have automatic retrivial of Movimento when I load up a Movimenti instance and vice versa and automatic deletion of orphaned Movimento objects. So I thought of putting two one-to-one relationships in Movimenti towards Movimento and one backwards. But it doesn't work, it doesn't generate the correct database tables and it even gives out exceptions. These are the mappings (there are even the subclasses that I don't include).
Movimento.hbm.xml:
<?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>
<class dynamic-insert="false" dynamic-update="false" mutable="true" name="persistence.beans.jCommesse.Movimento" optimistic-lock="version" polymorphism="implicit" select-before-update="false">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name = "descrizione" type="java.lang.String">
<column name = "descrizione"></column>
</property>
<one-to-one name = "movimenti" class = "persistence.beans.jCommesse.Movimento" constrained="true">
</one-to-one>
<joined-subclass name = "persistence.beans.jCommesse.Materiali" table = "Materiali">
<key column="id"/>
<property name = "consegnato" type="java.lang.Boolean">
<column name = "consegnato"/>
</property>
<property name="costo" type = "java.lang.Double">
<column name = "costo"/>
</property>
<property name = "costoTrasporto" type = "java.lang.Double">
<column name = "costoTrasporto"/>
</property>
<property name = "quantita" type = "java.lang.Double">
<column name = "quantita"/>
</property>
<property name = "riferimentoFattura" type = "java.lang.Integer">
<column name = "riferimentoFattura"/>
</property>
<property name = "tipoQuantita" type = "java.lang.String">
<column name = "tipoQuantita"/>
</property>
</joined-subclass>
<joined-subclass name = "persistence.beans.jCommesse.RientroMateriali" table = "RientroMateriali">
<key column="id"/>
<property name = "costo" type = "java.lang.Double">
<column name = "costo"/>
</property>
<property name = "costoDelTrasporto" type = "java.lang.Double">
<column name = "costoDelTrasporto"/>
</property>
<property name = "quantita" type = "java.lang.Double">
<column name = "quantita"/>
</property>
<property name = "riferimentoFattura" type = "java.lang.Integer">
<column name = "riferimentoFattura"/>
</property>
<property name = "tipoQuantita" type = "java.lang.String">
<column name = "tipoQuantita"/>
</property>
</joined-subclass>
<joined-subclass name = "persistence.beans.jCommesse.CostiExtra" table = "CostiExtra">
<key column = "id"/>
<property name = "nota" type = "java.lang.String">
<column name = "nota"/>
</property>
<property name = "prezzo" type = "java.lang.Double">
<column name = "prezzo"/>
</property>
</joined-subclass>
</class>
</hibernate-mapping>
and Movimenti.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 10-feb-2010 11.24.48 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="persistence.beans.jCommesse.Movimenti" table="movimenti" catalog="jcommesse">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="idCommessa" type="java.lang.Integer">
<column name="idCommessa" />
</property>
<property name="nomemovimento" type="string">
<column name="nomemovimento" length="250" />
</property>
<one-to-one name="preventivato" class="persistence.beans.jCommesse.Movimento" cascade="all" />
</class>
</hibernate-mapping>
all this doesn't create the tables but instead comes out with this nasty exception:
10-feb-2010 15.04.46 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: schema update complete
Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: persistence.beans.jCommesse.Materiali.movimenti
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
at testgeneration.testSchema(testgeneration.java:34)
at testgeneration.main(testgeneration.java:53)
Java Result: 1
as you can see it says "schema generation complete" (I'm using update for development).
Movimenti and Movimento come out like this on mySQL:
CREATE TABLE `movimenti` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`idCommessa` int(11) DEFAULT NULL,
`nomemovimento` varchar(250) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
and
CREATE TABLE `movimento` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`descrizione` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FKBEB397FC4778E205` (`id`),
CONSTRAINT `FKBEB397FC4778E205` FOREIGN KEY (`id`) REFERENCES `movimento` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1
I've found a couple of issues:
The exception is because of a bug in the use of the class Materiali (it also has a field movimenti that must be non-null but that's not related to your question and you're confused by it).
In the mapping Movimenti.hbm.xml, you forgot to map the two fields preventivato and effettivo. You must map them with reverse=true.
I really suggest to use annotations for the mapping. They are much more simple to use and keep all the information in one place.
The reason why you don't see any references in the two tables is because Hibernate creates a many-to-many mapping (which needs a third table). I understand what you're trying to achieve but I'm not sure Hibernate is smart enough to do it.
Related
How can I create a Hibernate mapping for a one-to-many mapping to a polymorphic class so that the foreign key is added to the child table?
The polymorphic class is mapped so that the base class has its own table and the child class has a table joined to the base class table. All my attempts add an id column and foreign key to the base table.
Java classes:
public class Base
{
private String id;
private int BaseProperty;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public int getBaseProperty()
{
return BaseProperty;
}
public void setBaseProperty(int baseProperty)
{
BaseProperty = baseProperty;
}
}
public class Child extends Base
{
private int childProperty;
public int getChildProperty()
{
return childProperty;
}
public void setChildProperty(int childProperty)
{
this.childProperty = childProperty;
}
}
public class Owner
{
private String id;
private Set<Child> children;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public Set<Child> getChildren()
{
return children;
}
public void setChildren(Set<Child> children)
{
this.children = children;
}
}
Hibernate mapping:
<hibernate-mapping auto-import="true" default-lazy="false">
<class name="net.opsource.oec.integration.service.sven.Owner"
table="SR_OWNER">
<id name="id" type="string" unsaved-value="null">
<column name="ID" sql-type="char(128)" not-null="true"/>
<generator class="assigned"/>
</id>
<set name="children" cascade="all">
<key column="OWNED_CHILD_ID" foreign-key="FK_CHILD2_OWNER"/>
<one-to-many class="net.opsource.oec.integration.service.sven.Child"/>
</set>
</class>
<class name="net.opsource.oec.integration.service.sven.Base"
abstract="true" table="SR_BASE"
discriminator-column="Base">
<discriminator column="JOB_TYPE" type="string" length="50"/>
<id name="id" type="string" unsaved-value="null">
<column name="ID" sql-type="char(128)" not-null="true"/>
<generator class="assigned"/>
</id>
<discriminator column="TYPE" type="string" length="50"/>
<property name="baseProperty"/>
</class>
<subclass name="net.opsource.oec.integration.service.sven.Child"
extends="net.opsource.oec.integration.service.sven.Base"
discriminator-value="Child">
<join table="SR_CHILD" fetch="select">
<key column="CHILD_ID" unique="true"
foreign-key="FK_CHILD_BASE"/>
<property name="childProperty"/>
</join>
</subclass>
</hibernate-mapping>
This generates the following schema:
create table SR_BASE (
ID char(128) not null,
TYPE varchar(50) not null,
baseProperty integer,
OWNED_CHILD_ID varchar(255),
primary key (ID)
) ENGINE=InnoDB;
create table SR_CHILD (
CHILD_ID varchar(255) not null,
childProperty integer,
primary key (CHILD_ID)
) ENGINE=InnoDB;
create table SR_OWNER (
ID char(128) not null,
primary key (ID)
) ENGINE=InnoDB;
How can I tell Hibernate to create the OWNED_CHILD_ID column and its key in the SR_CHILD table instead?
We are using Hibernate 4.2.
Thanks / Sven
I have a table (node) with 3 data
- id(pk)
- question
- result
and 2 foreign key (many-to-one)
- LEFT_ID
- RIGHT_ID
here my hbm
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.beans.Nodes" table="node">
<id name="id" type="int" access="field">
<column name="id" />
<generator class="assigned" />
</id>
<property name="question" type="java.lang.String">
<column name="question" />
</property>
<property name="result" type="java.lang.String">
<column name="result" />
</property>
<many-to-one column="LEFT_ID" name="left" class="com.beans.Nodes" insert="false" update="false"></many-to-one>
<many-to-one column="RIGHT_ID" name="right" class="com.beans.Nodes" insert="false" update="false"></many-to-one>
</class>
</hibernate-mapping>
and my bean with getter/setter on LEFT/RIGHT_ID
#Entity
#Table (name = "node")
public class Nodes
{
#Id
#GeneratedValue (strategy = GenerationType.IDENTITY)
#Column(name="id")
private int id;
#Column (name = "question")
private String question;
#Column ( name = "result")
private String result;
private Nodes LEFT_ID;
private Nodes RIGHT_ID;
public Nodes getLeftNodes()
{
return LEFT_ID;
}
public void setLeftNodes(Nodes LEFT_ID)
{
this.LEFT_ID=LEFT_ID;
}
public Nodes getRightNodes()
{
return RIGHT_ID;
}
public void setRifhtNodes(Nodes right)
{
this.RIGHT_ID=right;
}
}
But when i deploy my project, i have this error
**Query: ReadAllQuery(referenceClass=Nodes sql="SELECT id, question, result, LEFT_ID_id, RIGHT_ID_id FROM node")**
When hibernate is a select, he change the name of the column. LEFT_ID becomes LEFT_ID_id and inevitably he finds nothing !
Why hibernate change the name of the LEFT_ID column ?
thanks
I am getting :
org.hibernate.MappingException: Foreign key (FKBB979BF4266AA123:address [a_id]))
must have same number of columns as the referenced
primary key (address [p_id,a_id])
as I try to run the following (though incomplete) snippet :
public static void main(String args[]) {
Configuration config = new Configuration().configure();
SessionFactory sessFact = config.buildSessionFactory();
Session sess = sessFact.openSession();
Transaction trans = sess.beginTransaction();
}
The hibernate mapping xml is shown below :
<class name="pojo.Person" table="person">
<id column="p_id" name="personID">
<generator class="increment" />
</id>
<property name="personName" column="p_name" />
<set name="addressSet" table="address" cascade="all">
<key column="p_id" />
<many-to-many class="pojo.Address" column="a_id" />
</set>
</class>
<class name="pojo.Address" table="address">
<id column="a_id" name="addressID">
<generator class="foreign" />
</id>
<property name="address" column="address" />
</class>
I am trying a many to many association between Person and Address class.
What is the reason for this exception ?
I have created two tables person and address using these sql commands :
CREATE TABLE person(p_id INTEGER,p_name TEXT,PRIMARY KEY(p_id));
CREATE TABLE address(a_id INTEGER,address TEXT);
POJO
Person
public class Person {
private int personID;
private String personName;
private Set addressSet;
public int getPersonID() {
return personID;
}
public void setPersonID(int personID) {
this.personID = personID;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public Set getAddressSet() {
return addressSet;
}
public void setAddressSet(Set addressSet) {
this.addressSet = addressSet;
}
Address
public class Address {
private int addressID;
private String address;
private Set personSet;
public int getAddressID() {
return addressID;
}
public void setAddressID(int addressID) {
this.addressID = addressID;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Set getPersonSet() {
return personSet;
}
public void setPersonSet(Set personSet) {
this.personSet = personSet;
}
}
For a ManyToMany Relationshhip you need a dedicated mapping table
6.2.4. Collections of values and many-to-many associations
i.e. You need something like a PersonAddress Table
CREATE TABLE personaddress (p_id integer, a_id integer)
Where p_id is a FK Reference to the Person Table and a_id a FK Reference to the Address Table
You need to specify different table name for many-to-many association as it's handled by a separate table:
<class name="pojo.Person" table="person">
<id column="p_id" name="personID">
<generator class="increment" />
</id>
<property name="personName" column="p_name" />
<set name="addressSet" table="person_address" cascade="all">
<key column="p_id" />
<many-to-many class="pojo.Address" column="a_id" />
</set>
</class>
Note that <set> now references to person_addresses table. With default configuration, Hibernate is able to create it automatically.
There's another mistake that I see: ID generator for Address entity should not be foreign, it's usually used in 1-to-1 relationships (uses ID of another associated object). You can use the same 'increment' use used for Person entity:
<class name="Address" table="address">
<id column="a_id" name="addressID">
<generator class="increment" />
</id>
<property name="address" column="address" />
</class>
You need to create a reference table:
CREATE TABLE PersonsAddresses (personId BIGINT, addressId BIGINT)
and change the mapping of the set to this:
<set name="addressSet" table="PersonsAddresses" order-by="personId">
<key column="personId" foreign-key="p_id"/>
<many-to-many column="addressId" node="a_id" class="pojo.Address" />
</set>
im using an intermediate entity-class to map additional columns to a jointable.
SQL:
CREATE TABLE backlogaufgabe
(
id serial NOT NULL,
id_backlog integer NOT NULL,
id_aufgabe integer NOT NULL,
revision integer NOT NULL,
datum date NOT NULL,
rang integer NOT NULL,
aufw_schaetzung integer,
aufw_messung integer,
CONSTRAINT backlogaufgabe_pkey PRIMARY KEY (id),
CONSTRAINT backlogaufgabe_id_aufgabe_fkey FOREIGN KEY (id_aufgabe)
REFERENCES aufgabe (id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE RESTRICT,
CONSTRAINT backlogaufgabe_id_backlog_fkey FOREIGN KEY (id_backlog)
REFERENCES backlog (id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE RESTRICT
)
WITH (
OIDS=FALSE
);
public static class Id implements Serializable {
/** fkey Backlog. */
private int backlogId;
/** fkey Aufgabe. */
private int aufgabeId;
/** fkey Revision. */
private int revisionId;
/** Default-Konstruktor. */
public Id() { }
/**
* Konstruktor - Both Id.
*
* #param pAufgabeId Die Aufgaben-Id
* #param pBacklogId Die Backlog-Id
*/
public Id(final int pBacklogId, final int pAufgabeId,
final int pRevisionId) {
this.backlogId = pBacklogId;
this.aufgabeId = pAufgabeId;
this.revisionId = pRevisionId;
}
#Override
public final boolean equals(final Object obj) {
if (obj != null && obj instanceof Id) {
Id that = (Id) obj;
return (this.backlogId == that.backlogId
&& this.aufgabeId == that.aufgabeId
&& this.revisionId == that.revisionId);
} else {
return false;
}
}
#Override
public final int hashCode() {
return this.backlogId + this.aufgabeId + this.revisionId;
}
}
public class BacklogAufgabe {
private Id id = new Id();
private Backlog backlog;
private Aufgabe aufgabe;
private int revision;
private java.sql.Date datum;
private int rang;
private int aufwSchaetzung;
private int aufwMessung;
/** Default-Konstruktor. */
public BacklogAufgabe() { }
....
....
}
The Id-class generates the Id as a composite-id from 3 values, the fkeys from both tables + 1 value from the class which is and must be redundant.
My question is: how do i need to map this correctly with xml? Mapping the composite-id isnt a problem i think, but i only can map the 3rd value "revision" one time in a mapping file, i havnt found anything that helps me with this problem...
Here is my actual mapping-try:
<hibernate-mapping package="app.domain">
<class mutable="false" name="app.domain.BacklogAufgabe" table="backlogaufgabe">
<composite-id class="BacklogAufgabe$Id" name="id">
<key-property access="field" column="id_backlog" name="backlogId"/>
<key-property access="field" column="id_aufgabe" name="aufgabeId"/>
<key-property access="field" column="revision" name="revisionId"/>
</composite-id>
<property column="datum" name="datum" not-null="true" type="date"/>
<property column="rang" name="rang" not-null="true" type="int"/>
<property column="revision" name="revision" not-null="true" type="int"/>
<property column="aufw_schaetzung" name="aufwSchaetzung" not-null="true" type="int"/>
<property column="aufw_messung" name="aufwMessung" not-null="true" type="int"/>
<many-to-one cascade="save-update" column="id_aufgabe" insert="false" lazy="false"
name="aufgabe" not-null="true" update="false"/>
<many-to-one cascade="save-update" column="id_backlog" insert="false" lazy="false"
name="backlog" not-null="true" update="false"/>
<many-to-one cascade="save-update" column="revision" insert="false" lazy="false"
name="revision" not-null="true" update="false"/>
</class>
</hibernate-mapping>
So, if anyone knows how to map this sql-table with the 3 composite-id values "fkey1, fkey2, "value-from-the-class" while keeping the mapping for the property for the "value-from-the-class" i would be thankfull for any tips and answers.
In other words, how is it possible to map a column as part of the composite-key and as normal property, both together in one mapping?
Thank you for the help.
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;
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">
<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!
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;
}
...
}