Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate makes Weld not initializing in Java SE

So, I have a basic Java SE program with dependency injection using Weld 1.2.

Everything is working fine, until I throw Hibernate into the mix, with the following pom.xml dependency entries:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
</dependency>
<dependency>
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se</artifactId>
    <version>2.2.4.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.6.Final</version>
</dependency>

Hibernate being the added dependency, making it break. This is my entry class:

public class EntryPoint {
    public static void main( String[] ARGV ) {
        Weld weld = new Weld();
        WeldContainer container = weld.initialize();

        Application application = container.instance().select(Application.class).get();

        application.testFetch();

        weld.shutdown();
    }
}

When I try to run it after including Hibernate dependency, this is my output:

Sep 11, 2014 11:13:44 PM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 2.2.4 (Final)
Sep 11, 2014 11:13:44 PM org.jboss.weld.bootstrap.WeldStartup startContainer
INFO: WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
Exception in thread "main" org.jboss.weld.exceptions.DeploymentException: org.jboss.jandex.ClassInfo.hasNoArgsConstructor()Z
    at org.jboss.weld.executor.AbstractExecutorServices.checkForExceptions(AbstractExecutorServices.java:66)
    at org.jboss.weld.executor.AbstractExecutorServices.invokeAllAndCheckForExceptions(AbstractExecutorServices.java:43)
    at org.jboss.weld.executor.AbstractExecutorServices.invokeAllAndCheckForExceptions(AbstractExecutorServices.java:51)
    at org.jboss.weld.bootstrap.ConcurrentBeanDeployer.addClasses(ConcurrentBeanDeployer.java:62)
    at org.jboss.weld.bootstrap.BeanDeployment.createClasses(BeanDeployment.java:209)
    at org.jboss.weld.bootstrap.WeldStartup.startInitialization(WeldStartup.java:351)
    at org.jboss.weld.bootstrap.WeldBootstrap.startInitialization(WeldBootstrap.java:76)
    at org.jboss.weld.environment.se.Weld.initialize(Weld.java:157)
    at com.mybeautycompare.integration.EntryPoint.main(EntryPoint.java:24)
    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:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.NoSuchMethodError: org.jboss.jandex.ClassInfo.hasNoArgsConstructor()Z
    at org.jboss.weld.environment.se.discovery.WeldSEClassFileInfo.<init>(WeldSEClassFileInfo.java:65)
    at org.jboss.weld.environment.se.discovery.WeldSEClassFileServices.getClassFileInfo(WeldSEClassFileServices.java:85)
    at org.jboss.weld.bootstrap.FastAnnotatedTypeLoader.loadAnnotatedType(FastAnnotatedTypeLoader.java:61)
    at org.jboss.weld.bootstrap.BeanDeployer.addClass(BeanDeployer.java:97)
    at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$1.doWork(ConcurrentBeanDeployer.java:65)
    at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$1.doWork(ConcurrentBeanDeployer.java:62)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Line 24 in my EntryPoint class is: WeldContainer container = weld.initialize();

like image 420
Wrench Avatar asked Sep 11 '14 22:09

Wrench


People also ask

Does hibernate support lazy initialization for detached objects?

As you know that hibernate does not support lazy initialization for detached objects. Typically, you load an entity using hibernate session and work-through it and then close the session. After closing the session, entity becomes detached entity, so it cannot make further calls to database until again associated with a new session.

What are the static methods of hibernate initialize and initialize?

The static methods Hibernate.initialize () and Hibernate.isInitialized (), provide the application with a convenient way of working with lazily initialized collections or proxies. Hibernate.initialize (entity.getXXX ()) will force the initialization of a proxy, entity.getXXX (), as long as its Session is still open.

How to force hibernate to initialize a proxy or collection before closing?

2. Using Hibernate.initialize() to initialize proxy/collection. Sometimes a proxy or collection needs to be initialized before closing the Session. One way is to force initialization by calling entity.getXXX() or entity.getXXX().size(), for example. However, this can be confusing to readers of the code and it is not convenient for generic code.

How to use CDI in Java EE with Weld?

For using CDI in Java EE, you obviously need a Java EE Container, a plain old application with a main method won't do. Weld is simply telling you that transactions are not available (since you're not running in an EE container), so any transaction-related CDI features will be disabled.


1 Answers

This has nothing to do with Hibernate. You're missing jandex from your classpath, which is required in Weld 2.2.x. Verify that you end up with a jandex 1.2 jar on your classpath after building.

Since you're using maven, add this to your pom.xml:

<dependency>
    <groupId>org.jboss</groupId>
    <artifactId>jandex</artifactId>
    <version>1.2.2.Final</version>
</dependency>

Also related:

Why is Hibernate 4.2 using jandex and classmate if its Maven POM defines them as test scope?

like image 106
John Ament Avatar answered Nov 15 '22 20:11

John Ament