Недостающие ссылки в проекции Spring Data Rest - PullRequest
0 голосов
/ 15 февраля 2019

Я работаю с Spring Data Rest и Hibernate в моем текущем проекте.Я только добавил прогнозы для некоторых объектов, чтобы уменьшить количество обращений к бэкэнду при отображении информации.Мой код выглядит так:

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(name = "withParameters", types = { Component.class })
public interface ComponentWithParameters
{
    Long getId();
    String getName();
    List<Parameter> getParameters();
}

Проекция должнапросто добавьте параметры к компоненту, который он делает идеально:

{
    "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
        }
    }
}

Я ожидал, что параметры в списке будут иметь все ссылки, которые обычно применяется Spring Data Rest.Примерно так:

{
    "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
        }
    }
}

Я нашел этот вопрос, который очень напоминает мой, и, по-видимому, это была ошибка, и мне было интересно, если это снова на этот раз.

Я использую следующие версии:

  • 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

Надеюсь, кто-нибудь поможет мне разобратьсяэто изСпасибо всем заранее.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...