Hello, 

I have a strange behavior during selection of my Nodes from database. I have a 
graph structure and I want to delete all child Nodes that are connected to me 
(the root Node) with a "cascaded=true" property on my Edge object.

I have the following entities (source code):

@Entity
@Table(name = "NODE")
public class Node implements ICopyable {
   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="NodeSeq")
   @SequenceGenerator(name="NodeSeq",sequenceName="NODE_ID_SEQ", 
allocationSize=50)
   @Column(name = "ID")
   private long id;

   @Version
   @Column(name = "VERSION")
   private int version;

   @OneToMany(mappedBy = "to", fetch = FetchType.LAZY, cascade = 
CascadeType.ALL, orphanRemoval = true)
   private Set<Edge> inputs = new HashSet<Edge>();

   @OneToMany(mappedBy = "from", fetch = FetchType.LAZY, cascade = 
CascadeType.ALL, orphanRemoval = true)
   private Set<Edge> outputs = new HashSet<Edge>();
}

@Entity
@Table(name = "EDGE")
public class Edge {
   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EdgeSeq")
   @SequenceGenerator(name = "EdgeSeq", sequenceName = "EDGE_ID_SEQ", 
allocationSize = 50)
   @Column(name = "ID")
   private long id;

   @Version
   @Column(name = "VERSION")
   private int version;

   @Basic(fetch = FetchType.LAZY)
   @Column(name = "CASCADED", updatable = false)
   private boolean cascaded;

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "FROM_ID", nullable = false, updatable = false)
   private Node from;

   @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
   @JoinColumn(name = "TO_ID", nullable = false, updatable = false)
   private Node to;
}

The code I execute to select all child Nodes is:

   private static final String QUERY_OUTPUTS = "SELECT n FROM Edge e JOIN e.to 
AS n WHERE e.from = :theNode AND e.cascaded = " + true;

   private Set<Node> fetchOutputs(Node current, Set<Node> previous) {
      if (previous.contains(current)) {
         // Already fetched
         return previous;
      }
      previous.add(current);

      Query query = getEntityManager().createQuery(QUERY_OUTPUTS);
      query.setParameter("theNode", current);
      List<?> outputs = query.getResultList();
      for (Object output : outputs) {
         // ClassCastException here but only the second time it is called?!
         fetchOutputs((Node) output, previous);
      }

      return previous;
   }

The strange thing is that I get a ClassCastException in the fetchOutputs method 
(in loop) but only the second time I get there (recursive method).

First select my root Node:
[2009-04-19 10:53:45,937] DEBUG org.jenmo.core.repository.DefaultDeleteAction 
SELECT n FROM Node n WHERE n.id = :theId 
3558  JenmoPU  TRACE  [main] openjpa.jdbc.SQL - <t 236054577, conn 966979630> 
executing prepstmnt 254232091 SELECT t0.ID, t0.VERSION, t0.NODETYPE_ID FROM 
NODE t0 WHERE (t0.ID = ?) [params=(long) 50]
3559  JenmoPU  INFO   [main] openjpa.Runtime - Query "SELECT n FROM Node n 
WHERE n.id = :theId" is excluded temporarily due to "Excluded by user". 

Then select "child" Nodes (fetchOutputs method):
[2009-04-19 10:53:45,944] DEBUG org.jenmo.core.repository.DefaultDeleteAction 
SELECT n FROM Edge e JOIN e.to AS n WHERE e.from = :theNode AND e.cascaded = 
true 
3576  JenmoPU  TRACE  [main] openjpa.jdbc.SQL - <t 236054577, conn 966979630> 
executing prepstmnt 1742602365 SELECT t1.ID, t1.VERSION, t1.NODETYPE_ID FROM 
EDGE t0 INNER JOIN NODE t1 ON t0.TO_ID = t1.ID WHERE (t0.FROM_ID = ? AND 
t0.CASCADED = ?) [params=(long) 50, (boolean) true]
[2009-04-19 10:53:45,976] DEBUG org.jenmo.core.repository.DefaultDeleteAction 
SELECT n FROM Edge e JOIN e.to AS n WHERE e.from = :theNode AND e.cascaded = 
true 
3596  JenmoPU  TRACE  [main] openjpa.jdbc.SQL - <t 236054577, conn 966979630> 
executing prepstmnt 631900693 SELECT t1.ID, t1.VERSION, t1.NODETYPE_ID FROM 
EDGE t0 INNER JOIN NODE t1 ON t0.TO_ID = t1.ID WHERE (t0.FROM_ID = ? AND 
t0.CASCADED = ?) [params=(long) 100, (boolean) true]

And the exception:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to 
org.jenmo.core.domain.Node
        at 
org.jenmo.core.repository.del.NodeDeleteAction.fetchOutputs(NodeDeleteAction.java:87)
        at 
org.jenmo.core.repository.del.NodeDeleteAction.fetchOutputs(NodeDeleteAction.java:87)
        at 
org.jenmo.core.repository.del.NodeDeleteAction.delete(NodeDeleteAction.java:103)
        at 
org.jenmo.core.repository.del.NodeDeleteAction.delete(NodeDeleteAction.java:1)
        at org.jenmo.core.repository.DefaultDaoJPA.delete(DefaultDaoJPA.java:77)
        at 
org.jenmo.core.domain.TestDbDeleteBug.testDelete(TestDbDeleteBug.java:69)

Indeed this is what I have in my fetchOutputs method:

first time (OK): org.jenmo.core.domain.n...@26556949(pk=100)
second time (!OK): [150, 1, 10]

Thanks a lot for your help!
andiqo
-- 
View this message in context: 
http://n2.nabble.com/select-issue-in-recursive-method--tp2658588p2658588.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to