Cyril N.
Cyril N.

Reputation: 39879

OneToMany returns 1 result when no result should be returned?

I have a two models, the second is mapping to the same first model twice, with specific values (it has the role of a ManyToMany table with added values) :

public class ModelB extend Model {
    public ModelA parent;
    public ModelA child;
    public String value;
    public boolean verified = true;
}

In ModelA :

@OneToMany(mappedBy="child")
List<ModelB> items;

If no entries are in the database for ModelB and I do a modelA.items.size() => 1!

Why 1 ? It should be 0.

This results in an error regarding the boolean and some other unexplained.

How can I fix it?

Upvotes: 0

Views: 257

Answers (2)

H. Koehorst
H. Koehorst

Reputation: 11

I ran into the same issue and was able to solve it by adding an identity column to the secondary table (ModelB). I did not investigate the cause but I suppose Ebean needs a primary key on the table in these kind of situations.

Upvotes: 0

Morghus
Morghus

Reputation: 11

http://www.avaje.org/ebean/introquery_joinquery.html

It appears that when joining using a "fetch join" as described in the link above, presumably this is the behavior of Ebean currently. However you can work around that by performing a "Query join" for the OneToMany relation like this:

List<Order> orders =   
Ebean.find(Order.class)  
    .fetch("customer", new FetchConfig().query())  
    .findList(); 

Upvotes: 1

Related Questions