Hello,

Thank you for your response. It worked. However, it works just when loading
the graph from the first state. If i try to load the flow directly it
doesn't work.

This is what i did:

@Entity
@FetchGroups( {
                @FetchGroup(name = "State_outgoingTransitions", attributes = {
@FetchAttribute(name = "outgoingTransitions", recursionDepth = -1) }),
                @FetchGroup(name = "State_incomingTransitions", attributes = {
@FetchAttribute(name = "incomingTransitions", recursionDepth = -1) }) })
public class State implements Serializable {
...

@Entity
@FetchGroups( {
                @FetchGroup(name = "State_outgoingTransitions", attributes = {
@FetchAttribute(name = "fromState", recursionDepth = -1) }),
                @FetchGroup(name = "State_incomingTransitions", attributes = {
@FetchAttribute(name = "toState", recursionDepth = -1) }) })
public class Transition implements Serializable {
...


...
System.out.println("FLOW: LAZY LOADING: ");
EntityManager em = emf.createEntityManager();
Flow f = em.find(Flow.class, 2L);
System.out.println(f);
em.close();
                        

System.out.println("FLOW: FETCH GROUPS: ");
em = emf.createEntityManager();
OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
oem.getFetchPlan().addFetchGroups("State_outgoingTransitions",
"State_incomingTransitions");
f = em.find(Flow.class, 2L);
                
//close persistence context
em.close();
System.out.println(f);
                
                
System.out.println("STATE: FETCH GROUPS");
em = emf.createEntityManager();
oem = OpenJPAPersistence.cast(em);
oem.getFetchPlan().addFetchGroups("State_outgoingTransitions",
"State_incomingTransitions");
State s = em.find(State.class, 4L);

//close persistence context
em.close();
        
//build the flow object
f = new Flow();
f.setInitialState(s);
System.out.println(f);
...

And this is the output:

FLOW: LAZY LOADING: 
[s1, s2, s4, s5, s3]
[t12, t24, t45, t23, t34, t31, t32, t21]
FLOW: FETCH GROUPS: 
[s1, s2, s3]
[t12, t24, t23, t34, t31, t32, t21]
STATE: FETCH GROUPS
[s1, s2, s4, s5, s3]
[t12, t24, t45, t23, t34, t31, t32, t21]


As you can see, when trying to load the flow directly with f =
em.find(Flow.class, 2L) the result is the same as before.

Anyway, i think i can handle it from here. Thanks a lot for your help.


Fay Wang wrote:
> 
> Hi,
>         Since this is an indirect recursion, i.e., from State ->
> Transition -> State -> Transition -> ...
> you need to add the recursionDepth in both State and Transition as shown
> below:
> 
> (1) State.java
> @Entity
> @FetchGroups( {
>         @FetchGroup(name = "State_OutgoingTransitions", 
>                 attributes = {...@fetchattribute(name =
> "outgoingTransitions", recursionDepth = 10) }),
>         @FetchGroup(name = "State_IncomingTransitions", 
>                 attributes = {...@fetchattribute(name =
> "incomingTransitions", recursionDepth = 10) }) })
>                 
> public class State implements Serializable {
>     @Id
>     @GeneratedValue(strategy = GenerationType.IDENTITY)
>     private Long id;
> 
>     @Column(length = 64)
>     private String name;
> 
>     @OneToMany(mappedBy = "fromState",
>              cascade = {CascadeType.PERSIST, CascadeType.MERGE,
> CascadeType.REFRESH})
>     private List<Transition> outgoingTransitions;
> 
>     @OneToMany(mappedBy = "toState", 
>             cascade = { CascadeType.PERSIST, CascadeType.MERGE,
> CascadeType.REFRESH })
>     private List<Transition> incomingTransitions;
>     
> ...
> }
> 
> (2) Transition.java
> @Entity
> @FetchGroups( {
>     @FetchGroup(name = "Transition_ToState", 
>             attributes = {...@fetchattribute(name = "toState", recursionDepth
> = 10) }),
>     @FetchGroup(name = "Transition_FromState", 
>             attributes = {...@fetchattribute(name = "fromState",
> recursionDepth = 10) }) })
>             
> 
> public class Transition implements Serializable {
>     @Id
>     @GeneratedValue(strategy = GenerationType.IDENTITY)
>     private Long id;
> 
>     @Column(length = 64)
>     private String name;
> 
>     @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE,
>             CascadeType.REFRESH })
>     @JoinColumn(name = "FROM_STATE_ID", referencedColumnName = "ID")
>     private State fromState;
> 
>     @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE,
>             CascadeType.REFRESH })
>     @JoinColumn(name = "TO_STATE_ID", referencedColumnName = "ID")
>     private State toState;
> ...
> }
> 
> 
> (3) When doing the findBy operation, add the fetch groups to the fetch
> plan:
> 
>         EntityManager em = emf.createEntityManager();
>         OpenJPAEntityManager oem = OpenJPAPersistence.cast (em);
>         JDBCFetchPlan jfp = (JDBCFetchPlan)oem.getFetchPlan();
>         jfp.addFetchGroup("State_OutgoingTransitions");
>         jfp.addFetchGroup("State_IncomingTransitions");
>         jfp.addFetchGroup("Transition_ToState");
>         jfp.addFetchGroup("Transition_FromState");
>         
>         State s1 = em.find(State.class, 2);
> 
> The resulting s1 should contain the complete graph.
> 
> Regards,
> Fay
> 

-- 
View this message in context: 
http://n2.nabble.com/Fetchgroups-recursion-problem-tp3874382p3901554.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to