Bug 408 : Join fetching related objects that use inheritance causes wrong SQL
Priority 
High
Reported Version 
 
Logged By 
Ariel Scarpinelli
Status 
Fixed
Fixed Version 
3.x
Assigned To 
 
Product 
Ebean - core
Duplicate Of 
 
Created 
28/06/2012
Updated 
28/06/2012
Type 
Bug
 
Attachments 
No attachments

Consider the following entities:

// Profile.java
@Entity
public class Profile {

@ManyToOne(cascade = CascadeType.ALL)
public Picture picture;
public String slug;
...

}

// Media.java
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType=DiscriminatorType.STRING, name = "type")
public class Media {

public String url;
...
}

// Picture.java
@Entity
@DiscriminatorValue("Picture")
public class Picture extends Media {
...
}


If you try to fetch a Profile with its Picture using the following sentence:

Profile aProfile = Ebean.find(Profile.class).fetch("picture").where().eq("slug", slug).findUnique();

It generates the following SQL:

select (list of columns...)
from profile t0
left outer join media t1 on t1.id = t0.picture_id
where t1.type = 'Picture' and t0.slug = ?

Which works OK, but fails to load a Profile when it has no related Picture (a.k.a null), which is a valid case.

The generated SQL should be instead:

select (list of columns...)
from profile t0
left outer join media t1 on t1.id = t0.picture_id and t1.type = 'Picture'
where t0.slug = ?

or well

select (list of columns...)
from profile t0
left outer join media t1 on t1.id = t0.picture_id
where (t1.type = 'Picture' or t1.type is null) and t0.slug = ?

Hope this makes sense.

Thanks!

 
woResponse

Upload a file