Play Framework Ebean two ManyToMany relations return same data

流过昼夜 提交于 2019-12-24 13:42:36

问题


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


回答1:


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...




回答2:


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>();
}


来源:https://stackoverflow.com/questions/30935374/play-framework-ebean-two-manytomany-relations-return-same-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!