JPA, a little more complex example.

This week I was writing a tool to extract reports from Mantis Bugtrack using JPA and EJB3. Since I´m not good in JPQL (sonething like a SQL for objects) the search for examples began. The problem was that all examples were too simple, here´s some notes one a more complex query and how was done.

Mantis is a bugtrack tool and allows a bug relationship and custom fields, my report will make use of both. Below is a simplified diagram of the database tables involved in this process.


The scenario was 2 projects. The first (I will call it A) will open bugs for the second (from now called B) the bugs will be related. Example: A operator opens a bug in project A, after that n other bugs are opened in project B, the relation between bugs is B are child of A. This relation is explicitly set using Mantis relationship features which will be reflected in our bug_relationship table. The last thing on this problem is that a custom field value should be retrieved too for a pivot transformation. Here's another diagram showing the relationship:


To not extend too much this post I will show a simplified version of the mapped entities. Gets, Sets, packages and imports were omitted for the sake of the reader.

public class Project implements Serializable {
 private int id;

 private String name;

 private Collection<bug> bugs;

    // gets and sets omitted.

public class Bug implements Serializable {
 private int id;

 private int projectId;

 private Project project;

      //gets and sets omitted

public enum BugRelationType {

 private final int relationship;

 BugRelationType(int rel) {
  this.relationship = rel;

 public Integer toInteger() {
  return Integer.valueOf(this.relationship);


public class CustomField implements Serializable {

 private int id;

 private String name;

      // gets and sets omitted


public class CustomFieldString implements Serializable {

 private CustomFieldStringPK pk;

 private String value;

public class CustomFieldStringPK implements Serializable {

 private static final long serialVersionUID = 1L;

 private Integer fieldId;

 private Integer bugId;

Alright, with those entities in hand how we will compose a good JPQL without access the database more than one time? It's possible? Is possible to join all these entities? Those were my questions. The answer is affirmative, but to do so is not straight forward (at least for me) like when I'm writing SQL.

Here's is the final query:

        bt, cs.value
        CustomFieldString cs,
        CustomField cf,
        Project ps, IN (ps.bugs) bs,
        Project pt, IN (pt.bugs) bt,
        BugRelationship rel
        ps = :projSource
        AND pt = :projTarget
        AND rel.sourceBugId =
        AND rel.destinationBugId =
        AND   =
        AND =
        AND cf = :customField
        AND rel.relationshipType = :relType

Now just fill up the named parameters and you have it, a fairly complex JPQL query.

That's it.

Published in Jul 28, 2009