Tree with jpa container source of 2 different model objects

I am using a Tree for a side menu in my application. For this tree I have two domain objects (TestCase, TestSession). The desired output should be a tree that looks like this:

-TestCaseA (title)
-TestSession1 (title)
-TestSession2 …
-TestCaseB
-TestSession1
-TestSession2

The relevant parts of the models are designed as follows (but can be changed if needed)

class TestCase {
   int id;
   string title;
   Set<TestSession> sessions;
...
}

class TestSession {
   int id;
   string title;
   TestCase parent;
...
}

I have no problem getting test cases as the data source for the tree and easily displays all TestCases in the db, eg.

JPAContainer<TestCase> testcases... Tree tree = new Tree("", testcases); tree.setItemCaptionPropertyId("title"); My question is, as I want the root items of the tree to display the title property of the TestCase and then display the title of all the Sessions of that test case for all session instances…seeing as these are two separate domain models, how is this accomplished? Of course all the information needed can be accessed from the TestSession POJO alone, since it has it’s own title and a reference to the parent TestCase.
But how do I set the tree caption property to one thing (test case title) in the root items and to another (session title) in all it’s children items?

Thanks!

Hi Aaron,

I’ve solved something similar this way:

[code]
// Item captions must be defined explicitly
tree.setItemCaptionMode(ItemCaptionMode.EXPLICIT);

// Now fill the tree
for (…iterate through all TestCases… ) {
tree.addItem(testCase);
tree.setItemCaption(testCase, testCase.getName());

 if (testCase.getSessions().size() == 0) {
      tree.setChildrenAllowed(testCase, false);
 } else {
      // and fill the subtree
      for (TestSession testSession : testCase.getSessions()) {
           tree.addItem(testSession);
           tree.setItemCaption(testSession, testSession.getName());
           tree.setParent(testSession, testCase);
           tree.setChildrenAllowed(testSession, false);
       }
 }

}
[/code]Maybe not the most optimal, but at least it works for me.

Cheers,
Koenraad

Hey Koenraad,

thanks for the idea. yes this does seem like a possible answer. of course there come times when it is easier to have the actual jpa container bound to the tree rather than using the container to manually add items to the tree, but this could at least work.

a followup problem when implementing this, is that my model classes and db seem to have some problem.

I am using JPA annotations and the TestCase and TestSession classes relate to each other in a parent/child relationship.

@Entity
public class TestCase {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @OneToMany(mappedBy = "parentcase", fetch=FetchType.EAGER)
    private Set<TestSession> sessions;

...

@Entity
public class TestSession {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @ManyToOne(fetch = FetchType.EAGER)
    private TestCase parentcase;

The actual db structures generated look as follows (
non relevant things removed
):
TestCase table:
-id

TestSession table:
-id
-parentcase

My problem is that I get an empty set when I try to get all child sessions from the parent with something like you recommended:

testcase.getSessions()

I know entries exist in the child database pointing to a parent, but the parent does not seem to see the relation and I’d like to be able to retrieve results from the parent side.

any suggestions? thanks