2016-10-05 2 views
0

私は非常に簡単なスプリングブート+スプリングデータ休止アプリケーションを持っています。私は春のデータを使用して1対1のマッピングを持つエンティティを保存しようとしましたが、親だけが保存されているように見えますが、子はありません。以下 は私のコードは、しかし、私はhttp://localhost:8080/api/ を訪れる場合、私はその後、私はhttp://localhost:8080/api/personsを訪問応答スプリングデータの残りの部分に1対1のマッピングを持つエンティティを保存します

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

下に見て、これまでのすべてのものが良いです

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

起動後

@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はうまくいきました。なぜ春のデータ休憩がここでは機能しないのか分かりません。

+0

OKに {"country":"US,"state":"SV:}、 と最後のプット・アドレスでアドレスを掲示しなければなりません。これを行う方法はないと思われる。 {"国" = "マイケル"}で人を投稿し、{"国": "国": "SV:}でアドレスを投稿し、この人に最後に住所を付ける必要があります{ \t "name": "Michael"、 \t "address": "http:// localhost:8080/addresses/1" } – vincent

答えて

0

OK。これを行う方法はないと思われる。私が最初に {"name"="Michael"} によって人物を投稿してから、この人 { "name":"Michael", "address":"localhost:8080/addresses/1" }

関連する問題