本文介绍了阿瓦耶·伊比恩(Avaje Ebean). ManyToMany延迟BeanSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Play Framework 2.0(使用Ebean作为ORM)编写小型应用程序.所以我需要User类和UserGroup类之间的多对多关系.这是一些代码:

I am writing small app, using Play Framework 2.0 which uses Ebean as ORM.So I need many-to-many relationship between User class and UserGroup class.Here is some code:

@Entity
public class User extends Domain {

    @Id
    public Long id;
    public String name;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    public Set<UserGroup> groups = new HashSet();
}



@Entity
public class UserGroup extends Domain {

    @Id
    public Long id;     
    public String name;

    @ManyToMany(mappedBy="groups")
    public Set<User> users = new HashSet();
}

数据库方案生成器使用中间表为该代码生成良好的方案,并且一切正常,直到我使用多对多.

Database scheme generator generates good scheme for that code with intermediate table and all work quite ok, till I using many-to-many.

所以我要在一个请求中添加组:

So I am adding group in one request:

user.groups.add(UserGroup.find.byId(groupId));
user.update();

,然后尝试将它们输出到另一个系统:System.out:

And trying output them to System.out in another:

System.out.println(user.groups);

这将返回:

快速搜索显示BeanSet是Ebean的延迟加载容器.但是似乎它无法正常工作,或者我错过了一些重要的事情.

Quick search show that BeanSet is lazy-loading container from Ebean. But seems like it doesn't work in proper way or I missed something important.

关于我做错了什么吗?

推荐答案

您需要手动保存关联

user.groups.add(UserGroup.find.byId(groupId));
user.saveManyToManyAssociations("groups");
user.update();

这篇关于阿瓦耶·伊比恩(Avaje Ebean). ManyToMany延迟BeanSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 01:26