0

I have a table with reflexive association:

A container could be associated to a parent (Column container_id). A container can have containers as child.

@Entity
public class Container implements Serializable {

  @ManyToOne
  @JoinColumn(name = "container_id", nullable = true, referencedColumnName = "original_id")
  private Container containedIn;

  @Column(name = "original_id")
  private long originalId;

  @OneToMany(mappedBy = "containedIn")
  private List<Container> contains = new ArrayList<>();

... 

Problem:

The generated request is :

  Hibernate: select * from ( 
  select *  
  from container con0_, container con1_ 
  where con0_.container_id=con1_.original_id 
  order by con1_.code ) where rownum <= ?

where i'm expecting left outer join:

from container con0_ left outer join container con1_ 
      on con0_.container_id=con1_.original_id.

Container that doesn't have parent (Root Box CON0000007) are not retrieved as left join is not generated by Hibernate:

ID | ORIGINAL_ID| CODE | TYPE | CONTAINER_ID (Parent)

1 | 2 | CON000000002 | Package | 12

2 | 4 | CON000000004 | Package | 10

4 | 6 | CON000000006 | Package | 11

5 | 8 | CON000000008 | Package | 10

7 | 10 | CON0000002 | Box | 15

8 | 11 | CON0000003 | Box | 15

9 | 12 | CON0000004 | Box | 15

12 | 15 | CON0000007 | Root Box | NULL

Ahmed MANSOUR
  • 1,861
  • 1
  • 22
  • 32

1 Answers1

0

The model seems fine. It looks like you don't load children level in your query. And by default you have lazy load, so hibernate doesn't queries for children. If this is the case look here for examples Hibernate: best practice to pull all lazy collections

Community
  • 1
  • 1
Evgen
  • 998
  • 2
  • 9
  • 21
  • I updated my question with more details. the problem is Container that doesn't have parent (Root Box CON0000007) are not retrieved and normally hibernate should generate a left join relationship. – Ahmed MANSOUR Mar 30 '16 at 09:09