0

I use crnk (JSON-API) in java project and I have 3 questions regarding its usage with spring boot and jpa - haven't found exact implementation details in documentation.

  1. For example, I have 2 entities and respective tables:

    @Entity
    @JsonApiResource(type = "employee")
    public class Employee {
        @Id
        @JsonApiId
        private int id;
    
        private String name;
    
        @ManyToOne
        @JoinColumn(name = "typeId")
        private EmployeeType employeeType; //stored in table as typeId
    }
    
    @Entity 
    @JsonApiResource(type = "type")
    public class EmployeeType {
        @Id
        @JsonApiId
        private int id;
    
        private String typeName;
    
        private int salary;
    }
    

How should JsonApiRelation be introduced in order to be able to call "/employee/1" and "/employee/1/type" urls?

  1. For example there is one more entity.

    @Entity
    @JsonApiResource(type = "project")
    public class Project {
        @Id
        @JsonApiId
        private int id;
    
        private String supervisorName;
    
        private String projectName;
    }
    

First, I'd like to have List of Projects for each Employee, where he is a supervisor, joint by name and have it listed as attribute in Json.

Tried implementing it with @OneToMany and @JoinColumn annotations but got StackOverflowException. How could this be implemented. And second, how could this be implemented with Relation? Like "/employee/1/projects" url.

  1. How should I implement custom filtering of results for findAll method? For example, I have a List of all Employees, but I'd like to exclude some of them from the response. Which class/method should be introduced for this behaviour?
funny-shmunny
  • 87
  • 1
  • 13

1 Answers1

1
  1. @JsonApiRelation annotation should not be necessary. Crnk will detect the @ManyToOne annotation and map it accordingly.
  2. in case of crnk-jpa it is sufficient to specify all relationships in JPA. Matching JSON API relationships. So your approach seems good. What was the StackoverflowException stacktrace? (next to the examples, there are also many example entities in crnk-jpa)
  3. I would make use of a decorator. See http://www.crnk.io/documentation/#_request_filtering. RepositoryDecoratorFactory allows to place a custom repository between the caller and crnk-jpa (or any other kind of repository). There you can do any kind of modification perform (maybe) calling the "real" repository. => Will add an example for this

feel free also make open up tickets in crnk for any documentation/example clarifications.

Remo Meier
  • 141
  • 2
  • 1. Mapping to parent Resource works fine. I see EmployeeType with fields in attributes when I call "/employee/1". But when I try to call "/employee/1/projects", I get 500 error with ResourceFieldNotFoundException. Is this scenario at least valid/possible? 2. It was toString() issue, already resolved, thanks! – funny-shmunny Apr 04 '18 at 12:23
  • relationships are declared? and also visible when requesting /employee/1. – Remo Meier Apr 06 '18 at 13:34