2016-10-05 130 views
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/ 我看到下面响应

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

确定。似乎没有办法做到这一点。我必须首先通过{“name”=“Michael”}发布一个人,然后通过{“country”:“US”,“state”:“SV:}发布地址,并最后将地址发送给此人{ \t “name”:“Michael”, \t“address”:“http:// localhost:8080/addresses/1” } – vincent

回答

0

好的。似乎没有办法做到这一点。我必须通过 {"name"="Michael"} 第一发布者,然后通过 {"country":"US,"state":"SV:}, 和最后放地址发布一个地址,这个人 { "name":"Michael", "address":"localhost:8080/addresses/1" }