سؤال

I am trying to create database from annotated EJB entities. Using: Eclipse, Ant tool, Apache Tomee server, OpenJPA, JEE/EJB's, MySQL db.

My app fails during injecting entity manager(that is suppose to trigger db creation following annotated entity classes). Some crucial app parts:

Exception i am getting looks like this:

prepare-database:
     [echo] Inserting default user into database.
     [java] Dec 09, 2013 9:45:29 AM org.apache.openejb.client.EventLogger log
     [java] INFO: RemoteInitialContextCreated{providerUri=http://127.0.0.1:8080/tomee/ejb}
     [java] Bean found
     [java] Exception in thread "main" javax.ejb.EJBException: The bean encountered a non-application exception; nested exception is: 
     [java]     <openjpa-2.2.0-r422266:1244990 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: There were errors initializing your configuration: <openjpa-2.2.0-r422266:1244990 fatal user error> org.apache.openjpa.util.UserException: A connection could not be obtained for driver class "null" and URL "null".  You may have specified an invalid URL.
     [java]     at org.apache.openjpa.jdbc.schema.DataSourceFactory.newConnectException(DataSourceFactory.java:255)
     [java]     at org.apache.openjpa.jdbc.schema.DataSourceFactory.installDBDictionary(DataSourceFactory.java:241)
     [java]     at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getConnectionFactory(JDBCConfigurationImpl.java:733)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
     [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     [java]     at java.lang.reflect.Method.invoke(Method.java:606)
     [java]     at org.apache.openjpa.lib.conf.ConfigurationImpl.instantiateAll(ConfigurationImpl.java:295)
     [java]     at org.apache.openjpa.conf.OpenJPAConfigurationImpl.instantiateAll(OpenJPAConfigurationImpl.java:1671)
     [java]     at org.apache.openjpa.kernel.AbstractBrokerFactory.makeReadOnly(AbstractBrokerFactory.java:646)
     [java]     at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:203)
     [java]     at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:156)
     [java]     at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:227)
     [java]     at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:60)
     [java]     at org.apache.openejb.assembler.classic.ReloadableEntityManagerFactory.createEntityManager(ReloadableEntityManagerFactory.java:160)
     [java]     at org.apache.openejb.persistence.JtaEntityManagerRegistry.getEntityManager(JtaEntityManagerRegistry.java:115)
     [java]     at org.apache.openejb.persistence.JtaEntityManager.getEntityManager(JtaEntityManager.java:80)
     [java]     at org.apache.openejb.persistence.JtaEntityManager.persist(JtaEntityManager.java:126)
     [java]     at rs.ac.uns.ftn.informatika.mbs2.vezbe09.primer01.server.session.InitBean.init(InitBean.java:22)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

and so on ....

Ant task for triggering database generation:

<target name="prepare-database">
        <echo message="Inserting default user into database."/>
        <java classname="rs.ac.uns.ftn.informatika.mbs2.vezbe09.primer01.client.InitClient"  fork="true">
            <classpath>
                <pathelement path="${build}" />
                <fileset dir="${lib}">
                    <include name="*.jar"/>
                </fileset>
            </classpath>
        </java>
</target>

"InitClient" class (started from this ant task) has next code in its main method:

Context ctx = new InitialContext();
            Init init = (Init) ctx.lookup("InitBeanRemote");
            System.out.println("Bean found!");
            init.init();

"InitBean" class, where entity manager is being injected:

@Stateless
@Remote(Init.class)
public class InitBean implements Init {

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

    public void init() {

        Korisnik korisnik = new Korisnik("Admin", "Admin", "admin", "admin");
        em.persist(korisnik);
          //...
        }
}

persistent.xml - persistence unit definition:

<persistence-unit name="Vezbe09" transaction-type="JTA">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <jta-data-source>Vezbe09DS</jta-data-source>
    <properties>
        <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
        <property name="openjpa.jdbc.DBDictionary" value="mysql" />
        <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
    </properties>
</persistence-unit>

Data source definition in tomee.xml configuration file:

<Resource id="Vezbe09DS" type="DataSource">
    JdbcDriver          com.mysql.jdbc.Driver
    JdbcUrl             jdbc:mysql://127.0.0.1:3306/Vezbe09
    UserName            root
    Password            123
    JtaManaged          true
    DefaultAutoCommit   false
</Resource>

(Strange thing is that I have run this application before, in the past, and it was working fine. I was getting database, and running application.) Appreciate any suggestion.

هل كانت مفيدة؟

المحلول

MY FAULT!!! (still learning).

Above code and settings are fine. I thought that persistence provider(openJPA) is creating not only database tables, but database itself also. I thought that, because of given "JdbcUrl property" in the datasource definition (which contains db name). Obviously, i need to have created database, and persistence provider is creating just tables to existing database. So, I have (explicitly) created database using mysql terminal command "create database Vezbe09;" , deployed my app, run above ant task that triggers openJPA, and I got tables in my database.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top