Hibernate ManyToMany映射

时间:2012-11-22 11:29:52

标签: java hibernate

我有ManyToMany这样的映射:

@XmlTransient
    @ManyToMany(cascade = {CascadeType.ALL})
    @JoinTable(name = "users_clients",
            joinColumns = {@JoinColumn(name = "user_id")},
            inverseJoinColumns = @JoinColumn(name = "client_id"))
    public List<Client> getClients() {
        return clients;
    }

另一方:

@ManyToMany(mappedBy = "clients")
    private List<User> users = new ArrayList<User>();

因为你可以看到我有JoinColumn name = user_idclient_id但是hibernate mapps这个列的名称为userS_id和clientS_id作为它们的表名。为什么会这样?有什么建议吗?

1 个答案:

答案 0 :(得分:0)

尝试在@JoinColumn注释中指定列名称。你还错过了inverseJoinColumns

周围的大括号
    @JoinTable(name = "users_clients",
      joinColumns = {
        @JoinColumn(name = "user_id", referencedColumnName="user_id")},
          inverseJoinColumns = {
        @JoinColumn(name = "client_id", referencedColumnName="client_id")})