-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: OneToMany - Ladeproblem
PostPosted: Fri Apr 16, 2010 7:43 am 
Newbie

Joined: Tue Apr 06, 2010 2:44 pm
Posts: 4
Ich habe folgende 2 Klassen gegeben: Tree und Nodes. Datei hat der Tree zu Nodes eine OneToMany-Beziehung. Über die Tree_id wird der join vollzogen.

Code:
@Entity
@Table(...)
public class Tree {
   
   @Id
   @GeneratedValue
   @Column(name = "id")
   private Integer id;

   private Integer tree_id;
   private String name;

   @OneToMany(fetch = FetchType.EAGER, mappedBy = "tree_id", targetEntity = Nodes.class)
   @Cascade(value=org.hibernate.annotations.CascadeType.ALL)
    @JoinColumn(name="tree_id")
   private List<Node> nodeList = new ArrayList<Node>();
...
}




Code:
@Entity
@Table(...)
public class Node implements Serializable{
   /**
    *
    */
   private static final long serialVersionUID = 1L;
   
   @Id
   @GeneratedValue
   @Column(name = "id")
   private Integer id;

        ...

   @ManyToOne(fetch = FetchType.EAGER, targetEntity = Tree.class)
        @JoinColumn(insertable =false, updatable=false, name="tree_id")
   private Tree tree = new Tree();

        ...
}



Ich versuche nun einen Tree mit einer Node zu speichern. Dies gelingt ganz gut mithilfe der uni-direktionalen Beziehung.
Code:
      // Session anlegen
      Session session = HibernateUtils.getSessionFactory().openSession();

      // Transaktion starten
      Transaction transaction = session.beginTransaction();


      Treename treename = new Treename();
      treename.setName("test");
      treename.setTree_id(4);
      
      Node node = new Node();
      node.setName("root");
      node.setTreename(treename);
      treename.getNodeList().add(node);

      session.save(treename);

      // Transaktion beenden
      transaction.commit();
      
      // Session schließen
      session.close();
      
      
      // Session anlegen
      session = HibernateUtils.getSessionFactory().openSession();

      // Transaktion starten
      transaction = session.beginTransaction();

      int lastID = (Integer)session.createQuery("SELECT MAX(id) FROM Treename").uniqueResult();
      Treename treename2 = (Treename)session.get(Treename.class, treename.getId());
      session.delete(treename2);
      assertNotNull( treename2 );
      assertNotNull( treename2.getNodeList());
      assertEquals( 2, treename2.getNodeList().size() );
      // Transaktion beenden
      transaction.commit();
      
      // Session schließen
      session.close();




Der Unittest schlägt in diesem Beispiel leider fehl.

Schließe ich die Session, öffne anschließend eine neue und lese das gleiche Objekt aus, dann bekomme ich den Tree mit einer leeren Nodeliste zurück. Lasse ich die Session bis nach dem Auslesen offen, dann klappt das Lese der dazugehörigen Nodes einwandfrei.

Was muss ich machen, damit ich auch in einer komplett neuen Session, cascadierend auslesen kann?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.