'JPA Hibernate: Post and Get using a foreign key
I am using Spring, but also Hibernate to set up a RESTful service. Unfortunately, I don't know how to work on the weak entity's table when I just have the related foreign key.
I think, it is difficult to explain, let me give you an example:
Imagine we have the class Person and the class Dog. Every Dog that exists has exactly one Person as owner. A Person don't have to own a Dog. However, a Person can have as many Dogs as he or she wants.
Unfortunately, I am not able to show you any code because ctrl+k, or code tags doesn't work, but there are layers for both, Person and Dog:
in Package com.example.person: Person, PersonController, PersonService, PersonRepository
in Package com.example.dog: Dog, DogController, DogService, DogRepository
Now, I am in DogService.java. I want to do two things:
- Get all dogs of a Person just having the Person's id: My first idea was to use the Query annotation in DogRepository to do something like "... WHERE d.person = ?1". But I don't know exactly how to do that.
- Post a dog by using the id of the Person: How can I save something to the Repository by using the Person's id? Ususally I would use dogRepository. save(...).
It is like an invisible wall between dog and person in my head. Could you please help me?
Thank you very much!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
