rakpan
rakpan

Reputation: 2853

Spring data REST links empty result for onetomany and manytomany relations

I have two classes as below

@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
}

and

@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; 

}

Now, to access the objects, I have the spring data rest repositories as below

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

    EmployeeEntity getByUsername(String userName);

    EmployeeEntity getByEmailAddress(String emailAddress);
}

and

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

}

with the above setup, I tried to access the URL

http://localhost:8080/Employee/12345

and I successfully got the result as below

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

As a next step, when I access the URL

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

i get the output as

{ }

Subsequently, I also tried with the header "text/uri-list". When i do this I get a response code of 204.

Please help me in resolving the issue.

Thanks

Upvotes: 1

Views: 677

Answers (1)

rakpan
rakpan

Reputation: 2853

After giving up.. Found the answer after stumbling upon another question.

I just had to annotate the relationship with 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
}

Upvotes: 3

Related Questions