Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ebean loads @OneToMany associations using OUTER JOIN #48

Closed
davidonlaptop opened this issue Sep 4, 2013 · 1 comment
Closed

Ebean loads @OneToMany associations using OUTER JOIN #48

davidonlaptop opened this issue Sep 4, 2013 · 1 comment
Labels
Milestone

Comments

@davidonlaptop
Copy link

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:

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");
@rbygrave rbygrave added this to the 3.3.2 milestone Apr 25, 2014
@rbygrave
Copy link
Member

This issue was fixed in version 3.3.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants