【问题标题】:Ebean ManyToMany with bridge table find all query带有桥接表的 Ebean ManyToMany 查找所有查询
【发布时间】:2016-06-08 14:49:05
【问题描述】:

我有三个表:users(id, name, login, password), roles(id, name), user_roles(id, user_id, role_id)

这是我的代码

@Entity
@Table(name = "users")
public class User extends Model {
    @Id
    public Long id;

    public String name;

    public String login;

    public String password;

    @ManyToMany
    @JoinTable(
            name = "user_roles",
            joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")
    )
    public Set<Role> roles;

    public static Finder<Integer, User> find = new Finder<>(User.class);
}

@Entity
@Table(name = "roles")
public class Role extends Model {
    @Id
    public Long id;

    public String name;

    @ManyToMany(mappedBy = "roles")
    public List<User> users;

    public static Finder<Integer, Role> find = new Finder<>(Role.class);
}

我想显示所有具有角色的用户,例如:{"id":1, "name":"My Name", "login":"My Login", "password":"My Password", roles: [{"name":"ADMIN"}, {"name":"USER"}]} 我怎样才能做到这一点?我是 Ebean 和 ORM 的新手。感谢您的帮助。

更新

public Result all() {
    List<User> users = User.find.all();
    return ok(toJson(users));
}

但是现在我得到stackoverflow错误无限递归。

【问题讨论】:

  • 我在this 用户 Kurt Bourbaki 的问题中解决了这个问题

标签: java playframework orm ebean


【解决方案1】:

制作users.role = null,然后返回Json

【讨论】:

    猜你喜欢
    • 2013-06-12
    • 2020-09-24
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 2021-12-21
    • 1970-01-01
    相关资源
    最近更新 更多