Missing links in Spring Data Rest projection

可紊 提交于 2019-12-11 11:33:55

问题


I am working with Spring Data Rest and Hibernate in my current project. I just added projections for some entities to reduce the amount of calls to the backend when displaying information. My code looks like this:

Component.class

@NoArgsConstructor
@RequiredArgsConstructor
@EqualsAndHashCode
@Getter
@Setter
@Slf4j
@Entity
@Table(name = "components")
public class Component
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "component_id", unique = true)
    private Long id;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "component", cascade = {
            CascadeType.DETACH,
            CascadeType.PERSIST,
            CascadeType.REFRESH
    })
    private List<Parameter> parameters = new ArrayList<>();

    @NonNull
    @Column(name = "name", nullable = false)
    private String name;
}

Parameter.class

@AllArgsConstructor
@RequiredArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Table(name = "parameters")
public class Parameter
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "parameter_id", unique = true)
    private Long id;

    @NonNull
    @Column(name = "name", nullable = false, unique = true)
    private String key;

    @ManyToOne(fetch = FetchType.EAGER)
    private Component component;

    @Column(name = "type")
    private String type;
}

Projection class

@Projection(name = "withParameters", types = { Component.class })
public interface ComponentWithParameters
{
    Long getId();
    String getName();
    List<Parameter> getParameters();
}

The projection should just add the parameters to the component which it does perfectly:

{
    "name" : "New Component",
    "id" : 1,
    "parameters" : [ {
        "id" : 1,
        "key" : "Parameter_2",
        "type" : null
    }, {
        "id" : 2,
        "key" : "Parameter_19",
        "type" : null
    }, {
        "id" : 18,
        "key" : "Parameter_9",
        "type" : null
    } ],
    "_links" : {
        "self" : {
        "href" : "https://localhost:8020/api/components/1"
        },
        "component" : {
        "href" : "https://localhost:8020/api/components/1{?projection}",
        "templated" : true
        },
        "parameters" : {
        "href" : "https://localhost:8020/api/components/1/parameters{?projection}",
        "templated" : true
        }
    }
}

I was expecting the parameters in the list to have all the links that Spring Data Rest normally applies. Something like this:

{
    "name" : "New Component",
    "id" : 1,
    "parameters" : [ {
        "id" : 1,
        "key" : "Parameter_2",
        "type" : null,
        "_links":{  
            "self":{  
               "href":"http://localhost:8080/api/parameters/1{?projection}",
               "templated":true
            },
            "component":{  
               "href":"http://localhost:8080/api/parameters/1/component"
            }
         }
    }, {
        "id" : 2,
        "key" : "Parameter_19",
        "type" : null,
        "_links":{  
            "self":{  
               "href":"http://localhost:8080/api/parameters/2{?projection}",
               "templated":true
            },
            "component":{  
               "href":"http://localhost:8080/api/parameters/2/component"
            }
         }
    }, {
        "id" : 18,
        "key" : "Parameter_9",
        "type" : null,
        "_links":{  
            "self":{  
               "href":"http://localhost:8080/api/parameters/18{?projection}",
               "templated":true
            },
            "component":{  
               "href":"http://localhost:8080/api/parameters/18/component"
            }
         }
    } ],
    "_links" : {
        "self" : {
        "href" : "https://localhost:8020/api/components/1"
        },
        "component" : {
        "href" : "https://localhost:8020/api/components/1{?projection}",
        "templated" : true
        },
        "parameters" : {
        "href" : "https://localhost:8020/api/components/1/parameters{?projection}",
        "templated" : true
        }
    }
}

I found this question which resembles mine a lot and it appearently was a bug and I was wondering if this is one again this time.

I am using the following versions:

  • spring-data-rest-core:3.1.4.RELEASE
  • spring-data-rest-webmvc:3.1.4.RELEASE
  • spring-data-commons:2.1.4.RELEASE
  • spring-data-jpa:2.1.4.RELEASE

Hopefully, somebody can help me figure this out. Thank you all in advance.

来源:https://stackoverflow.com/questions/54711501/missing-links-in-spring-data-rest-projection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!