Play Framework Ebean two ManyToMany relations return same data

385 views Asked by At

My code looks like this:

@Entity
public class Document extends Model
{
    @Id
    private Long id;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "developers")
    private Set<Tester> developers = new HashSet<>();

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "testers")
    private Set<Tester> testers = new HashSet<>();
}

I am using the JoinTable annotation otherwise I end up with the same join table for the many-to-many relation. This works well and I get two tables generated (developers and testers).

I am even able to save the data properly. Setting the developers and/or testers and then saving the document entity works properly.

Now the problem is that the moment I do something like:

Document.find.all() to get the available Documents, the developers and the testers fields return the same data, despite the data being different in the database. Doing document.getDevelopers() and document.getTesters() return the same data with getDevelopers() always hiding the getTesters() data.

Is this some bug/limitation in the Ebean ORM?

I am using Play 2.3.8

2

There are 2 answers

0
Anton Sarov On BEST ANSWER

Explicitly fetching the fields returns the proper data.

Just define own find method like this:

public static List<Document> findAll() {
    return Ebean.find(Document.class)
                .fetch("developers")
                .fetch("testers")
        .findList();
}

Still wondering if there are better ways...

0
Pasquale Iannuzzi On

I find a Solution that works for me without fetching other tables and without get exceptions..

instead using the list itself declare an entity that hold the list.

@OneToOne(cascade = CascadeType.ALL)
ForeignStories foreignStories = new ForeignStories();

where ForeignStories is..

@Entity
@Table(name="foreign_stories")
@SuppressWarnings("serial")
public class ForeignStories extends Model {
    @Id
    @NotNull
    Long id;

    @ManyToMany(cascade = CascadeType.ALL)
    List<Story> foreignStories = new ArrayList<Story>();
}