JPA:未映射的多对多关系

时间:2014-05-13 11:18:06

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

我有2个班级,他们之间有多对多的关系。类是客户端和角色:

@Entity
@Table(name="iq_role")
public class Role extends IdEntity {

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "roles")
    private Set<Client> clients = new LinkedHashSet<>();
} 

@Entity
@Table(name="iq_client")
public class Client extends DeletableEntity {

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "iq_client_roles", joinColumns = @JoinColumn(name="iq_client", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name="iq_role", referencedColumnName = "id"))
    private Set<Role> roles = new HashSet<>();
}

当我选择一个特定的客户端并加入获取他的角色时,这就是SQL(由Hibernate生成):

select
  client.id,
  role.id
from
  iq_client client
  left outer join
    iq_client_roles client_roles
      on client.id = client_roles.iq_client
  left outer join
    iq_role role
      on client_roles.iq_role = role.id
where
  client.id=1
iq_client中的

我在表iq_role中有ID为3的记录我有ID 1和表iq_client_roles的记录我有记录3,1。 当我复制这个生成的SQL并从控制台运行它时,我得到了正确的结果,但是hibernate显然无法将结果映射到对象中。 知道为什么吗?

0 个答案:

没有答案