vincent
vincent

Reputation: 143

Save entity which has one-to-one mapping in spring data rest

I have a very simple spring boot + spring data rest app. I tried to save the entity which has a one-to-one mapping using spring data rest but looks like only the parent is saved, the child is not. Below is my code

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

}

After startup, if I visit http://localhost:8080/api/ I saw below response

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

Then I visit http://localhost:8080/api/persons, so far every thing is good

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

However, after I did a post http://localhost:8080/api/persons/ with

{
    "name": "Michael",
    "address": {
        "country": "US",
        "state": "SV"
        }

}

It shows below, looks like the address is not get inserted for Michael

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

Is there anything wrong with my code? I have tried using old approach without using spring data rest but using rest controller, the same json I posted was working fine. Not sure why spring data rest does not work here.

Upvotes: 1

Views: 612

Answers (1)

vincent
vincent

Reputation: 143

OK. Seems no way to do this. I have to first post a person by {"name"="Michael"} and then post an address by {"country":"US,"state":"SV:} , and last put address to this person { "name":"Michael", "address":"localhost:8080/addresses/1" }

Upvotes: 1

Related Questions