How to save child entity with Spring data JPA repositories if you know the parent Id?
For example if we have one to many relation:
@Entity
public class Customer {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
@ManyToOne
private CustomerCategory category;
}
@Entity
public class CustomerCategory {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
}
One can do something like:
CustomerCategory someCategory = customerRepository.findOne(1L);// how to skip that line. The id should be enough.
Customer cust = new Customer("Evgeni", "Dimitrov", someCategory);
customerRepository.save(cust);
JPA can do load(just create a proxy and set the Id) instead of 'get'(select from the DB). Is that possible with Spring Data JPA?
Forget Id. You are now at the Object level of ORM (ObjectRelationMapping). Objects are composed of other objects. NO IDs.