Bonjour,
j'utilise la version 3 de Hibernate et je ne sais pas si ce que je souhaite faire est possible.
Je definis l'objet suivant :
Code:
package com.resultgroup.river.business.domain;
import java.util.Set;
public class Node extends Domain
{
public final static String TYPE_FILE = "FILE";
public final static String TYPE_FOLDER = "FOLDER";
private String name;
private String type;
private Node parent;
private Set children;
private int level;
private String idParent;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
public Set getChildren()
{
return children;
}
public void setChildren(Set children)
{
this.children = children;
}
public int getLevel()
{
return level;
}
public void setLevel(int level)
{
this.level = level;
}
public Node getParent()
{
return parent;
}
public void setParent(Node parent)
{
this.parent = parent;
}
public String getIdParent()
{
return idParent;
}
public void setIdParent(String idParent)
{
this.idParent = idParent;
}
}
Avec le mapping suivant:
Code:
<?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.resultgroup.river.business.domain.Node" table="nodes">
<id name="id" type="string" unsaved-value="null" >
<column name="node_id" sql-type="int(16)" not-null="true"/>
<generator class="native"/>
</id>
<property name="name" type="string">
<column name="name" length="16" not-null="true"/>
</property>
<property name="type" type="string">
<column name="type" length="16" not-null="true"/>
</property>
<property name="level" type="integer">
<column name="level" length="16" not-null="true"/>
</property>
<many-to-one name="parent" column="node_ref_id" not-null="false"/>
<set name="children" inverse="true" cascade="all-delete-orphan" lazy="true">
<key column="node_id"/>
<one-to-many class="com.resultgroup.river.business.domain.Node" />
</set>
</class>
</hibernate-mapping>
les tables MySQL cree sont les suivantes:
Code:
create table nodes (
node_id int(16) not null auto_increment,
name varchar(16) not null,
type varchar(16) not null,
level integer not null,
node_ref_id varchar(255),
primary key (node_id)
)
alter table nodes add index FK64212B1ACACFCF1 (node_id), add constraint FK64212B1ACACFCF1 foreign key (node_id) references nodes (node_id)
alter table nodes add index FK64212B1650B859D (node_ref_id), add constraint FK64212B1650B859D foreign key (node_ref_id) references nodes (node_id)
j'obtient l'erreur suivante :
Code:
org.hibernate.exception.ConstraintViolationException: could not insert: [com.resultgroup.river.business.domain.Node]
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1767)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2168)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:34)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:239)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:159)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:477)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:472)
at com.resultgroup.river.business.persistence.HibernatePersistenceManager.store(HibernatePersistenceManager.java:21)
at com.resultgroup.river.business.command.CreateNodeRoot.saveNode(CreateNodeRoot.java:26)
at com.resultgroup.river.business.command.CreateNodeRoot.action(CreateNodeRoot.java:21)
at com.resultgroup.river.business.command.Command.execute(Command.java:30)
at com.resultgroup.river.business.command.Main.action(Main.java:72)
at com.resultgroup.river.business.command.Command.execute(Command.java:30)
at com.resultgroup.river.business.command.Main.main(Main.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
Caused by: java.sql.SQLException: Cannot add or update a child row: a foreign key constraint fails
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2851)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1531)
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1366)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:952)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1974)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1897)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1758)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1749)
... 25 more
java.lang.UnsupportedOperationException: TODO!org.hibernate.exception.ConstraintViolationException: could not insert: [com.resultgroup.river.business.domain.Node]
at com.resultgroup.river.business.persistence.HibernatePersistenceManager.store(HibernatePersistenceManager.java:26)
at com.resultgroup.river.business.command.CreateNodeRoot.saveNode(CreateNodeRoot.java:26)
at com.resultgroup.river.business.command.CreateNodeRoot.action(CreateNodeRoot.java:21)
at com.resultgroup.river.business.command.Command.execute(Command.java:30)
at com.resultgroup.river.business.command.Main.action(Main.java:72)
at com.resultgroup.river.business.command.Command.execute(Command.java:30)
at com.resultgroup.river.business.command.Main.main(Main.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
org.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs)
java.lang.NullPointerException
at com.resultgroup.river.business.command.Main.action(Main.java:78)
at com.resultgroup.river.business.command.Command.execute(Command.java:30)
at com.resultgroup.river.business.command.Main.main(Main.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
Process finished with exit code 0
Avec par exemple le code suivant (pour cree un noeud root):
Code:
Node node = new Node();
node.setName(name);
node.setType(Node.TYPE_FOLDER);
node.setChildren(new HashSet());
node.setLevel(0);
node.setParent(node);
saveNode(node);
Est t'il possible de faire cela? (est il possible de relier une cle primaire a une cle etrangere faisant partie de la meme table?)
Il y a t'il un autre moyen?
Merci beaucoup.