Hallo,
erst mal meine map datei:
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">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="core.database.hibernate.Node2" table="node2" catalog="ave">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<many-to-one name="node2ByChildId" class="core.database.hibernate.Node2" fetch="select">
<column name="child_id" />
</many-to-one>
<many-to-one name="node2ByParentId" class="core.database.hibernate.Node2" fetch="select">
<column name="parent_id" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="name" length="30" not-null="true" />
</property>
<set name="node2sForChildId" inverse="true" >
<key>
<column name="child_id" />
</key>
<one-to-many class="core.database.hibernate.Node2" />
</set>
<set name="node2sForParentId" inverse="true">
<key>
<column name="parent_id" />
</key>
<one-to-many class="core.database.hibernate.Node2" />
</set>
</class>
</hibernate-mapping>
die java klassen
Code:
package core.database.hibernate;
import java.util.HashSet;
import java.util.Set;
/**
* AbstractNode2 generated by MyEclipse - Hibernate Tools
*/
public abstract class AbstractNode2 implements java.io.Serializable {
// Fields
private Integer id;
private Node2 node2ByChildId;
private Node2 node2ByParentId;
private String name;
private Set node2sForChildId = new HashSet(0);
private Set node2sForParentId = new HashSet(0);
// Constructors
/** default constructor */
public AbstractNode2() {
}
public AbstractNode2(String name){
this.name = name;
}
/** minimal constructor */
public AbstractNode2(Integer id, String name) {
this.id = id;
this.name = name;
}
/** full constructor */
public AbstractNode2(Integer id, Node2 node2ByChildId, Node2 node2ByParentId, String name, Set node2sForChildId, Set node2sForParentId) {
this.id = id;
this.node2ByChildId = node2ByChildId;
this.node2ByParentId = node2ByParentId;
this.name = name;
this.node2sForChildId = node2sForChildId;
this.node2sForParentId = node2sForParentId;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Node2 getNode2ByChildId() {
return this.node2ByChildId;
}
public void setNode2ByChildId(Node2 node2ByChildId) {
this.node2ByChildId = node2ByChildId;
}
public Node2 getNode2ByParentId() {
return this.node2ByParentId;
}
public void setNode2ByParentId(Node2 node2ByParentId) {
this.node2ByParentId = node2ByParentId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set getNode2sForChildId() {
return this.node2sForChildId;
}
public void setNode2sForChildId(Set node2sForChildId) {
this.node2sForChildId = node2sForChildId;
}
public Set getNode2sForParentId() {
return this.node2sForParentId;
}
public void setNode2sForParentId(Set node2sForParentId) {
this.node2sForParentId = node2sForParentId;
}
}
package core.database.hibernate;
// Generated by MyEclipse - Hibernate Tools
import java.util.Set;
/**
* Node2 generated by MyEclipse - Hibernate Tools
*/
public class Node2 extends AbstractNode2 implements java.io.Serializable {
// Constructors
/** default constructor */
public Node2() {
}
public Node2(String name){
super(name);
}
/** minimal constructor */
public Node2(Integer id, String name) {
super(id, name);
}
/** full constructor */
public Node2(Integer id, Node2 node2ByChildId, Node2 node2ByParentId, String name, Set node2sForChildId, Set node2sForParentId) {
super(id, node2ByChildId, node2ByParentId, name, node2sForChildId, node2sForParentId);
}
public void add(Node2 child){
child.setNode2ByParentId(this);
this.setNode2ByChildId(child);
}
}
und meine test klasse:
Code:
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import core.database.hibernate.HibernateSessionFactory;
import core.database.hibernate.Node2;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Transaction tx = null;
Node2 no = new Node2();
no.setName("a");
Node2 noB = new Node2("b");
noB.add(new Node2("c"));
no.add(noB);
Session session = HibernateSessionFactory.getSession();
try {
tx = session.beginTransaction();
session.save(no);
tx.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
List nodes = session.createQuery("select h from Node2 as h").list();
for(Iterator iter = nodes.iterator(); iter.hasNext();){
Node2 n = (Node2)iter.next();
System.out.println("Name: " +n.getName());
}
}
}
Nun meine frage: Wenn ich die test Klasse ausführe bekomme ich immer eine fehler Meldung mit der ich nix anfangen kann.
Code:
org.hibernate.TransientObjectException: core.database.hibernate.Node2
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:221)
at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:476)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:2803)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:467)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:190)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:113)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:195)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at test.main(test.java:32)
Code Zeile 32 ist: tx.commit();
Ich habe kein Ahnung was ich falsch mache. Kann mir jemand helfen??