Spring数据REST为onetomany和manytomany关系链接空结果

时间:2015-01-02 20:48:17

标签: rest spring-data

我有两个课程如下

@Entity
@Table(name = "Employee", schema = "...", catalog = "...")
public class EmployeeEntity implements Serializable, UserDetails {

    @Id()
    @Column(name = "EmployeeID", nullable = false, insertable = true, updatable = true, length = 20)

    @Basic
    @Column(name = "UserName", nullable = true, insertable = true, updatable = true, length = 20)
    private String username;


    @OneToMany
    @JoinColumn(name="UserName")    
    private Set<EmployeesGroupsEntity> employeeGroups;



 //Getter and setters
}

@Entity
@Table(name = "EmployeesGroups", schema = "...", catalog = "...")
public class EmployeesGroupsEntity implements  Serializable {

    @Id
    @Column(name = "UserName", nullable = false, insertable = true, updatable = true, length = 20)
    private String username;

    @Basic
    @Column(name = "Group", nullable = false, insertable = true, updatable = true, length = 255)    
    private String groups; 

}

现在,要访问对象,我有弹簧数据休息库,如下所示

@RepositoryRestResource(collectionResourceRel = "Employee", path = "Employee")
public interface IEmployeeRepository extends PagingAndSortingRepository<EmployeeEntity, String>  {

    EmployeeEntity getByUsername(String userName);

    EmployeeEntity getByEmailAddress(String emailAddress);
}

@RepositoryRestResource(collectionResourceRel = "EmployeesGroups", path = "EmployeesGroups")
public interface IEmployeesGroupsRepository extends PagingAndSortingRepository<EmployeesGroupsEntity,String> {

}

通过上述设置,我尝试访问网址

http://localhost:8080/Employee/12345

我成功得到了如下结果

{
  "employeeId" : "12345",
  "username" : "firstx",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/Employee/12345"
    },
    "employeeGroups" : {
      "href" : "http://localhost:8080/Employee/12345/employeeGroups"
    }
  }
}

下一步,当我访问网址

 http://localhost:8080/Employee/12345/employeeGroups

我得到的输出为

{ }

随后,我也尝试使用标题“text / uri-list”。当我这样做时,我得到一个204的响应代码。

请帮我解决问题。

由于

1 个答案:

答案 0 :(得分:3)

放弃之后..在找到另一个问题后找到答案。

我只需要注释与RestResource的关系(exported = true)

@Entity
@Table(name = "Employee", schema = "...", catalog = "...")
public class EmployeeEntity implements Serializable, UserDetails {

    @Id()
    @Column(name = "EmployeeID", nullable = false, insertable = true, updatable = true, length = 20)

    @Basic
    @Column(name = "UserName", nullable = true, insertable = true, updatable = true, length = 20)
    private String username;


    @OneToMany
    @RestResource(exported = true)
    @JoinColumn(name="UserName")    
    private Set<EmployeesGroupsEntity> employeeGroups;



 //Getter and setters
}