Why does Spring return an object without previous change? - spring

I'm using spring jpa and something weird is happening. I have a JpaRepository repository and there is an update that I did inside the service, right after I need to get the new object and use it.
The problem is that after calling updateValue, the findById still returns the old state. Like the call to updateValue wasn't committed or something.
By running an integration test, it returns the new value. And after the whole transaction, the new value shows up.
repository.updateValue(productId,
repository.findById
Why the findById is not returning the updated object?

Related

Why JPA repository not fetch all data and only on the second query?

I'll try to get an entity from the database using a JPA repository. But the result only have his ID set, all other data are null (but they are not null in the database) like this :
I don't understand why, but the first time I call the repository, I have an error : SQLGrammarException.
If I call the repository a second time immediatly after first call and with same parameters, the call work but I receive an empty entity (only with ID as describe above)
I think have a problem with transactions, but I doesn't configure them (only put a springframework.Transactional on my REST Controller class).
The problem appeared when I start to use this (a solution to an another problem with TestRestTemplate usage for my JUnit tests)

Spring JPA force save

I have basic question but I cannot find the response for it.
As I see each time I change entity I have to save it.
But is there some way to enable autosave?
If the methods you update the entity is marked with #Transactional annotation, you don't need to call save or update methods.
Note that, the entity must be fetched from DB to be maintained. If the entity has already created, you still need to use save method.

Spring Data JPA save() throws NPE

I wrote a web service with spring boot using spring data jpa for persistence.
The webservice has some static objects (in Singleton Bean) that regulary needs to be backed up to my database.
Sometimes! (This sucks...I dont' really know what happens) when I call
ObjectType updated = myRepository.save(existingObject)
I get an java.lang.NullPointerException - without usable stacktrace as the method doing this is running via #Scheduled.
I tried debugging and existingObject seems to be absolutely fine. The error only occurs, when existingObject is actually NOT a new object (i.e. when id != 0)
P.S. I am using Spring Boot therefore not really using EntityManager. I only use the #Autowired myRepository.
I'm seeing something similar happening. During save, it seems the object is re-fetched from DB (perhaps to see which fields were altered?) but a ManyToOne relationship is not loaded (even though the FetchType is explicitly set to EAGER).
For some reason, a compareTo is called on the relationship. The related object isn't null, but it only has its ID filled in (presumably because that was available in the object that was fetched from the DB). All other fields are null.
When the compareTo then does its stuff, a NullPointerException follows.
As to the actual solution, I don't know yet, as I would have expected the FetchType EAGER to make sure the relationship is loaded. Hopefully this helps someone to further find the root cause.
(I would have added this as a comment as it doesn't actually answer the question, but StackOverflow won't let me due to insufficient reputation...)
You haven't provided enough information. IF that line is where the NullPointerException is occurring, then the only possibilities are that myRepository is null, or existingObject is null. However, it's possible the NullPointerException is happening as a result of something in the save. Wrap the code in a try catch, and log the exception stacktrace to file.
If needed, checkout the logging customization notes here:
http://projects.spring.io/spring-boot/docs/spring-boot/README.html

Can I prevent a LINQ DataContext from using a transaction when a TransactionScope is present?

I'm using a TransactionScope in a model controller class that coordinates several lower level, data access classes. The data access classes each use their own LINQ DataContext, and thanks to the magic of TransactionScope, they all participate in the same transaction if one is present.
Under normal circumstances, this is perfect and everything works. However, I've added an activity logging class and one of the places it can write to is the database. Unfortunately, it automatically picks up on the TransactionScope and if the transaction gets rolled back, so do all the log entries.
I've checked the Transaction property of the DataContext and it's null, as expected, so I'm not sure how to tell it to ignore the TransactionScope.
In your logging class, wrap your using(new datacontext()) into:
using (var s = new TransactionScope(TransactionScopeOption.Suppress)) {
}

Spring transaction propagation_required issue

In our java project we are using ORM with hibernate and spring.
I had problems in deleting persistent objects. For example this sample method gets entities by ids and then delete them:
#Transactional
public void remove(List<Long> ids) {
SearchTemplate template = new SearchTemplate();
template.addParameter("milestoneId",ids);
List <InvoiceQueue> items = this.findByCriteria(template);
...
this.delete(items);
}
Method executes Ok without any exception but doesn't actually delete the items from the DB.
Adding the following annotation to the method definition #Transactional(propagation = Propagation.REQUIRES_NEW) solves the problem.
Can anyone explain why it doesn't work with the default propagation type PROPAGATION_REQUIRED.
Thanks in advance.
Environment details :
hibernate.version 3.5.5-Final, spring.version 3.0.5.RELEASE
Really just repeating what #PeterBagyinszki said in his comment, but the reason quite probably is that the transaction within which your delete occurs gets rolled back due to some other part throwing an exception, and all the changes made during the transaction get canceled. With Propagation.REQUIRES_NEW, the delete is done within it's own separate nested transaction. The outcome of the nested transaction (committed or rolled back) won't affect the "outer" transaction and vice versa.
Check your logs to see what is causing the transaction to be rolled back, note that even something like a simple SELECT -query failing with something like NoResultException will cause the transaction to roll back, unless you explicitly state in the #Transactional-annotation it not to roll back on certain exceptions.

Resources