438 words

LEFT (OUTER) JOIN problem when mappedBy is defined

#1
2011-07-01 07:48

Hi all,

I want to get objects with their lists, even when the lists are empty. So I've witten this query:

"SELECT i FROM Invoice i LEFT OUTER JOIN i.itemList items"

But this query never returns invoices without items! Only invoices with items.

When I remove mappedBy from @OneToMany annotation, LEFT JOIN works correctly.

Is this normal behaviour?

Thank you

Michael

 

Sample application

public class JavaApplication {
    public static void main(String[] args) {
        com.objectdb.Enhancer.enhance("javaapplication.*");
 
        EntityManagerFactory emf =
            Persistence.createEntityManagerFactory("$objectdb/db/test.odb");
        EntityManager em = emf.createEntityManager();
 
        em.getTransaction().begin();
        Invoice i1 = new Invoice("1");
        em.persist(i1);
        Invoice i2 = new Invoice("2");
        i2.getItemList().add(new InvoiceItem(i2));
        em.persist(i2);
        Invoice i3 = new Invoice("3");
        em.persist(i3);
        em.getTransaction().commit();
 
        Query query = em.createQuery(
            "SELECT i FROM Invoice i LEFT OUTER JOIN i.itemList items");
        List resultList = query.getResultList();
        System.out.println(resultList);
 
        em.close();
        emf.close();
    }
}
 
 
@Entity
public class Invoice {
 
    @OneToMany(mappedBy = "invoice", fetch= FetchType.EAGER, orphanRemoval=true, cascade= CascadeType.ALL)
    private List<InvoiceItem> itemList = new ArrayList<InvoiceItem>();
    private String name;
    public Invoice(String name){
        this.name = name;
    }
    public List<InvoiceItem> getItemList() {
        return itemList;
    }
    @Override
    public String toString() {
        return name;
    }
}
 
 
@Entity
public class InvoiceItem{
 
    @ManyToOne
    private Invoice invoice;
 
    InvoiceItem(Invoice i) {
        this.invoice = i;
    }
}
anaq
anaq's picture
Joined on 2011-06-08
User Post #15
#2
2011-07-01 08:54

I've forgotten the version - I've tested with 2.2.7 build 5 and 2.2.7 build 8

anaq
anaq's picture
Joined on 2011-06-08
User Post #16
#3
2011-07-01 14:40

It was an ObjectDB bug. Please try build 2.2.7_09.

Thank you for this report.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #398

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel