New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add insert in JpaRepository [DATAJPA-1707] #2003
Comments
Jens Schauder commented I don't see how this would work in a consistent way.
There is one thing one could implement easily in a custom method if desired: performing a |
hbourada commented Imagine we have 2 REST endpoints one for insert and other for update to distinguish the 2 operations , if the entity does not have a nullable technical identifier (example: Long) which simplify new entity state checking we can not implements this functionality using regular
@Entity
class User {
@Id
String username;
String password; ....
}
class UserService {
UserRepository repository;
void insert(User u) {
repository.findById(u.getUsername()).ifPresent(u -> {throw new IllegalArgumentException("..")});
// if another parallel request insert same username and the 2 verify //condition above => we can not ensure insert
repository.save(u);
}
} if we have
|
Jens Schauder commented
Yes you can: Just select it from the database, which is the same way JPA does it in these cases.
The "if" is the problem. Judging from Stack Overflow questions and similar sources suggests many, possibly the majority of the JPA users are not aware of the basics JPA entity lifecycle, 1st level cache and dirty checking. This means many users aren't even aware that there is a decision to make or an expectation to have. As a case in point: the two question I asked still aren'd answered. Although I admit they are kind of rhetorical since I don't think there are good answers for this |
hbourada commented
I don't see any problem, if referenced entity exist => we insert current created entity
if user modify an entity after a persist using a setter and current transaction is not commited it's a normal that persistence context execute an insert after an update (I ensure my affirmation by a test) N.B.: Another point in spring data Mongo we have an insert method in so for consistency is not a bad idea to add |
Jens Schauder commented As laid out there is no clear way to make this work consistent with immediate expectations and the typical JPA behaviour. It also doesn't match the model of a "repository" where you just add an aggregate and therefore we try to avoid such method where possible |
Jens Schauder commented Regarding consistency with other repositories: The addition to the |
hbourada opened DATAJPA-1707 and commented
We should have in addition to
save
aninsert
only method inJpaRepository
to insert an entity if we want ensure creation of new entities without updating inadvertently a previous one:<S extends T> S insert(S entity)
Affects: 2.0 Backlog
The text was updated successfully, but these errors were encountered: