atos1990 / orika

Automatically exported from code.google.com/p/orika
0 stars 0 forks source link

mapping hibernate classes to webservice classes throws exceptions #75

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. call webservice
2. mapping hibernate class to WS class
3. it sometimes throws the exception.

What is the expected output? What do you see instead?
I got the exception.

What version of the product are you using? On what operating system?
Orika 1.4.0 - orika-core-1.4.0-20121211.022329-15-deps-included.jar

Throwed exception

ma.glasnost.orika.MappingException: 
ma.glasnost.orika.impl.generator.CompilerStrategy$SourceCodeGenerationException:
 class com.example.hibernate.Product$$EnhancerByCGLIB$$b9465553 is not 
accessible
    at ma.glasnost.orika.impl.generator.MapperGenerator.build(MapperGenerator.java:95)
    at ma.glasnost.orika.impl.DefaultMapperFactory.buildMapper(DefaultMapperFactory.java:1046)
    at ma.glasnost.orika.impl.DefaultMapperFactory.registerClassMap(DefaultMapperFactory.java:880)
    at com.example.mapping.EntityMapper.map(EntityMapper.java:50)
    at com.example.mapping.EntityMapper.map(EntityMapper.java:69)
    at com.example.ws.helpers.store.ProductHelper.getAll(ProductHelper.java:53)
    at com.example.ws.StoreWebService.getProducts(StoreWebService.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.sun.xml.ws.api.server.InstanceResolver$1.invoke(InstanceResolver.java:246)
    at com.sun.xml.ws.server.InvokerTube$2.invoke(InvokerTube.java:146)
    at com.sun.xml.ws.server.sei.EndpointMethodHandler.invoke(EndpointMethodHandler.java:257)
    at com.sun.xml.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:95)
    at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:629)
    at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:588)
    at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:573)
    at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:470)
    at com.sun.xml.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:295)
    at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:515)
    at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:285)
    at com.sun.xml.ws.transport.http.servlet.ServletAdapter.handle(ServletAdapter.java:143)
    at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doGet(WSServletDelegate.java:155)
    at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doPost(WSServletDelegate.java:189)
    at com.sun.xml.ws.transport.http.servlet.WSServlet.doPost(WSServlet.java:76)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:581)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1571)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: 
ma.glasnost.orika.impl.generator.CompilerStrategy$SourceCodeGenerationException:
 class com.example.hibernate.Product$$EnhancerByCGLIB$$b9465553 is not 
accessible
    at ma.glasnost.orika.impl.generator.JavassistCompilerStrategy.assureTypeIsAccessible(JavassistCompilerStrategy.java:178)
    at ma.glasnost.orika.impl.generator.MapperGenerator.build(MapperGenerator.java:55)
    ... 43 more
Caused by: orika_shaded.javassist.NotFoundException: 
com.example.hibernate.Product$$EnhancerByCGLIB$$b9465553
    at orika_shaded.javassist.ClassPool.get(ClassPool.java:450)
    at ma.glasnost.orika.impl.generator.JavassistCompilerStrategy.assureTypeIsAccessible(JavassistCompilerStrategy.java:176)
    ... 44 more

Original issue reported on code.google.com by m.brezov...@gmail.com on 3 Jan 2013 at 9:49

GoogleCodeExporter commented 9 years ago
Please can you verify with latest release, 1.4.0 is already out, and I see that 
you're using a snapshot ?

Original comment by elaat...@gmail.com on 3 Jan 2013 at 10:49

GoogleCodeExporter commented 9 years ago
Okay, what is the latest release? I found only 1.4.0 and I'm using the latest 
snapshot from this site. 
https://repository-orika.forge.cloudbees.com/snapshot/ma/glasnost/orika/orika-co
re/1.4.0-SNAPSHOT/

Original comment by m.brezov...@gmail.com on 3 Jan 2013 at 10:55

GoogleCodeExporter commented 9 years ago
1.4.0 is the latest release, and it's available from maven central

Original comment by elaat...@gmail.com on 3 Jan 2013 at 11:30

GoogleCodeExporter commented 9 years ago
But I'm using the release 1.4.0, I tried to use JAR from maven central, but it 
still throws the same exception.

Original comment by m.brezov...@gmail.com on 4 Jan 2013 at 12:14

GoogleCodeExporter commented 9 years ago
The problem is that the class you're trying to map is not visible by orika 
class loader. 
Can you please try to use HibernateUnenhanceStrategy, (example):
MapperFactory factory = new 
DefaultMapperFactory.Builder().unenhanceStrategy(new 
HibernateUnenhanceStrategy()).build();

BTW If you have a failing test case please attach it to this issue.

Original comment by elaat...@gmail.com on 4 Jan 2013 at 12:45

GoogleCodeExporter commented 9 years ago
I was using HibernateUnenhanceStrategy before thowing this exception. I don't 
understand what does invisibility mean, but all classes are public.

Original comment by m.brezov...@gmail.com on 4 Jan 2013 at 3:43

GoogleCodeExporter commented 9 years ago
'visibility' refers to the class-loader hierarchy (it's a bit complicated); in 
short, a classloader can't 'see' classes that were loaded by a child 
classloader. Tools like CGLib and Javassist typically have to create a separate 
child classloader to load the classes they create through enhancement at 
runtime. 

the class com.example.hibernate.Product$$EnhancerByCGLIB$$b9465553 (in your 
stack trace) is a special class created by CGLib at runtime.

What version of Hibernate are you using?

Original comment by matt.deb...@gmail.com on 9 Jan 2013 at 2:30

GoogleCodeExporter commented 9 years ago
Okay, I don't understand background of this problem, but i would like to 
resolve it. I use Hibernate version 3.2.5.

Original comment by m.brezov...@gmail.com on 9 Jan 2013 at 9:54

GoogleCodeExporter commented 9 years ago
thanks. it turns out hibernate < 3.3 uses CGLib for proxies and >= 3.3 uses 
Javassist, so it may be that our HibernateUnenhanceStrategy is only working 
with the Javassist versions. we'll craft an integration tests with 3.2.5 to 
verify...

Original comment by matt.deb...@gmail.com on 10 Jan 2013 at 4:16

GoogleCodeExporter commented 9 years ago
I just ran a quick test with what we already have using hibernate 3.2, and 
don't get any failures.

Is there anything else about your setup that might help us understand the 
problem?
Could you maybe provide an example of the code in your EntityMapper class 
and/or the ProductHelper class? 
It's hard to do much more unless we can construct an example failing test 
case...

Original comment by matt.deb...@gmail.com on 11 Jan 2013 at 5:53

GoogleCodeExporter commented 9 years ago
I remind that this problem occurs after about 10 calling and then it occurs 
regularly in every other calling.

I use following configuration:
Apache Tomcat/7.0.23
JVM Version 1.7.0_02-b13
OS Name Windows 7
MySQL connector 5.1.13
METRO 2.0
Spring Framework 3.0.6

Here is ProductHelper - class Product is hibernate class, class ProductDTO is 
WS class. It use my own class EntityMapper - see down.

public class ProductHelper {

    Session session = null;
    EntityMapper<Product,ProductDTO> mapper;

    public ProductHelper() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
        this.mapper = new EntityMapper<Product,ProductDTO>();
    }

    public List getAll(int idCompany) {
        List<ProductDTO> dtoList = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Product as product where product.category.user.company ='" + idCompany + "'");
            List<Product> productList = (List<Product>) q.list();
            dtoList = mapper.map(productList, ProductDTO.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dtoList;
    }

}

Here is EntityMapper class:

public class EntityMapper<T, S> implements Mapping<T, S> {

    // ORIKA
    private MapperFactory mapperFactory;
    private MapperFacade facade;

    public EntityMapper() {
        // ORIKA
        this.mapperFactory = new DefaultMapperFactory.Builder().build();

        DefaultMapperFactory.Builder builder = new DefaultMapperFactory.Builder();
        builder.unenhanceStrategy(new HibernateUnenhanceStrategy());
        this.mapperFactory = builder.build();

        this.facade = mapperFactory.getMapperFacade();

    }

    @Override
    public S map(T srcObject, Class<S> dstType) {
        // ORIKA
        mapperFactory.registerClassMap(mapperFactory.classMap(srcObject.getClass(), dstType).byDefault().toClassMap());
        S dstObject = (S) facade.map(srcObject, dstType);

        return dstObject;
    }

    @Override
    public List<S> map(List<T> srcList, Class<S> dstType) {

        List<S> dstList = new ArrayList<S>();

        for (T t : srcList) {
            S dstObject = map(t, dstType);
            dstList.add(dstObject);
        }

        return dstList;
    }

}

Original comment by m.brezov...@gmail.com on 11 Jan 2013 at 10:24

GoogleCodeExporter commented 9 years ago
Thanks for the info.
I'd start with a few changes in EntityMapper:
In general, registering a class-map is considered an initialization step,
and only needs to be done once per class (not for every instance)...I have
a guess that registering the classmap every time is probably the issue.
(see comments below).

public class EntityMapper<T, S> implements Mapping<T, S> {

    // ORIKA
    private MapperFactory mapperFactory;
    private MapperFacade facade;

    public EntityMapper() {
        // ORIKA

       // Unneeded statement; gets replaced by the factory built in the
next statements
       // this.mapperFactory = new DefaultMapperFactory.Builder()
       // .build();

        DefaultMapperFactory.Builder builder = new
DefaultMapperFactory.Builder();
        builder.unenhanceStrategy(new HibernateUnenhanceStrategy());
        this.mapperFactory = builder.build();

        this.facade = mapperFactory.getMapperFacade();

    }

    @Override
    public S map(T srcObject, Class<S> dstType) {
        // ORIKA
        // You don't need this statement; in fact, it may be what's causing
the problem
        // In general, registering a classmap each time will be very
in-efficient, and Orika is already
        // good enough to register one as needed if you're just using the
defaults
        // mapperFactory.registerClassMap(mapperFactory.classMap(srcObject.getClass(),
dstType).byDefault().toClassMap());
        S dstObject = (S) facade.map(srcObject, dstType);

        return dstObject;
    }

    @Override
    public List<S> map(List<T> srcList, Class<S> dstType) {

        List<S> dstList = new ArrayList<S>();

        for (T t : srcList) {
            S dstObject = map(t, dstType);
            dstList.add(dstObject);
        }

        return dstList;
    }

}

Original comment by matt.deb...@gmail.com on 11 Jan 2013 at 5:44

GoogleCodeExporter commented 9 years ago
did you get any chance to try those changes?

Original comment by matt.deb...@gmail.com on 18 Jan 2013 at 1:50

GoogleCodeExporter commented 9 years ago
Hi, I'm sorry for the delay in replying, I would like to thank you for your 
suggestions. I've changed my code according to your suggestion. But it still 
throws me the same exception.

Original comment by m.brezov...@gmail.com on 19 Jan 2013 at 12:05

GoogleCodeExporter commented 9 years ago
I'm having a bit of trouble reproducing the specific problem -- if you could 
provide a test case, that would be very helpful in resolving...

Original comment by matt.deb...@gmail.com on 21 Jan 2013 at 6:03

GoogleCodeExporter commented 9 years ago
Okay, I'll try to prepare simplified test case for you. I'll reply later.

Original comment by m.brezov...@gmail.com on 21 Jan 2013 at 6:06

GoogleCodeExporter commented 9 years ago

Original comment by elaat...@gmail.com on 8 Mar 2013 at 9:05