저는 아주 간단한 스프링 부트 + 스프링 데이터 휴지 앱을 가지고 있습니다. 봄 데이터 나머지를 사용하여 일대일 매핑을 가진 엔티티를 저장하려고했지만 부모 만 저장되는 것처럼 보이지만 자식은 저장되지 않습니다. 다음은 내가 지금까지 모든 것은스프링 데이터 나머지 부분에 일대일 매핑을 가진 엔티티 저장
{
"_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를 방문
{
_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/ 방문하는 경우, 시작 후 내 코드
@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/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"
}
}
},
{
"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
}
}
위해 삽입되지 것처럼 보이는 내 코드 문제 있나요
{
"name": "Michael",
"address": {
"country": "US",
"state": "SV"
}
}
? 나는 봄 데이터 휴식을 사용하지 않고 오래된 컨트롤러를 사용하여 시도했지만, 나머지 컨트롤러를 사용하여 게시 된 동일한 json 잘 작동했다. 왜 봄 데이터 휴식은 여기서 작동하지 않는지 잘 모르겠다.
확인에
{"country":"US,"state":"SV:}
, 과 마지막 넣어 주소로 주소를 게시해야합니다. 이것을 할 방법이없는 것 같습니다. { "국가": "마이클"}로 사람을 먼저 게시 한 다음 { "국가": "상태": "SV :}"로 주소를 게시하고이 사람에게 주소를 붙여야합니다. { \t "name": "Michael", \t "address": "http : // localhost : 8080/addresses/1" } – vincent