gpt4 book ai didi

spring - 链接到 Spring Data Rest 中的嵌入式实体

转载 作者:行者123 更新时间:2023-12-04 12:47:22 24 4
gpt4 key购买 nike

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

国家

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

我还为 Person 定义了存储库, CountryCity .

当我向/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"
}
}
}

我怀疑由于地址是一个嵌入对象,因此生成的国家和城市链接是错误的。尽管 city 他们不返回任何东西和 country值存在。正确的链接应该是什么?

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

最佳答案

可能的解决方案:

  • 将关联移动到父实体
  • 将 embeddable 提升为单独的实体资源
  • 添加 ResourceProcessor删除这些链接
  • 添加自定义 Controller 来处理这些链接

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

    关于spring - 链接到 Spring Data Rest 中的嵌入式实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27982508/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com