I have two entities:
Account {
@Id
Long id;
}
Preference {
...
@ManyToOne
private Account account;
}
When saving a Preference, I have the accountId, but I don't have an Account entity. How should I save the preference in this situation?
Load the Account entity and set it on the preference? To me, that seems like an erroneous trip to the database.
Have an accountId field that is persistable and make the Account field read-only? Having both an accountId field and an Account field seems redundant?
Use a NamedQuery to persist the Preference? I was hoping to just generically save an entity without special logic for.
Use the getReference call of the entityManager to load customer object using the id and then set that onto the customer history. In most cases this call would return a proxy with just the id embedded, the customer attributes will not be loaded unless some other method of the customer is invoked.
Sometimes your object or table has no primary key. The best solution in this case is normally to add a generated id to the object and table. If you do not have this option, sometimes there is a column or set of columns in the table that make up a unique value. You can use this unique set of columns as your id in JPA.
For SpringData Jpa, a cleaner approach will be to use repository. saveAll instead of a for loop with repository. save . saveAll will automatically iterate through the list and save it.
Use em.getReference(Account.class, accountId)
. It returns an uninitialized proxy on the entity, without going to the database. Your use-case is the main reason for the existence of this method.
Additional to em.getReference
or JpaRepository.getOne
manipulation for spring-data-jpa there are other solutions:
It is possible via Update Query. E.g. with spring-data
@Transactional
@Modifying
@Query("UPDATE Preference SET prop1=:prop1 .. WHERE u.id=:id")
int updatePreference(@Param("id") int id, @Param("prop1") String prop1, ...);
see Updating Entities with Update Query in Spring Data JPA
There is spring-data DomainClassConverter
that is auto resolving entity instances from request parameters or path variables. E.g.
@GetMapping("/employees/{id}")
public Employee getEmployeeById(@PathVariable("id") Employee employee) {
return employee;
}
See Resolving entity instances from request parameters or path variables
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With