Hello,
I am devolping with Hibernate 4.x and I want to know how to map a class than only contains a List as an attribute. All the elements of the List are inserted in the same table (treerepresentation). The parent class is the representation of a tree and the List contains all the nodes. I want to save in order on db, so I have the order of the nodes inside.
SQL Table is:
Code:
CREATE TABLE IF NOT EXISTS treerepresentation (
`id` VARCHAR(45) NOT NULL ,
`node_order` INT NOT NULL ,
`symbol` INT NULL ,
`frequency` INT NULL ,
`added` TINYINT(1) NULL ,
`children` INT NULL ,
PRIMARY KEY (`id`, `node_order`) )
ENGINE = InnoDB
The primary key is an identifier (represents the "owner" of the tree) and the order. In the same table there will be more than one tree.
The parent object is TreeRepresentation, this is the class definition:
Code:
public class TreeRepresentation implements Serializable {
private String id;
private List<TreeNodeRepresentation> nodes = new ArrayList<TreeNodeRepresentation>();
/* constructors, getters and setters */
}
The id attribute represents the "owner" of the tree.
The node class representing each node of the tree is TreeNodeRepresentation, this is the class definition:
Code:
public class TreeNodeRepresentation implements Serializable {
private String terminalId;
private int order;
private int symbol;
private int frequency;
private boolean added;
private int children;
/* constructors, getters and setters */
}
I tried this mappings:
TreeRepresentation.hbm.xmlCode:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hbm">
<class name="TreeRepresentation">
<meta attribute="class-description">
</meta>
<id name="id" type="string"/>
<list name="nodes" table="treerepresentation">
<key>
<column name="id"/>
</key>
<list-index column="node_order"/>
<composite-element class="TreeNodeRepresentation"/>
</list>
</class>
</hibernate-mapping>
TreeNodeRepresentation.hbm.xmlQuote:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hbm">
<class name="TreeNodeRepresentation">
<meta attribute="class-description">
</meta>
<composite-id>
<key-property name="terminalId" column="id" type="string"/>
<key-property name="order" column="node_order" type="int"/>
</composite-id>
<property name="symbol" column="symbol" type="int" />
<property name="frequency" column="frequency" type="int" />
<property name="added" column="added" type="boolean" />
<property name="children" column="children" type="int" />
</class>
</hibernate-mapping>
It could be said that the TreeRepresentation is an aggregation of TreeNodeRepresentation objects and it could be thought that having a container wrapper is not the best solution, but it is required having the TreeRepresentation and the List inside.
The error I am having occurs when I commit a save action:
Quote:
session.save((TreeRepresentation) tree);
session.getTransaction().commit();
The error I get is:
Code:
2013-09-13 11:55:53,921 DEBUG [Thread-6] org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement(SqlStatementLogger.java:104) - insert into TreeRepresentation (id) values (?)
Hibernate: insert into TreeRepresentation (id) values (?)
2013-09-13 11:55:53,937 DEBUG [Thread-6] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions(SqlExceptionHelper.java:139) - Field 'node_order' doesn't have a default value [n/a]
java.sql.SQLException: Field 'node_order' doesn't have a default value
Besides if I set a default value to node_order to 0 I get another exception (duplicate entry for primary key) :
Code:
2013-09-13 12:44:15,328 DEBUG [Thread-6] org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement(SqlStatementLogger.java:104) - insert into TreeRepresentation (id) values (?)
Hibernate: insert into TreeRepresentation (id) values (?)
2013-09-13 12:44:15,359 DEBUG [Thread-6] org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1205) - Inserting collection: [TreeRepresentation.nodes#3551]
2013-09-13 12:44:15,359 DEBUG [Thread-6] org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement(SqlStatementLogger.java:104) - insert into treerepresentation (id, node_order) values (?, ?)
Hibernate: insert into treerepresentation (id, node_order) values (?, ?)
2013-09-13 12:44:15,375 DEBUG [Thread-6] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions(SqlExceptionHelper.java:139) - Duplicate entry '3551-0' for key 'PRIMARY' [n/a]
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '3551-0' for key 'PRIMARY'
What am I doing wrong in the mapping files?
Thank you very much.
Best regards.