【问题标题】:Links to Embedded entities in Spring Data RestSpring Data Rest 中嵌入实体的链接
【发布时间】:2015-03-14 23:19:59
【问题描述】:

我的项目中定义了以下实体:

国家

@Entity
@Data
public class Country {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    @Column(nullable = false)
    String name;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    List<City> cities = new ArrayList<City>();

}

城市

@Entity
@Data
public class City {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;
    @Column(nullable = false)
    String name;
    @ManyToOne
    Country country;
}

人物

@Entity
@Data
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;
    @Column
    String name;
    @Embedded
    Address address = new Address();
}

地址

@Data
public class Address {
    @Column
    String line;
    @ManyToOne
    Country country;
    @ManyToOne
    City city;
}

我还为PersonCountryCity 定义了存储库。

当我向 /persons/1 发出 GET 请求时,我得到以下结果:

{
   "name":null,
   "address":{
      "line":"Address1"
   },
   "_links":{
      "self":{
         "href":"http://localhost:8080/persons/1"
      },
      "city":{
         "href":"http://localhost:8080/persons/1/city"
      },
      "country":{
         "href":"http://localhost:8080/persons/1/country"
      }
   }
}

我怀疑由于地址是一个嵌入对象,生成的国家和城市链接是错误的。尽管存在 citycountry 值,但它们不会返回任何内容。正确的链接应该是什么?

Spring Data Rest 不支持嵌入对象吗?

【问题讨论】:

  • 我认为您无法联系到/persons/1/address
  • 它返回 HTTP 400 错误请求,并带有以下消息:PersistentEntity 不能为空!

标签: spring rest spring-boot spring-data-jpa spring-data-rest


【解决方案1】:

可能的解决方案:

  • 将关联移至父实体
  • 将可嵌入性提升到单独的实体资源中
  • 添加 ResourceProcessor 以删除这些链接
  • 添加自定义控制器来处理这些链接

更新: 这似乎已在 Spring-DATA-REST v2.1 中修复。见DATAREST-262

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-28
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    • 2016-03-21
    相关资源
    最近更新 更多