Creating a table in ebean with two lists connecting to another table

633 views Asked by At

I'm using Ninja framework and I'm trying to create a table that has two lists of other tables of the same kind. The problem is that all the content from the other list is also inside of the other list.

Main:

test();

List<Foo> foos = Foo.find.all();

for(Foo foo : foos){
    System.out.println("Printing bars1, size: " + foo.getBars1().size());
    for(Bar bar : foo.getBars1()){
        System.out.println(bar.getText());
    }
    System.out.println("Printing bars2, size: " + foo.getBars2().size());
    for(Bar bar : foo.getBars2()){
        System.out.println(bar.getText());
    }
}

Function test:

private void test() {
    Foo foo = new Foo();

    Bar bar1 = new Bar();
    Bar bar2 = new Bar();

    bar1.setText("This should only be in bars1");
    bar2.setText("This should only be in bars2");

    foo.getBars1().add(bar1);
    foo.getBars2().add(bar2);

    foo.save();
}

Foo:

package models;

import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToMany;

@Entity
public class Foo extends BaseModel {

    public static final Find<Long, Foo> find = new Find<Long, Foo>() {};

    @OneToMany(cascade = CascadeType.ALL)
    private List<Bar> bars1;

    @OneToMany(cascade = CascadeType.ALL)
    private List<Bar> bars2;

    public List<Bar> getBars1() {
        return bars1;
    }

    public void setBars1(List<Bar> bars1) {
        this.bars1 = bars1;
    }

    public List<Bar> getBars2() {
        return bars2;
    }

    public void setBars2(List<Bar> bars2) {
        this.bars2 = bars2;
    }

}

Bar:

package models;

import javax.persistence.Entity;
import javax.validation.constraints.Size;

@Entity
public class Bar extends BaseModel {

    public static final Find<Long, Bar> find = new Find<Long, Bar>() {};

    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}

Print from main:

Printing bars1, size: 2
This should only be in bars1
This should only be in bars2
Printing bars2, size: 2
This should only be in bars1
This should only be in bars2

Expected:

Printing bars1, size: 1
This should only be in bars1
Printing bars2, size: 1
This should only be in bars2
2

There are 2 answers

0
OscarBcn On BEST ANSWER

It was a detected issue according: https://github.com/ebean-orm/avaje-ebeanorm/issues/445

is needed to specify a unique join name, I tried this change in your Foo class and it worked:

@Entity
public class Foo extends BaseModel {

    public static final Find<Long, Foo> find = new Find<Long, Foo>() {};

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "bars1")
    private List<Bar> bars1;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "bars2")
    private List<Bar> bars2;

    public List<Bar> getBars1() {
        return bars1;
    }

    public void setBars1(List<Bar> bars1) {
        this.bars1 = bars1;
    }

    public List<Bar> getBars2() {
        return bars2;
    }

    public void setBars2(List<Bar> bars2) {
        this.bars2 = bars2;
    }

}

log log

Hope it helps.

0
Darek On

(I can't add comment so pardon me for adding something that just might be worth considering as a full answer to your problem)

As far as I know (in Hibernate, so maybe in Ebean too), default @Entity relationship model assumes, if not specified, certain join table names for relationships. That name is concatenation of both relationship owners and _ in the middle. Maybe in this case, regardless of different class fileds name, ORM manager, while creating mentioned default table name, created same table name for both collections. In other words: maybe "underneath" - both fields are referencing to the same join table and, in this way, to the same collection.

(This is more guess because i can't test at this moment mentioned ORM behaviour...)