I would like to use #Retryable annotation for restTemplate. I've added:
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
as well as #EnableRetry to config class. I've marked method:
restTemplate.exchange(url, HttpMethod.POST, request, String.class);
with (in a new thread)
#Retryable(maxAttempts=4,value=Exception.class,backoff=#Backoff(delay = 2000))
But I'm getting error from catalina:
27-Oct-2017 18:11:41.023 SEVERE [http-nio-8080-exec-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
at
<ommitted>
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1103)
... 61 more
Caused by: java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.<clinit>(ReflectiveAspectJAdvisorFactory.java:76)
at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.<init>(AnnotationAwareAspectJAutoProxyCreator.java:53)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
... 63 more
Caused by: java.lang.ClassNotFoundException: org.aspectj.lang.annotation.Around
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
... 70 more
27-Oct-2017 18:11:41.038 INFO [http-nio-8080-exec-1] org.apache.catalina.core.ApplicationContext.log Closing Spring root WebApplicationContext
What I'm doing wrong ?
EDITED:
I moved a little bit forward. Found that Spring Retry makes use of AOP so I added:
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-aop</artifactid>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.8</version>
</dependency>
Now I'm getting different error:
27-Oct-2017 21:11:12.071 SEVERE [http-nio-8088-exec-5] org.springframework.web.servlet.DispatcherServlet.initServletBean Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController' defined in file [(...)\HomeController.class]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.aop.framework.ObjenesisCglibAopProxy
<ommitted>
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.springframework.aop.framework.ObjenesisCglibAopProxy
at org.springframework.aop.framework.DefaultAopProxyFactory.createAopProxy(DefaultAopProxyFactory.java:60)
at org.springframework.aop.framework.ProxyCreatorSupport.createAopProxy(ProxyCreatorSupport.java:105)
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:109)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:468)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:349)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:298)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:421)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1558)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
... 35 more
To isolate the problem I created small springMVC project and it's the same. To keep everything simple used only few dependencies. This is output from mvn dependency:tree:
[INFO] com.example:store:war:0.0.1-SNAPSHOT
[INFO] +- org.springframework:spring-webmvc:jar:4.0.3.RELEASE:compile
[INFO] | +- org.springframework:spring-beans:jar:4.0.3.RELEASE:compile
[INFO] | +- org.springframework:spring-context:jar:4.0.3.RELEASE:compile
[INFO] | +- org.springframework:spring-core:jar:4.0.3.RELEASE:compile
[INFO] | | \- commons-logging:commons-logging:jar:1.1.3:compile
[INFO] | +- org.springframework:spring-expression:jar:4.0.3.RELEASE:compile
[INFO] | \- org.springframework:spring-web:jar:4.0.3.RELEASE:compile
[INFO] +- javax.servlet:jstl:jar:1.2:compile
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] +- org.springframework.retry:spring-retry:jar:1.2.1.RELEASE:compile
[INFO] +- com.mashape.unirest:unirest-java:jar:1.4.9:compile
[INFO] | +- org.apache.httpcomponents:httpasyncclient:jar:4.1.1:compile
[INFO] | | +- org.apache.httpcomponents:httpcore:jar:4.4.4:compile
[INFO] | | \- org.apache.httpcomponents:httpcore-nio:jar:4.4.4:compile
[INFO] | +- org.apache.httpcomponents:httpmime:jar:4.5.2:compile
[INFO] | \- org.json:json:jar:20160212:compile
[INFO] +- org.springframework:spring-aop:jar:4.2.5.RELEASE:compile
[INFO] | \- aopalliance:aopalliance:jar:1.0:compile
[INFO] \- org.aspectj:aspectjweaver:jar:1.8.8:compile
It worked for me after adding the spring-boot-starter-aop dependency like below:
compile ("org.springframework.boot:spring-boot-starter-aop:1.5.10.RELEASE")
I will answer my own question 'cause it seems to be working now. All dependencies that are necessary:
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
check if there is no duplicate dependencies (f.e. by mvn dependency:tree, use <exclusions> where necessary). Be sure you decorated your config class with #EnableRetry. For some reason you need to separate the caller from the method itself. F.e.this:
#Retryable(maxAttempts = 4, value = {ResourceAccessException.class}, backoff = #Backoff(delay = 5000))
public ResponseEntity<String> tryToSendAndReturnResponseByRestTemplate(final RestTemplate restTemplate,
final HttpEntity<MyObjDTO>
request) {
return restTemplate.exchange(resolveUrl(ENDPOINT_ADDRESS), HttpMethod.POST, request, String.class);
}
goes to SenderManager class marked as f.e. #Service and in a different class f.e. PhoneDirector class you call:
senderManager.tryToSendAndReturnResponseByRestTemplate(restTemplate, request);
It should work. (If you need to specify what supposed to be returned if after 4 (only here) attempts you're still getting an exception, you can create another method (in SenderManager class) marked with #Recover, like this:
#Recover
public ResponseEntity<String> recoverWhenSendingMessageFailed(final ResourceAccessException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.REQUEST_TIMEOUT);
}
But If you needed args from #Retryable (in #Recover method) remeber that arguments are populated from the argument list of the failed method in the same order as the failed method, and with the same return type
spring-retry uses spring aop, make sure you have aop added in your pom and try clean building the application.
for spring app :
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
for spring boot :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
Related
I am getting this error
java.lang.AbstractMethodError: Receiver class com.sun.jersey.api.uri.UriBuilderImpl does not define or inherit an implementation of the resolved method abstract uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder; of abstract class javax.ws.rs.core.UriBuilder.
at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:96)
at org.glassfish.jersey.client.JerseyWebTarget.<init>(JerseyWebTarget.java:50)
at org.glassfish.jersey.client.JerseyClient.target(JerseyClient.java:274)
at org.glassfish.jersey.client.JerseyClient.target(JerseyClient.java:56)
...
when calling
client = ClientBuilder.newClient();
WebTarget url = client.target(someURL).path(somePath); // the error happens here !!!
Response response = url.request().get();
We use the jersey library.
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.35</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.35</version>
</dependency>
Any ideas what could be causing this and how to fix?
We use JDK 11 to run this code.
I can see that from another dependency in the same project we're also pulling these:
[INFO] | | +- com.sun.jersey:jersey-core:jar:1.9:compile
[INFO] | | +- com.sun.jersey:jersey-json:jar:1.9:compile
[INFO] | | +- com.sun.jersey:jersey-server:jar:1.9:compile
I wonder if this could be some sort of clash between versions, or even something more subtle?! I currently have no idea. I don't find much info on the web too about this particular error.
I've used Spring Boot version 1.5.2 with Maven default pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
...
Because Spring Boot does not have XmlMapper from Maven: jackson-dataformat-xml, so I added this dependency in pom.xml
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.8.7</version>
</dependency>
and check the Jackson dependencies for project, all of them have same version 2.8.7 (newest).
mvn dependency:tree -Dincludes=com.fasterxml.jackson.core
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) # demo ---
[INFO] com.example:demo:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.5.2.RELEASE:compile
[INFO] | \- com.fasterxml.jackson.core:jackson-databind:jar:2.8.7:compile
[INFO] +- com.fasterxml.jackson.dataformat:jackson-dataformat-xml:jar:2.8.7:compile
[INFO] | \- com.fasterxml.jackson.core:jackson-core:jar:2.8.7:compile
[INFO] \- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.7:compile
And when I run the application, it cannot start as there is an error in Spring Boot for NoSuchMethod found.
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.filter.OrderedHttpPutFormContentFilter]: Factory method 'httpPutFormContentFilter' threw exception; nested exception is java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
... 26 common frames omitted
Caused by: java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z
at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:564) ~[jackson-databind-2.8.7.jar:2.8.7]
at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:474) ~[jackson-databind-2.8.7.jar:2.8.7]
at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.build(Jackson2ObjectMapperBuilder.java:588) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.<init>(MappingJackson2HttpMessageConverter.java:57) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter.<init>(AllEncompassingFormHttpMessageConverter.java:61) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.filter.HttpPutFormContentFilter.<init>(HttpPutFormContentFilter.java:63) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.boot.web.filter.OrderedHttpPutFormContentFilter.<init>(OrderedHttpPutFormContentFilter.java:29) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
Any idea for this problem? I want to use XmlMapper from Jackson library to deserialize/serialize XML string to object and vice-versa (not from the HTTPRequest but internally).
Here is the full pom.xml for Maven https://pastebin.com/LMujJY7e
I think you have run into a problem of version compatibility of dependencies.
I use spring boot 1.5.2 with jackson-dataformat-xml without any problems.
This is part of my build.gradle
ext {
springBootVersion = '1.5.2.RELEASE'
}
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.8.5'
hope it helps
Please try updating the jackson bom version in parent pom file as 2.10.4. This resolved the issue for me.
<jackson-bom.version>2.10.4</jackson-bom.version>
I would like to use JdbcTemplate to query some very simple values:
String sql = "SELECT NAME FROM CUSTOMER WHERE CUST_ID = ?";
String name = (String)getJdbcTemplate().queryForObject(
sql, new Object[] { custId }, String.class);
So I added the flowing entry to my pom:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
I see other Jars added
spring-tx-4.3.7.RELEASE.jar
spring-beans-4.3.7.RELEASE.jar
spring-core-4.3.7.RELEASE.jar
I would like to minimize the jar clash with the host application, and reduce my code footprint,
Out of this list what can I exclude and still letting JdbcTemplate work?
I won't disappoint you but ... none of them is excludable. All of the mentioned jars are needed.
You can check that out by visiting the dependency respository site itself. There you can find all of them (and even more) in the "Compile Dependencies" list.
Or execute mvn dependency:tree -Dverbose -Dincludes=org.springframework:spring-core,org.springframework:spring-tx,org.springframework:spring-beans inside the root of your project to verify it on your own.
The command should give you something like:
[INFO] \- org.springframework:spring-jdbc:jar:4.3.7.RELEASE:compile
[INFO] +- org.springframework:spring-beans:jar:4.3.6.RELEASE:compile (version managed from 4.3.7.RELEASE)
[INFO] | \- (org.springframework:spring-core:jar:4.3.6.RELEASE:compile - version managed from 4.3.7.RELEASE; omitted for duplicate)
[INFO] +- org.springframework:spring-core:jar:4.3.6.RELEASE:compile
[INFO] \- org.springframework:spring-tx:jar:4.3.6.RELEASE:compile (version managed from 4.3.7.RELEASE)
[INFO] +- (org.springframework:spring-beans:jar:4.3.6.RELEASE:compile - version managed from 4.3.7.RELEASE; omitted for duplicate)
[INFO] \- (org.springframework:spring-core:jar:4.3.6.RELEASE:compile - version managed from 4.3.7.RELEASE; omitted for duplicate)
I'm developing a software with jackson, but totally get stuck... When I run the spring-boot program with java -jar <MY_APPLICATION>.jar command, I got the following error:
...
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'jacksonObjectMapper' threw exception; nested exception is
java.lang.IllegalAccessError: tried to access method com.fasterxml.jackson.databind.ser.std.StdSerializer.<init>(Ljava/lang/Class;)V from class com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 84 more
Caused by: java.lang.IllegalAccessError: tried to access method com.fasterxml.jackson.databind.ser.std.StdSerializer.<init>(Ljava/lang/Class;)V from class com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
at com.fasterxml.jackson.datatype.jsr310.JavaTimeModule.<init>(JavaTimeModule.java:159)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:78)
at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.registerWellKnownModulesIfAvailable(Jackson2ObjectMapperBuilder.java:719)
at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.configure(Jackson2ObjectMapperBuilder.java:572)
at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.build(Jackson2ObjectMapperBuilder.java:554)
at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration.jacksonObjectMapper(JacksonAutoConfiguration.java:87)
at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration$$EnhancerBySpringCGLIB$$2434a0bb.CGLIB$jacksonObjectMapper$0(<generated>)
at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration$$EnhancerBySpringCGLIB$$2434a0bb$$FastClassBySpringCGLIB$$d685921f.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration$$EnhancerBySpringCGLIB$$2434a0bb.jacksonObjectMapper(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 85 more
There are more error messages above the error, but I think the important message is java.lang.IllegalAccessError: tried to access method com.fasterxml.jackson.databind.ser.std.StdSerializer.<init>(Ljava/lang/Class;), which I know it cannot happen generally.
I confirmed that the jackson modules are in the same version(2.6.5 which comes from spring-boot's dependency), and codehaus's jackson is not included:
$ mvn dependency:tree -Dverbose -Dincluces=com.fasterxml.jackson.core | grep -v omitted
...
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) # patriot-fdc-web ---
[INFO] <MY_GROUP>:<MY_APPLICATION_A>:war:0.3.9-SNAPSHOT
[INFO] +- <MY_GROUP>:<MY_APPLICATION_B>:jar:0.3.9-SNAPSHOT:compile
[INFO] | \- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.6.5:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-core:jar:2.6.5:compile
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:1.3.3.RELEASE:compile
[INFO] \- com.fasterxml.jackson.core:jackson-databind:jar:2.6.5:compile (version managed from 2.4.2)
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.5:compile (version managed from 2.6.0)
Can anybody encounter such a problem? Anyone can suggest any solution or possibility for the error? Thank you in advance.
I've found the cause.
The dependency included calcite-avartica artifact, and it has a shaded jackson in it with version 2.1.1. That's why I cannot find the dependency by mvn dependency:tree. I found it through debugging remotely by seeing where StdSerializer came from.
https://issues.apache.org/jira/browse/CALCITE-1110
Avartica 1.9.0 or later might not bother us. I just excluded calcite-core and calcite-avartica and finally it worked fine.
Had a similar issue in sbt, the fix was also to remove calcita-core and calcite-avatica like so
ExclusionRule(organization = "org.apache.calcite", name = "calcite-core"),
ExclusionRule(organization = "org.apache.calcite", name = "calcite-avatica")
I also had a similar issue, and it was due to transitive dependency of jackson-datatype-jsr310 with lower version coming along with jackson-databind. Got the issue resolved by adding the exclude statement from the first level dependency in build.gradle.
compile (<dependency>){
exclude group: "com.fasterxml.jackson.datatype", module: "jackson-datatype-jsr310"
}
In case if you are using Maven, just identify which dependency is adding this dependency of Jackson with jsr310. To do this,
Open pom in Eclispe and select tab dependency hierarchy and search jsr310
Identify the name of dependency by which it is being added
Add below exclusion into pom.xml
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</exclusion>
</exclusions>
I have a class hierarchy like this:
#Entity
#Table (name="call_distribution_policies")
#Inheritance (strategy=InheritanceType.JOINED)
public class CallDistributionPolicy implements Serializable, Cloneable{
----------------
}
#Entity
#Table(name="skill_based_call_distribution_policies")
public class SkillBasedCallDistributionPolicy extends CallDistributionPolicy {
--------------
}
public class CallDistributionPolicyDAOJPAImpl extends
AbstractJPADAOImpl<CallDistributionPolicy> implements
CallDistributionPolicyDAO {
}
public CallDistributionPolicy get(long id) {
try {
Query query = entityManager
.createQuery("from CallDistributionPolicy where id = :id");
query.setParameter("id", id);
List<CallDistributionPolicy> resultList = query.getResultList();
if (!CollectionUtils.isEmpty(resultList)) {
return resultList.get(0);
}
return null;
} catch (EntityNotFoundException e) {
return null;
}
}
}
When I do this:
log.debug(" loaded: " + callDistributionPolicyDao.get(10).toString())
It prints the toString() of the SkillsBasedCallDistributionPolicy
But when I try to cast it like this:
SkillsBasedCallDistributionPolicy scdp = (SkillsBasedCallDistributionPolicy) callDistributionPolicyDao.get(10)
I get class cast exception.
com.vantage.callcenter.core.entity.acd.CallDistributionPolicy$$EnhancerByCGLIB$$334f3d1b cannot be cast to com.vantage.callcenter.core.entity.acd.SkillBasedCallDistributionPolicy
The instanceof check fails too!
When I inspect the object in eclipse, I see the CGLIB Proxy, but as far as I understand, the CGLIB proxy should extend the SkillsBasedCallDistributionPolicy class? In the CGLIB$CALLBACK_0 property, I can see the entity class is "CallDistributionPolicy" but the target is "SkillsBasedCallDistributionPolicy".
What should be the proper process of loading the Subclass? I can see hibernate is generating all the right SQL and loading the proper subclass, but how can I check the instanceof and cast it into a subclass?
I am using hibernate 3.2.1 , Spring 2.5.5 , cglib2.1_3. Any suggestions?
I know that this has been a problem in Hibernate during a long time, see for example:
ClassCastException cglib lazy loading
JPA: instanceof vs. hibernate proxy: when is it safe?
How to Avoid ClassCastExceptions when using Hibernate Proxied Objects)
And by problem, I mean bug, instanceof and casting should just work.
But I couldn't reproduce your issue with Hibernate 3.3.0.SP1. Both instanceof and casting to subclasses of a hierarchy using a joined strategy just worked. Tested with:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-cglib-repack</artifactId>
<version>2.1_3</version>
</dependency>
and
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.8.0.GA</version>
</dependency>
I'm pretty sure there was a Jira issue for this, but couldn't find it.
The problem(bug) is consistent across my application. Can you post your working pom.xml here so I can see exactly what hibernate dependencies are you using?
Below the dependencies I used:
<project>
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.10</version>
</dependency>
</dependencies>
</dependencyManagement>
...
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.4.0.GA</version>
<exclusions>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-cglib-repack</artifactId>
<version>2.1_3</version>
</dependency>
...
</dependencies>
</project>
Here is the dependency tree:
[INFO] +- org.hibernate:hibernate-entitymanager:jar:3.4.0.GA:compile
[INFO] | +- org.hibernate:ejb3-persistence:jar:1.0.2.GA:compile
[INFO] | +- org.hibernate:hibernate-commons-annotations:jar:3.1.0.GA:compile
[INFO] | +- org.hibernate:hibernate-annotations:jar:3.4.0.GA:compile
[INFO] | +- org.hibernate:hibernate-core:jar:3.3.0.SP1:compile
[INFO] | | +- antlr:antlr:jar:2.7.6:compile
[INFO] | | \- commons-collections:commons-collections:jar:3.1:compile
[INFO] | +- org.slf4j:slf4j-api:jar:1.5.10:compile
[INFO] | +- dom4j:dom4j:jar:1.6.1:compile
[INFO] | | \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] | \- javax.transaction:jta:jar:1.1:compile
...
[INFO] \- org.hibernate:hibernate-cglib-repack:jar:2.1_3:compile