spring Hystrix command causing exception org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist - spring

i have Hystrix command written on controller class
#HystrixCommand(groupKey = "sctGroup", commandKey = "createStandaloneController", threadPoolKey = "create-sct-pool",
ignoreExceptions = {BadRequestException.class})
i have a repository.save written in the service for an entity with one to one mapping.
hystrix is causing exception
org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist:
when i remove the Hystrix command annotation the functionality is working as expected. however failing with hystrix.

Related

In the Spring boot, when I fetch data from DB, error popping List is null, but data available in db table

data not fetching, error thrown list is empty but data available in DB
service class code,
Service class code block
Controller class
Controller code block
error msg
Error msg
You must add #Autowired to have a dependency injected:
#Autowired
private country gocount;
Bean wiring is missing here hence null pointer exception is being thrown.
You need to do one of the following.
Autowire the dependency
#Autowired
private Country repocount;
Make a constructor and set this dependency.
Using a Lombok annotation may also help.
By seeing the error it is cleared that You haven't Autowired the Bean In controller this will surely solve the problem

Rollback is not working with spring declarative

I'm working with spring declarative approach to rollback transaction on any exceptions with in the method. This method had multiple DAO calls[1,2,3..] and those needs to be maintained in a single transaction. So i'm trying to acheive if any exceptions comes in DAO call[3], then spring has to rollback preceeding DAO calls. i.e[1,2]
#Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true,rollbackFor=java.lang.Throwable.class)
public void processWorkflowActionsInOneTransaction(...) throws Exception {
// DAO call 1 here....
// DAO call 2 here....
// DAO call 3 here....[throw exception]
}
The above configuration created a single transaction for the mentioned method, but it is not triggered rollback after exception. I have attached spring logs for the better understanding. Please help me if any one had a similar issue.
Spring logs

spring cron4j scheduling method invoked before bean initialization

I have build a web application and scheduled cron job using cron4j. While executing the cron the run() method is calling and in the run() method all other bean objects are showing null. Hence, I am getting NullPointerException. Below is my sample code.
class Employee{
#autowired
IEmployeeService employeeService;
public void run() {
employeeService.getEmployeeDetails();
}
}
The above example employeeService object getting null and all other bean objects inside getEmployeeDetails(); are getting null and getJdbcTemplate() is also null.
How to initialize bean objects in spring while executing cron using cron4j.
You can use #BeanFactoryPostProcessor annotation to order creation of beans and create/run your job at the end of bean initialization.
The definition of BeanFactoryPostProcessor to bean (configuration metadata) processing. That is to say, the Spring IoC container allows configuration of BeanFactoryPostProcessor metadata in the container before the actual read instantiate any other bean, and may modify it. If you want, you can configure multiple BeanFactoryPostProcessor. You can control the BeanFactoryPostProcessor by setting the'order'attribute of the execution order.
From Spring documentation:
The next extension point that we will look at is the org.springframework.beans.factory.config.BeanFactoryPostProcessor. The semantics of this interface are similar to those of the BeanPostProcessor, with one major difference: BeanFactoryPostProcessor operates on the bean configuration metadata; that is, the Spring IoC container allows a BeanFactoryPostProcessor to read the configuration metadata and potentially change it before the container instantiates any beans other than BeanFactoryPostProcessors.
Please find more information on Spring docs: http://docs.spring.io/spring/docs/4.1.3.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#beans-factory-extension-factory-postprocessors

Unit test class annotated by #Transactional and implications to detach/evict in hibernate

I'm struggling with a problem I and can not find out proper solution or even a cause neither in hibernate docs, sources nor S/O.
I have spring/hibernate application with DAO-Service-RPC layers, where DAO provides Hibernate entities and Service DTOs for RPC. Therefore I'm converting (mapping by Dozer) Entities to DTOs in service methods and mapping DTOs back to Entities there as well.
Mapping is as follows (not full method, checking ommited):
#Transactional
public updateAuthor(Author author) {
AuthorEntity existingEntity = this.authorDao.findById(author.getId());
this.authorDao.detach(existingEntity);
this.authorAssembler.toEntity(author, existingEntity, null);
this.authorDao.merge(existingEntity);
}
I have unit test classes annotated by #Transactional to avoid test data bleeding. Now, I realized, that there is something, I don't understand, going on in Service.
When I have my test class annotated by #Transactional, calling detach() seems to work (i.e. Hibernate is not reporting org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session, but #Version number on entity is not incremented properly (as if the parent (unit test)) TX was still holding on.
When I remove my test class annotation, mapping throws org.hibernate.LazyInitializationException: failed to lazily initialize a collection - which makes sense on it's own, because the entity is detached from session. What I don't understand is, why this exception is NOT thrown when the test class is annotated. As I understand, entity is detached from the current session in both cases.
My other question is - assuming the entity is behaving correctly - how to avoid missing such errors in unit tests and avoid test bleeding as well, as it seems to be, this type of error manifests only with unannotated test class.
Thank you!
(JUnit4, Spring 4.0.2.RELEASE, Hibernate 4.3.1.Final)

Jackson Json parsing exception for hibernate class

I'm using Spring 3 + JPA 2 (hibernate Impl) + Spring MVC's contentNegotiationResolver based on Json media type and got the following exception stack trace is thrown while leveraging Spring framework's
org.springframework.web.servlet.view.json.MappingJacksonJsonView
*
org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain:..... )
at org.codehaus.jackson.map.ser.StdSerializerProvider$1.serialize(StdSerializerProvider.java:62)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:268)
at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:146)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:118)
at org.codehaus.jackson.map.ser.ContainerSerializers$IndexedListSerializer.serializeContents(ContainerSerializers.java:236)
at org.codehaus.jackson.map.ser.ContainerSerializers$IndexedListSerializer.serializeContents(ContainerSerializers.java:189)
at org.codehaus.jackson.map.ser.ContainerSerializers$AsArraySerializer.serialize(ContainerSerializers.java:111)
at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:296)
at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:224)
at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:925)
at org.springframewor
*
The solution provided in the below thread is not working (using Json AutoDetect) doesnt work for me.
Strange Jackson exception being thrown when serializing Hibernate object
Providing Json AutoDetect and explicitly providing Json property annotation on getter method is not working - it still parses all the attributes. Also I've succeeded by avoiding certain attributes of JPA Entity class using #JsonIgnore which is kind of recursive or parent/child relation in nature. I've also tried detaching the entity class but no luck.
From my entity class, all the Jackson parser need to do is parse some three simple String attributes and rest of the attributes is marked with #JsonIgnore
Please let me know if any of you faced similar issue and resolved it.

Resources