冬眠。三张桌子之间的多对多

时间:2014-08-06 07:59:08

标签: java sql hibernate orm many-to-many

大家好

今天我遇到了一些麻烦...... 我有3个表,每个表之间有多对多的关系。见下面的链接

diagram

以下实体的简短定义: 支数

@Entity
@Table(name = "systembranches")
public class Systembranches {
    private long id;
    ...
    private Set<Systemusers> systemuserses;
    private Set<Userroles> userroleses;
    ...
    @ManyToMany
    @JoinTable(
            name = "branchroles",
            joinColumns = @JoinColumn(
                    name = "BranchId",
                    referencedColumnName = "Id",
                    nullable = false),
            inverseJoinColumns = @JoinColumn(
                    name = "UserId",
                    referencedColumnName = "Id",
                    nullable = false))
    public Set<Systemusers> getSystemuserses() {
        return systemuserses;
    }

    public void setSystemuserses(Set<Systemusers> users) {
        this.systemuserses = users;
    }

    @ManyToMany (mappedBy = "systembranches")
    public Set<Userroles> getUserroleses() {
        return userroleses;
    }

    public void setUserroleses(Set<Userroles> userroleses) {
        this.userroleses = userroleses;
    }
}

用户

@Entity
public class Systemusers {
    private long id;
    ...
    private Set<Userroles> userroles;
    private Set<Systembranches> systembrancheses;
    ...
    @ManyToMany
    @JoinTable(
            name = "branchroles",
            joinColumns = @JoinColumn(
                    name = "UserId",
                    referencedColumnName = "Id",
                    nullable = false),
            inverseJoinColumns = @JoinColumn(
                    name = "RoleId",
                    referencedColumnName = "Id",
                    nullable = false))
    public Set<Userroles> getUserroles() {
        return userroles;
    }

    public void setUserroles(Set<Userroles> roles) {
        this.userroles = roles;
    }

    @ManyToMany (mappedBy = "systemuserses")
    public Set<Systembranches> getSystembranches() {
        return systembrancheses;
    }

    public void setSystembranches(Set<Systembranches> systembrancheses) {
        this.systembrancheses = systembrancheses;
    }

}

角色

@Entity
public class Userroles {
    private long id;
    ...
    private Set<Systemusers> systemusers;
    private Set<Systembranches> systembrancheses;

    ...

    @ManyToMany (mappedBy = "userroles")
    public Set<Systemusers> getSystemusers() {
        return systemusers;
    }

    public void setSystemusers(Set<Systemusers> systemusers) {
        this.systemusers = systemusers;
    }

    @ManyToMany
    @JoinTable(
            name = "branchroles",
            joinColumns = @JoinColumn(
                    name = "RoleId",
                    referencedColumnName = "Id",
                    nullable = false),
            inverseJoinColumns = @JoinColumn(
                    name = "BranchId",
                    referencedColumnName = "Id",
                    nullable = false))
    public Set<Systembranches> getSystembranches() {
        return systembrancheses;
    }

    public void setSystembranches(Set<Systembranches> systembrancheses) {
        this.systembrancheses = systembrancheses;
    }

}

您可以看到通过第四个表( branchroles )加入的所有表格在我的应用程序中没有实体。 该表包含以下数据:

id UserID BranchId RoleId
1    2        3        2
2    3        3        3
3    1        1        4
5    4        2        4
8    5        2        4
9    6        2        2

我需要获取某些实体的列表(例如角色)取决于两个Set&lt;&gt;此实体内的属性(Set<Systemusers>Set<Systembranchs>

SQL上的

应该如下所示:

SELECT * FROM Userroles r
JOIN branchroles connector ON r.id = connector.RoleId
JOIN systembranches b ON b.id = connector.BranchId
JOIN systemusers u ON u.id = connector.UserId
WHERE u.id = 3 and b.id = 2

在我编写的Java代码中

getSessionFactory().getCurrentSession()
                   .createCriteria(Userroles.class)
                   .createCriteria("systemusers")
                   .add(Restrictions.eq("id", userId))
                   .createCriteria("systembranches")
                   .add(Restrictions.eq("id", branchId))
                   .list();

和其他类似的变体,如

.createCriteria(Userroles.class, "r")
                   .createCriteria("r.systemusers", "u", JoinType.INNER_JOIN)

但每次Hibernate生成这样的代码

Hibernate SQL

正如您在此SQL中可以看到一个额外的JOIN,结果是下一个JOIN表达式中的无效依赖

我该如何解决?我想使用Criteria API编写代码,没有一对多关系,没有branchroles实体,如果可能的话。你能帮忙吗?

由于

1 个答案:

答案 0 :(得分:-1)

您在数据库结构中缺少某些内容。

如果您正在寻找多对多关系,那么可以在两个表之间进行。

一个表将具有您拥有的表的映射详细信息。

如果你想要三个表,那么你需要考虑你的JoinTable作为应用程序中的实体表。

将其映射到具有多对多关系的第三个表。

希望这会对你有所帮助