3 Replies Latest reply on Nov 15, 2005 8:38 AM by chelaru.viorel

    Problem creating EntityManagerFactory

    Guilherme Melo e Maranhão Newbie

      Hi everyone,

      I'm doing exactly the way EntiyManager Docummentaion recommends to create an EntityManagerFactory:

      EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager");

      But I receive the exception:

      java.lang.ClassCastException: org.hibernate.ejb.HibernatePersistence

      Does anyone know what's wrong?

      Thanks, Guilherme

        • 1. Re: Problem creating EntityManagerFactory
          Bill Burke Master

          Yes, this API doesn't work inside JBoss AS. Use the J2EE way of doing things: aka @PersistenceContext or you can look up the EM/EMF in JNDI:

          java:/EntityManagers/your-unit-name
          java:/EntityManagerFactories/your-unit-name

          • 2. Re: Problem creating EntityManagerFactory
            Guilherme Melo e Maranhão Newbie

            Ok, but if I use @PersistenceContext(), how do I create my entityManager?
            I am doing in three ways and in both I got exception:
            The first way is:

            @PersistenceContext(unitName="sistemabancario")
            EntityManager em;

            public void mymethod (){
            HibernatePersistence persistence = new HibernatePersistence();
            EntityManagerFactory emf = persistence.createEntityManagerFactory(new HashMap());
            }

            I get:
            org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
            org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)
            org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
            org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:378)
            org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:110)
            org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1797)
            org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1125)
            org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:358)
            org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:484)
            org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:351)
            org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:87)
            sistemaBancario.servidor.facade.logar.LogarFacadeSessionBean.autenticarBancario(LogarFacadeSessionBean.java:46)
            sistemaBancario.cliente.struts.logar.LogarAction.autenticarUsuario(LogarAction.java:72)
            sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            java.lang.reflect.Method.invoke(Method.java:585)
            org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:276)
            org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:196)
            org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
            org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
            org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
            org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
            org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

            The second way is:
            HibernatePersistence persistence = new HibernatePersistence();
            HashMap hash = new HashMap();
            hash.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
            EntityManagerFactory emf = persistence.createEntityManagerFactory(hash);

            I get:
            org.hibernate.HibernateException: Could not instantiate dialect class
            org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:84)
            org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:42)
            org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:378)
            org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:110)
            org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1797)
            org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1125)
            org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:358)
            org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:484)
            org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:351)
            org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:87)
            sistemaBancario.servidor.facade.logar.LogarFacadeSessionBean.autenticarBancario(LogarFacadeSessionBean.java:46)
            sistemaBancario.cliente.struts.logar.LogarAction.autenticarUsuario(LogarAction.java:72)
            sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            java.lang.reflect.Method.invoke(Method.java:585)
            org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:276)
            org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:196)
            org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
            org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
            org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
            org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
            org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

            The third way is:
            HibernatePersistence persistence = new HibernatePersistence();
            HashMap hash = new HashMap();
            hash.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
            EntityManagerFactory emf = persistence.createEntityManagerFactory("sistemabancario", hash);

            I get:
            javax.persistence.PersistenceException: org.hibernate.HibernateException: Could not instantiate dialect class
            org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:149)
            org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:73)
            sistemaBancario.servidor.facade.logar.LogarFacadeSessionBean.autenticarBancario(LogarFacadeSessionBean.java:46)
            sistemaBancario.cliente.struts.logar.LogarAction.autenticarUsuario(LogarAction.java:72)
            sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            java.lang.reflect.Method.invoke(Method.java:585)
            org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:276)
            org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:196)
            org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
            org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
            org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
            org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
            org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

            I have tried with two persistence.xmls:
            One with hibernate.cfg.xml:
            <entity-manager>
            sistemabancario
            org.hibernate.ejb.HibernatePersistence
            <jta-data-source>java:/DefaultDS</jta-data-source>
            sistemaBancario.servidor.persistencia.vo.BancarioVo
            sistemaBancario.servidor.persistencia.vo.PessoaFisicaVo




            </entity-manager>

            Hibernate.cfg.xml:
            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
            <hibernate-configuration>
            <session-factory>
            root

            org.hibernate.dialect.MySQLDialect
            jdbc:mysql://localhost/sistemabancario
            com.mysql.jdbc.Driver
            org.hibernate.cache.HashtableCacheProvider
            </session-factory>
            </hibernate-configuration>

            And other without hibernate.cfg.xml.
            In both cases I got the same exception.

            I 've tested mysql driver and nothing is wrong with it.

            What is this dialect problem?

            Thanks, Guilherme



            • 3. Re: Problem creating EntityManagerFactory
              chelaru.viorel Newbie

              hi,
              i think you should put your sql dialect in your persistence.xml of your ear, or in persistence properties... to load it at deploy. i'm not sure but i think this is the problem.

              Viorel