-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Changing to programmatic style raises exception
PostPosted: Thu Apr 14, 2016 3:53 am 
Newbie

Joined: Tue Nov 24, 2015 3:52 pm
Posts: 12
Hi,

I had an entity manager initialized using annotations:

Code:
   @PersistenceContext(unitName = "local_dev")
   protected EntityManager manager;


In order to have a dynamic behavior (different persistence units) I switched to programmatic initialization :

Code:
public abstract class PersistenceRepository<T extends Identifiable> implements Repository<T> {

   private Class<T> type;

   protected EntityManager manager;

   public PersistenceRepository(Class<T> type) {
      initEntityManager();
      this.type = type;
   }

   private void initEntityManager() {
      EntityManagerFactory emf = null;
      switch (EnvironmentHelper.ENV_TYPE) {
      case LOCAL:
         emf = Persistence.createEntityManagerFactory("local_dev");
         break;
      case INTEG:
         emf = Persistence.createEntityManagerFactory("local_dev");
         break;
      case PREPROD:
         emf = Persistence.createEntityManagerFactory("preprod");
         break;
      case PROD:
         emf = Persistence.createEntityManagerFactory("prod");
         break;
      default:
         break;
      }

      manager = emf.createEntityManager();

   }


But with this approach I have an exception:
Code:
Caused by: javax.ejb.EJBException: org.jboss.weld.exceptions.CreationException: WELD-000079: Could not find the EJB in JNDI:  class com.visiativ.my3dplayer.repository.AttachmentRepository$Proxy$_$$_Weld$EnterpriseProxy$
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:190) [wildfly-ejb3-8.2.1.Final.jar:8.2.1.Final]
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:275) [wildfly-ejb3-8.2.1.Final.jar:8.2.1.Final]
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.requiresNew(CMTTxInterceptor.java:369) [wildfly-ejb3-8.2.1.Final.jar:8.2.1.Final]
        at org.jboss.as.ejb3.tx.LifecycleCMTTxInterceptor.processInvocation(LifecycleCMTTxInterceptor.java:66) [wildfly-ejb3-8.2.1.Final.jar:8.2.1.Final]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
        at org.jboss.as.weld.injection.WeldInjectionContextInterceptor.processInvocation(WeldInjectionContextInterceptor.java:43)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
        at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) [wildfly-ejb3-8.2.1.Final.jar:8.2.1.Final]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
        at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45) [wildfly-ee-8.2.1.Final.jar:8.2.1.Final]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
        at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:64) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
        at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:326) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
        at org.jboss.invocation.PrivilegedWithCombinerInterceptor.processInvocation(PrivilegedWithCombinerInterceptor.java:80) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
        at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
        at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:160) [wildfly-ee-8.2.1.Final.jar:8.2.1.Final]
        ... 11 more
Caused by: org.jboss.weld.exceptions.CreationException: WELD-000079: Could not find the EJB in JNDI:  class com.visiativ.my3dplayer.repository.AttachmentRepository$Proxy$_$$_Weld$EnterpriseProxy$
        at org.jboss.weld.injection.producer.ejb.SessionBeanProxyInstantiator.newInstance(SessionBeanProxyInstantiator.java:69) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
        at org.jboss.weld.bean.SessionBean.create(SessionBean.java:149) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
        at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:69) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
        at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:742) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
        at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:840) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
        at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
        at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:370) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
        at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:381) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
        at org.jboss.weld.injection.producer.DefaultInjector$1.proceed(DefaultInjector.java:71) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
        at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
        at org.jboss.weld.injection.producer.DefaultInjector.inject(DefaultInjector.java:73) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
        at org.jboss.weld.injection.producer.ejb.SessionBeanInjectionTarget.inject(SessionBeanInjectionTarget.java:140) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
        at org.jboss.as.weld.injection.WeldInjectionContext.inject(WeldInjectionContext.java:39)
        at org.jboss.as.weld.injection.WeldInjectionInterceptor.processInvocation(WeldInjectionInterceptor.java:51)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
        at org.jboss.as.ee.component.AroundConstructInterceptorFactory$1.processInvocation(AroundConstructInterceptorFactory.java:28) [wildfly-ee-8.2.1.Final.jar:8.2.1.Final]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
        at org.jboss.as.weld.injection.WeldInterceptorInjectionInterceptor.processInvocation(WeldInterceptorInjectionInterceptor.java:56)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
        at org.jboss.as.weld.ejb.Jsr299BindingsCreateInterceptor.processInvocation(Jsr299BindingsCreateInterceptor.java:94)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
        at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50) [wildfly-ee-8.2.1.Final.jar:8.2.1.Final]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:273) [wildfly-ejb3-8.2.1.Final.jar:8.2.1.Final]
        ... 27 more
Caused by: javax.persistence.PersistenceException: Could not set provided connection [org.jboss.jca.adapters.jdbc.jdk7.WrappedConnectionJDK7@63454e9f] to auto-commit mode (needed for schema generation)
        at org.hibernate.jpa.internal.schemagen.JpaSchemaGenerator$ConnectionProviderJdbcConnectionAccess.<init>(JpaSchemaGenerator.java:709)
        at org.hibernate.jpa.internal.schemagen.JpaSchemaGenerator$ConnectionProviderJdbcConnectionAccess.<init>(JpaSchemaGenerator.java:686)
        at org.hibernate.jpa.internal.schemagen.JpaSchemaGenerator.determineAppropriateJdbcConnectionContext(JpaSchemaGenerator.java:413)
        at org.hibernate.jpa.internal.schemagen.JpaSchemaGenerator.access$100(JpaSchemaGenerator.java:69)
        at org.hibernate.jpa.internal.schemagen.JpaSchemaGenerator$Generation.execute(JpaSchemaGenerator.java:122)
        at org.hibernate.jpa.internal.schemagen.JpaSchemaGenerator.performGeneration(JpaSchemaGenerator.java:76)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:859)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845)
        at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844)
        at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) [hibernate-jpa-2.1-api-1.0.0.Final.jar:1.0.0.Final]
        at com.visiativ.my3dplayer.repository.core.PersistenceRepository.initEntityManager(PersistenceRepository.java:38)
        at com.visiativ.my3dplayer.repository.core.PersistenceRepository.<init>(PersistenceRepository.java:27)
        at com.visiativ.my3dplayer.repository.AttachmentRepository.<init>(AttachmentRepository.java:24)
        at com.visiativ.my3dplayer.repository.AttachmentRepository$Proxy$_$$_Weld$EnterpriseProxy$.<init>(Unknown Source)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.8.0_77]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) [rt.jar:1.8.0_77]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [rt.jar:1.8.0_77]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) [rt.jar:1.8.0_77]
        at java.lang.Class.newInstance(Class.java:442) [rt.jar:1.8.0_77]
        at org.jboss.weld.security.NewInstanceAction.run(NewInstanceAction.java:33) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
        at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.8.0_77]
        at org.jboss.weld.injection.producer.ejb.SessionBeanProxyInstantiator.newInstance(SessionBeanProxyInstantiator.java:56) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
        ... 50 more
Caused by: java.sql.SQLException: You cannot set autocommit during a managed transaction!
        at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.setJdbcAutoCommit(BaseWrapperManagedConnection.java:968)
        at org.jboss.jca.adapters.jdbc.WrappedConnection.setAutoCommit(WrappedConnection.java:716)
        at org.hibernate.jpa.internal.schemagen.JpaSchemaGenerator$ConnectionProviderJdbcConnectionAccess.<init>(JpaSchemaGenerator.java:706)
        ... 73 more

07:43:50,067 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 1) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "my3dplayer.war")]) - failure description: {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"my3dplayer.war\".component.InitDefaultSampleThumbs.START" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"my3dplayer.war\".component.InitDefaultSampleThumbs.START: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
    Caused by: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
    Caused by: javax.ejb.EJBException: org.jboss.weld.exceptions.CreationException: WELD-000079: Could not find the EJB in JNDI:  class com.visiativ.my3dplayer.repository.AttachmentRepository$Proxy$_$$_Weld$EnterpriseProxy$
    Caused by: org.jboss.weld.exceptions.CreationException: WELD-000079: Could not find the EJB in JNDI:  class com.visiativ.my3dplayer.repository.AttachmentRepository$Proxy$_$$_Weld$EnterpriseProxy$
    Caused by: javax.persistence.PersistenceException: Could not set provided connection [org.jboss.jca.adapters.jdbc.jdk7.WrappedConnectionJDK7@63454e9f] to auto-commit mode (needed for schema generation)
    Caused by: java.sql.SQLException: You cannot set autocommit during a managed transaction!"}}
07:43:50,073 ERROR [org.jboss.as.server] (management-handler-thread - 1) JBAS015870: Deploy of deployment "my3dplayer.war" was rolled back with the following failure message:
{"JBAS014671: Failed services" => {"jboss.deployment.unit.\"my3dplayer.war\".component.InitDefaultSampleThumbs.START" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"my3dplayer.war\".component.InitDefaultSampleThumbs.START: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
    Caused by: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
    Caused by: javax.ejb.EJBException: org.jboss.weld.exceptions.CreationException: WELD-000079: Could not find the EJB in JNDI:  class com.visiativ.my3dplayer.repository.AttachmentRepository$Proxy$_$$_Weld$EnterpriseProxy$
    Caused by: org.jboss.weld.exceptions.CreationException: WELD-000079: Could not find the EJB in JNDI:  class com.visiativ.my3dplayer.repository.AttachmentRepository$Proxy$_$$_Weld$EnterpriseProxy$
    Caused by: javax.persistence.PersistenceException: Could not set provided connection [org.jboss.jca.adapters.jdbc.jdk7.WrappedConnectionJDK7@63454e9f] to auto-commit mode (needed for schema generation)
    Caused by: java.sql.SQLException: You cannot set autocommit during a managed transaction!"}}


This is my persistence.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
   xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://xmlns.jcp.org/xml/ns/persistence
        http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

   <persistence-unit name="local_dev">

      <!-- <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> -->
      <provider>org.hibernate.ejb.HibernatePersistence</provider>

      <jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source>

      <properties>
         <!-- <property name="hibernate.hbm2ddl.auto" value="update" /> -->
         <!-- <property name="hibernate.connection.autocommit" value="after_transaction"
            /> -->
         <!-- <property name="hibernate.show_sql" value="false" /> -->
         <!-- more info on generation ppts: https://www.google.com/search?q=javax.persistence.schema-generation.drop-source&ie=utf-8&oe=utf-8 -->
         <!-- <property name="javax.persistence.schema-generation.database.action" -->
         <!-- value="drop-and-create" /> -->
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
         <property name="javax.persistence.schema-generation.database.action"
            value="drop-and-create" />
         <property name="javax.persistence.schema-generation.scripts.action"
            value="drop-and-create" />
         <property name="javax.persistence.schema-generation.create-source"
            value="metadata" />
         <property name="javax.persistence.schema-generation.drop-source"
            value="metadata" />
         <!-- <property name="javax.persistence.schema-generation-target" value="database"/> -->

         <!-- for script generation -->
         <property name="javax.persistence.schema-generation-target"
            value="database-and-scripts" />
         <property name="javax.persistence.schema-generation.scripts.create-target"
            value="file:/c:/tmp/sql/create.ddl" />
         <property name="javax.persistence.schema-generation.scripts.drop-target"
            value="file:/c:/tmp/sql/drop.ddl" />
         <property
            name="javax.persistence.schema-generation.create-database-schemas"
            value="true" />

         <!-- Populate the database -->
         <property name="javax.persistence.sql-load-script-source"
            value="sql/load.sql" />
      </properties>

   </persistence-unit>

   <persistence-unit name="preprod">
      <!-- <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> -->
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source>

      <properties>
         <property name="hibernate.show_sql" value="false" />
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />

      </properties>

   </persistence-unit>

   <persistence-unit name="prod">

      <!-- <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> -->
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source>

      <properties>
         <property name="hibernate.show_sql" value="false" />
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
      </properties>
   </persistence-unit>

</persistence>


Top
 Profile  
 
 Post subject: Re: Changing to programmatic style raises exception
PostPosted: Thu Apr 14, 2016 4:17 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
If you're bootstrapping the EntityManagerFactory programmatically, the Java EE container will not be able to find it.

What you need to do, is to use CDI Producers as explained in this post.


Top
 Profile  
 
 Post subject: Re: Changing to programmatic style raises exception
PostPosted: Thu Apr 14, 2016 4:24 am 
Newbie

Joined: Thu Apr 14, 2016 4:01 am
Posts: 2
Hi ElArbi

Are you using Java EE? If so try using a producer to inject your EntityManager

Code:
@Produces
public EntityManager create() {
    //read some property from the database and then construct your entity manager based upon the property
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence unit name which you want based upon something");
        EntityManager em = emf.createEntityManager();
       
        return em;
}


Top
 Profile  
 
 Post subject: Re: Changing to programmatic style raises exception
PostPosted: Thu Apr 14, 2016 4:31 am 
Newbie

Joined: Tue Nov 24, 2015 3:52 pm
Posts: 12
Excellent !
Thanks a lot, I'll try this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.