Код: Выделить всё
@Entity
PersonEntity {
UUID id;
...
@OneToMany(mappedBy...)
List contact
}
@Entity
ContactEntity {
UUID id;
boolean main;
...
@OneToMany(mappedBy...)
List details;
@ManyToOne
PersonEntity person;
}
@Entity
ContactDetailsEntity {
UUID id;
String value;
@ManyToOne
ContactEntity
}
Код: Выделить всё
Person {
UUID id;
...
List contact
}
Contact {
UUID id;
boolean main;
...
List details;
@JsonIgnore
UUID personId;
}
ContactDetails {
UUID id;
String value
@JsonIgnore
UUID contactId;
}
< /code>
Теперь следующая часть: необходимо управлять контроллерами с некоторыми правилами для этого: < /p>
[list]
[*] У каждого объекта есть собственный контроллер. Если это зависимая сущность, то родительский идентификатор должен быть указан;
[*] Должно быть возможно создать/извлечь/обновить детей, хотя родительский контроллер
[/list]
Итак, мы должны иметь:
POST /person - it takes person model as body and it create itself plus contact and contact details (all children)
RETRIEVE /person/{id} - it takes only id and return full structure (with all children and children with their children)
PATCH /person/{id} - it takes person model as body and it update itself plus contact and contact details
DELETE /person/{id} - it delete entities with CASCADE
POST /contact - it takes contact model as body and it create itself plus contact details (all children)
RETRIEVE /contact/{id} - it takes only id and return full structure (with all children and children with their children)
PATCH /contact/{id} - it takes contact model as body and it update itself plus contact details
DELETE /contact/{id} - it delete entities with CASCADE (itself + children)
POST /contact-details - it takes contact-details model as body and it create itself. It doesnt have children so only itself
RETRIEVE /contact/{id} - it takes only id and return full structure (itself means because it doesnt have children)
PATCH /contact/{id} - it takes contact-details model as body and it update itself
DELETE /contact/{id} - it delete itself
< /code>
Кажется, это не имеет большого значения, но то, что я пытаюсь сделать: разделить операцию CRUD на службы модели домена. Что это значит? Как вы можете видеть, должно быть возможное создание детей, хотя и родительский контроллер, но в то же время это должно быть возможно с помощью собственного контроллера: та же самая операция создания, те же валидации для детей. Итак, я пытаюсь разделить это, так что: < /p>
public class PersonService {
PersonRepository repository;
ContactService contactService;
Person create(Person person){
...
person.setId(UUID.random());
PersionEntity entity = PersonMapper.INSTANCE.toEntity(person);
repository.save(entity);
person.getContacts().forEach(contact -> contact.setPersonId(person.getId());
contactService.create(person.getContacts());
return repository.findById(person.getId())
}
Person update(Person person) {
...
person.setId(UUID.random());
PersionEntity entity = PersonMapper.INSTANCE.toEntity(person);
repository.save(entity);
contactService.update(person.getContacts());
return repository.findById(person.getId())
}
...
}
public class ContactService {
ContactRepository repository;
ContactDetailsService cdService;
Contact create(Contact contact) { //
Подробнее здесь: [url]https://stackoverflow.com/questions/79737025/how-to-split-into-services-per-each-jpa-entity-or-how-to-avoid-code-duplicate[/url]