小弟配置的是 角色 和 权限的 多对多,代码如下 。大侠帮帮忙权限:Resource 大致如下@Entity
@Table(name = "TBL_RESOURCE", catalog = Constants.DB_NAME)
public class Resource {
         ........
        public Set<role> roles ;       
 
         ManyToMany(mappedBy = "resources" , fetch = FetchType.EAGER) 
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
         ........

权限:role  大致如下
@Entity
@Table(name = "TBL_ROLE", catalog = Constants.DB_NAME)
public class Role { private Set<Resource> resources = new HashSet<Resource>() ;        @ManyToMany(targetEntity = Resource.class, fetch = FetchType.EAGER)   
@JoinTable(
name = "TBL_ROLE_RESOURCE", 
joinColumns = @JoinColumn(name = "ROLE_ID"), 
inverseJoinColumns = @JoinColumn(name = "RESOURCE_ID"))
public Set<Resource> getResources() {
return resources;
} public void setResources(Set<Resource> resources) {
this.resources = resources;
}}配置就是这些了。启动没问题 , 但是小弟查询的时候出现如下问题:
  String hql = "FROM Resource" ;
  createQuery(hql).list() ;这样子查询就说 resource is not mapped ,,,,
小弟 实在没办法了 。。找了N久了 。。 。。帮帮忙。

解决方案 »

  1.   

    Resource:
    ManyToMany(mappedBy = "resources" , fetch = FetchType.EAGER)
      

  2.   

    两个实体都要配置@JoinTable
    你只有Role 的注解中有@JoinTableResource实体中 getRoles()方法上面除了要@ManyToMany注解外,也要@JoinTable
    并将joinColumns 和 inverseJoinColumns属性的值调换一下。与Role中的相反。
      

  3.   

    没必要这么复杂,举一个我以前的例子角色和权限的关系,是多对多角色表中有权限的多对多的声明@ManyToMany(fetch = FetchType.EAGER)
    private Set<Authority> authority;权限表中没有声明Role(没有该属性)