You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have noticed that Ebean always load the associations using OUTER JOINS. I've tried with @OnetoOne, @onetomany, using a FetchConfig, or FetchType.LAZY, and still it always use an OUTER JOIN.
For example, I have a User model which has many books (object and db code at the end). If I load the user like this:
User user = User.find.fetch("books", new FetchConfig().query() ).where("id = 2").findUnique();
The first query loads the user:
select t0.id c0, t0.first_name c1, t0.last_name c2
from users t0
where t0.id = 2
The second query uses an outer join :
select t0.id c0
, t1.id c1, t1.name c2, t1.user_id c3
from users t0
left outer join books t1 on t1.user_id = t0.id
where t0.id = 2
order by t0.id
However, I would expect the second query to be something like :
select t1.id c1, t1.name c2, t1.user_id c3
from books t1
where t1.user_id = 2
Question 1. The user_id is already known by the first query, so why the join is required?
Question 2. Is there some way around this?
Code for models and SQL :
@Entity
@Table(name="users")
public class User
{
@Id
public int id;
public String firstName;
public String lastName;
@OneToMany(mappedBy = "user")
public List<Book> books;
public static Finder<Integer,User> find = new Finder<Integer,User>( Integer.class, User.class );
}
@Entity
@Table(name="books")
public class Book
{
@Id
public int id;
public String name;
@ManyToOne
public User user;
public static Finder<Integer,Book> find = new Finder<Integer,Book>( Integer.class, Book.class );
}
And the SQL code is :
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`first_name` varchar(45) DEFAULT NULL,
`last_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO users VALUES(1,"Joe","Bloh");
INSERT INTO users VALUES(2,"Maria","Luis");
CREATE TABLE `books` (
`id` INT NOT NULL ,
`user_id` INT NOT NULL ,
`name` VARCHAR(45) NULL ,
PRIMARY KEY (`id`)
);
INSERT INTO books VALUES(1,1,"Joe's first book");
INSERT INTO books VALUES(2,1,"Joe's second book");
INSERT INTO books VALUES(3,2,"Maria's first book");
INSERT INTO books VALUES(4,2,"Maria's second book");
The text was updated successfully, but these errors were encountered:
A similar issue was documented in the old bug tracking system (http://www.avaje.org/bugdetail-410.html), and discussed in the google group (https://groups.google.com/forum/#!topic/ebean/V6foc1K38Lk).
Excerpt from the discussion:
I have noticed that Ebean always load the associations using OUTER JOINS. I've tried with @OnetoOne, @onetomany, using a FetchConfig, or FetchType.LAZY, and still it always use an OUTER JOIN.
For example, I have a User model which has many books (object and db code at the end). If I load the user like this:
The first query loads the user:
The second query uses an outer join :
However, I would expect the second query to be something like :
Question 1. The user_id is already known by the first query, so why the join is required?
Question 2. Is there some way around this?
Code for models and SQL :
The text was updated successfully, but these errors were encountered: