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工作正常。不知道爲什麼春天的數據休息不起作用。
確定。似乎沒有辦法做到這一點。我必須首先通過{「name」=「Michael」}發佈一個人,然後通過{「country」:「US」,「state」:「SV:}發佈地址,並最後將地址發送給此人{ \t 「name」:「Michael」, \t「address」:「http:// localhost:8080/addresses/1」 } – vincent