我有一个非常简单的弹簧启动+弹簧数据休息应用程序。我尝试使用spring数据保存来保存具有一对一映射的实体,但看起来只有父节点被保存,而子节点却没有。 以下是我的代码
@SpringBootApplication
public class Application{
@Autowired
private PersonRepository personRepo;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
CommandLineRunner init(){
Address address = new Address();
address.setCountry("US");
address.setState("SV");
Person person = new Person();
person.setName("Vincent");
person.setAddress(address);
personRepo.save(person);
return null;
}
}
@Entity
public class Address implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private int id;
private String country;
private String state;
}
@Entity
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private int id;
private String name;
@OneToOne(cascade={CascadeType.ALL})
private Address address;
}
@Projection(name="inlineAddress",types={Person.class})
public interface InlineAddress {
String getName();
Address getAddress();
}
@RepositoryRestResource(excerptProjection=InlineAddress.class)
public interface PersonRepository extends JpaRepository<Person, Integer> {
Person findByName(@Param("name") String name);
Person findById(@Param("id") int id);
Page<Person> findByNameStartsWith(@Param("name") String name, Pageable page);
}
public interface AddressRepository extends JpaRepository<Address, Integer> {
}
启动后,如果我访问http://localhost:8080/api/ 我在下面看到了回复
{
_links: {
addresses: {
href: "http://localhost:8080/api/addresses{?page,size,sort}",
templated: true
},
persons: {
href: "http://localhost:8080/api/persons{?page,size,sort,projection}",
templated: true
},
profile: {
href: "http://localhost:8080/api/profile"
}
}
}
然后我访问http://localhost:8080/api/persons,到目前为止,每件事情都很好
{
"_embedded": {
"persons": [
{
"address": {
"country": "US",
"state": "SV"
},
"name": "Vincent",
"_links": {
"self": {
"href": "http://localhost:8080/api/persons/1"
},
"person": {
"href": "http://localhost:8080/api/persons/1{?projection}",
"templated": true
},
"address": {
"href": "http://localhost:8080/api/persons/1/address"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/api/persons"
},
"profile": {
"href": "http://localhost:8080/api/profile/persons"
},
"search": {
"href": "http://localhost:8080/api/persons/search"
}
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
然而,在我使用
发布http://localhost:8080/api/persons/之后{
"name": "Michael",
"address": {
"country": "US",
"state": "SV"
}
}
它显示如下,看起来像迈克尔没有插入地址
{
"_embedded": {
"persons": [
{
"address": {
"country": "US",
"state": "SV"
},
"name": "Vincent",
"_links": {
"self": {
"href": "http://localhost:8080/api/persons/1"
},
"person": {
"href": "http://localhost:8080/api/persons/1{?projection}",
"templated": true
},
"address": {
"href": "http://localhost:8080/api/persons/1/address"
}
}
},
{
"address": null,
"name": "Michael",
"_links": {
"self": {
"href": "http://localhost:8080/api/persons/2"
},
"person": {
"href": "http://localhost:8080/api/persons/2{?projection}",
"templated": true
},
"address": {
"href": "http://localhost:8080/api/persons/2/address"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/api/persons"
},
"profile": {
"href": "http://localhost:8080/api/profile/persons"
},
"search": {
"href": "http://localhost:8080/api/persons/search"
}
},
"page": {
"size": 20,
"totalElements": 2,
"totalPages": 1,
"number": 0
}
}
我的代码有什么问题吗?我尝试使用旧方法而不使用弹簧数据休息但使用休息控制器,我发布的相同json工作正常。不确定为什么弹簧数据休息在这里不起作用。
答案 0 :(得分:0)
行。似乎无法做到这一点。我必须先发一个人
{"name"="Michael"}
然后发一个地址
{"country":"US,"state":"SV:}
,
最后给这个人写了一个地址
{ "name":"Michael", "address":"localhost:8080/addresses/1" }