Save and Update in jpa CRUD repository - java

I have one data already saved in my databse based on my repository and service.i want to save another data with postman by changing only the player id.But it is not create a new entity data.it update the existing entity data.My question is how to update a data by my service when it finds a existing id.But when it finds a new id it will save a new data into databse.
This is my repo:
#Repository
#Transactional
public interface CricketPlayerRepository extends CrudRepository<CricketPlayer,String> {
Optional<CricketPlayer> findCricketPlayerByName(String name);
}
This is my service:
#Service
public class CricketPlayerService {
private CricketPlayerRepository cricketPlayerRepository;
public CricketPlayerService(CricketPlayerRepository cricketPlayerRepository) {
super();
this.cricketPlayerRepository = cricketPlayerRepository;
}
public CricketPlayerService() {
}
public Optional<CricketPlayer> getPlayerByName(String name){
return cricketPlayerRepository.findCricketPlayerByName(name);
}
public CricketPlayer save(CricketPlayer cricketPlayer){
Optional<CricketPlayer> id = cricketPlayerRepository.findById(cricketPlayer.getPlayerId());
if (id.isPresent()){
//code here
}
// if (entityManager.isNew(cricketPlayer)) {
// em.persist(cricketPlayer);
// return cricketPlayer;
// } else {
// return em.merge(cricketPlayer);
// }
return cricketPlayerRepository.save(cricketPlayer);
}
public Iterable<CricketPlayer> findAllPlayers() {
return cricketPlayerRepository.findAll();
}
public Optional<CricketPlayer> findPlayersById(String id) {
return cricketPlayerRepository.findById(id);
}
}

save and update operations in hibernate ( and other frameworks) are based on id value. if an id exists merge (update) entity and otherwise save new instance. So it cannot be done in this context.

1)If PlayerId is primary key id, then you would have called merge(entity). If PlayerId is present it will update else it will create new record.
2)If PlayrerId is not primary key id. Best practice is to avoid PlayerId as primary key.
In postman you should pass database table primary key id along with PlayerId.
Then you call merge(entity). It will take care of create or update based on primary key
id is null or not.
for example below if you have passed primary key id in request.
Entity e = new Entity();
if (entityFromRequest.getId() != null){ //this is the db id from request
//get the entity from db and then change the state
e = entity from db by using id
}
e.setPlayerId = entityFromRequest.getPlayerId
merge(e); // it will create or update record

Related

CrudRepository - Hibernate - Overwrite existing model with manual set id

I have a problem which I try to figure out since many hours now.
I must save a model with manual set id in the database using CrudRepository and Hibernate.
But the manual set of the id is ignored always.
Is it somehow possible, to force
CrudRepository.save(Model m)
to persist the given Model with UPDATE?
The queries always results in INSERT statements, without using the id.
The reason I must do this manually is, that the identifier is not the database ID - it is a ID generated outside as UUID which is unique over multiple databases with this model-entry. This model is shared as serialized objects via hazelcast-cluster.
Following an example:
The database already contains a Model-Entry with the id 1:
id identifier_field_with_unique_constraint a_changing_number
1 THIS_IS_THE_UNIQUE_STRING 10
Now I need to update it. I create a new Model version
Model m = new Model();
m.setIdentifierFieldWithUniqueConstraint(THIS_IS_THE_UNIQUE_STRING);
m.setAChangingNumberField(20);
saveMe(m);
void saveMe(Model m) {
Optional<Model> presentModalOpt = modelCrudRepo.findByIdentField(THIS_IS_THE_UNIQUE_STRING)
if(presentModalOpt.isPresent()) {
// The unique value in my identifier field exists in the database already
// so use that id for the new model, so it will be overwritten
m.setId(modalOpt.get().getId());
} else {
m.setId(null);
}
// This call will now do an INSERT, instead of UPDATE,
// even though the id is set in the model AND the id exists in the database!
modelCrudRepo.save(m);
// ConstraintViolationException for the unique identifier field.
// It would be a duplicate now, which is not allowed, because it uses INSERT instead of UPDATE
}
The id Field is tagged with #Id and #GeneratedValue annotation (for the case that the id is null and the id should be generated)
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
I even tried to changed this field only to an #Id field without #GeneratedValue and generate the ID always on my own. It had no effect, it always used INSERT statements, never UPDATE.
What am I doing wrong?
Is there another identifier for the CrudRepository that declares the model as an existing one, other than the id?
I'm happy for any help.
CrudRepository has only save method but it acts for both insert as well as update.
When you do save on entity with empty id it will do a save.
When you do save on entity with existing id it will do an update
that means that after you used findById for example and changed
something in your object, you can call save on this object and it
will actually do an update because after findById you get an object
with populated id that exist in your DB.
In your case you are fetching the records based on a field (unique) But records will update only when the model object has a existing primary key value
In your code there should be presentModalOpt instead of modalOpt
void saveMe(Model m) {
Optional<Model> presentModalOpt = modelCrudRepo.findByIdentField(THIS_IS_THE_UNIQUE_STRING)
if(presentModalOpt.isPresent()) { // should be presentModalOpt instead of modalOpt
} else {
m.setId(null);
}
modelCrudRepo.save(m);
}
See the default implementation -
/*
* (non-Javadoc)
* #see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
*/
#Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}

Why spring boot update method create new record

I have created update method in spring boot. It creates new record instead of update the record.
Tried code:
controller:
#PutMapping("update/{id}")
public Users updateUser(#PathVariable Integer id, #RequestBody Users user) {
user.setFname(user.getFname());
user.setLname(user.getLname());
user.setAddress(user.getAddress());
user.setTelno(user.getTelno());
return serveiceClass.updateUserbyId(id, user);
}
service:
public Users updateUserbyId(Integer id, Users users) {
return repositoryInterface.save(users);
}
How can I solve this problem?
You are not checking if user is present or not and you creating new record by calling save method. save method always insert new row if id in given object is not present in DB, in your case user is having id 0 I guess, thats why it is inserting new record. You need to fetch the User from given id and update the existing record in DB.
Create one more method in service class
public Users getUserById(Integer id) {
return repositoryInterface.findById(id).orElse(null);
}
And then..
#PutMapping("update/{id}")
public Users updateUser(#PathVariable Integer id, #RequestBody Users user) {
Users userExisting = serveiceClass.getUserById(id);
if(userExisting == null){
throw Exception("User Not Found");
}
userExisting.setFname(user.getFname());
userExisting.setLname(user.getLname());
userExisting.setAddress(user.getAddress());
userExisting.setTelno(user.getTelno());
return serveiceClass.updateUserbyId(userExisting);
}

JPA not fetching data that reflects state of database

I have encountered a curious bug or feature while writing code. Here's the situation:
We are using a PostgreSQL database, EclipseLink in a JavaEE project.
What I am doing in this part of the project is fetching an entity from the database i.e.:
User user = userController.get(userId);
Which then goes to our controller and fetches the user via a TypedQuery:
#Stateless
#LocalBean
public class UserController {
private EntityManager em;
public User get(Integer userId){
User retval = null;
TypedQuery<User> = em.createNamedQuery("User.findByUserId", User.class);
q.setParameter("userId", userId);
retval = q.getSingleResult();
}
public User update(final User modified){...}
}
And in my User class I have:
#NamedQuery(name = "User.findByUserId", query = "SELECT u FROM User u WHERE u.id = :userId"),
So the call goes, I get my user object with its respective data from the database.
In the class where I called the userController.get method I continue to modify the data on this object, and call our controller again to update this data on the database
user.setServiceId(1); //any id (integer) pointing to an existing service, this is a ManyToOne relationship
userController.update(user);
And here is where it gets funny. In our update method inside the controller class I have my modified User object and using this object I get the primary key userId and fetch the data again from the database to get the original:
#Stateless
#LocalBean
public class userController {
private EntityManager em;
public User get(Integer userId){...}
public User update(final User modified){
User retval = null;
if(modified != null){
try {
User original = get(modified.getId()); //Here I fetch the current state of the DB
if(original != null){
Set<Modifications> modifications = apply(original, modified); //Method to apply modifications
retval = em.merge(original); //Merge changes into database
em.flush(); //Force data to be persisted
catch(Exception e){
}
return retval;
}
}
However, the fields in the original object do not reflect the state of the database but instead contains the same data as the modified object. In this case, the serviceId on the database is null, and in the modified I set it to an ID. The original has its serviceId set to the same value as the modified object even though it should contain the fetched data from the database, in this case null
My current solution is to construct a new User object, after fetching the user from the database, and modify the data on that new object:
User user = userController.get(userId);
User newUser = new User(user);
newUser.setService(service);
userController.update(newUser);
Now when I do the update method, the original reflects the state of the database.
Or maybe it reflects the state of the user object that already exists in the persistence context?
But why does this happen? Since I do make a new get call with a SELECT statement to the database in my update method.
You are using the same EntityManager for everything, both the read and the 'merge', which in this case is then a no-op. Everything read in through an EM is managed, so that if you read it back again, you get the same instance back. As long as the User isn't being serialized, it is 'managed' by the EntityManager it was read from, and so that same instance, and its changes, are visible on any get calls on that ID.
You didn't show how you are getting EntityManagers, but I would guess is isn't container managed, as they would inject a new one for these calls, and then close them for you when done. You haven't shown any transaction logic on how the update and the em context it is using are hooked up, but I would suggest you create a new EntityManager for these calls. Flush also seems unnecessary, as if update is wrapped in a transaction, should handle flushing the update statement to the database without this extra call.
If user.setServiceId(1); is called when the "user" entity is managed, the call is going to update the database row.
you can check the manage entity lifecycle
You need to refresh the data after saving it to the database and to get the latest state of the object, as em.refresh(retval)
You can find the code added below.
#Stateless
#LocalBean
public class userController {
private EntityManager em;
public User get(Integer userId){...}
public User update(final User modified){
User retval = null;
if(modified != null){
try {
User original = get(modified.getId()); //Here I fetch the current state of the DB
if(original != null){
Set<Modifications> modifications = apply(original, modified); //Method to apply modifications
retval = em.merge(original); //Merge changes into database
em.flush(); //Force data to be persisted
em.refresh(retval); // This will fetch the updated data from database
catch(Exception e){
}
return retval;
}
}

Update entity if already exists or create using spring jpa

I am new to spring data jpa. I have a scenario where I have to create an entity if not exists or update based on non primary key name.Below is the code i wrote to create new entity,it is working fine,but if an already exists record ,its creating duplicate.How to write a method to update if exists ,i usually get list of records from client.
#Override
#Transactional
public String createNewEntity(List<Transaction> transaction) {
List<Transaction> transaction= transactionRespository.saveAll(transaction);
}
Add in your Transaction Entity on variable called name this for naming as unique:
#Entity
public class Transaction {
...
#Column(name="name", unique=true)
private String name;
...
}
Then you won't be able to add duplicate values for name column.
First, this is from google composite key means
A composite key is a combination of two or more columns in a table that can be used to uniquely identify each row in the table when the columns are combined uniqueness is guaranteed, but when it taken individually it does not guarantee uniqueness.
A composite key with an unique key is a waste.
if you want to update an entity by jpa, you need to have an key to classify if the entity exist already.
#Transactional
public <S extends T> S save(S entity) {
if(this.entityInformation.isNew(entity)) {
this.em.persist(entity);
return entity;
} else {
return this.em.merge(entity);
}
}
There are two ways to handle your problem.
If you can not get id from client on updating, it means that id has lost its original function. Then remove your the annotation #Id on your id field,set name with #Id. And do not set auto generate for it.
I think what you want is an #Column(unique = true,nullable = false) on your name field.
And that is the order to update something.
Transaction t = transactionRepository.findByName(name);
t.set.... //your update
transactionRepository.save(t);

Hibernate get entity by non-primary key and update it + Spring mvc

When I retrieve an object using its id and I change its properties and update it, everything work fine but when I get my objects using their name + version and update them, none of the changes save in the database. Could you please someone let me know what is the problem?!
// Get by id
public PdfDocument get(Long id) {
return (PdfDocument) session().get(PdfDocument.class, id);
}
// Get by name + version
public PdfDocument get(String name, int version) {
Criteria criteria = session().createCriteria(PdfDocument.class);
criteria.add(Restrictions.eq("name", name));
criteria.add(Restrictions.eq("version", version));
return ((PdfDocument) criteria.uniqueResult()) ;
}
// update
public void update(PdfDocument PdfDocument) {
session().saveOrUpdate(PdfDocument);
}
May be criteria returning a different entity than your expecting one. Check the id of returned entity and the expected one.

Categories