我正在尝试从带注释的EJB实体创建数据库。 使用:Eclipse,Ant工具,Apache Tomee服务器,OpenJPA,JEE / EJB,MySQL数据库。
我的应用程序在注入实体管理器期间失败(假设在注释的实体类之后触发数据库创建)。一些关键的应用程序部分:
异常我看起来像这样:
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任务:
<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”类(从此ant任务开始)在其主要方法中有下一个代码:
Context ctx = new InitialContext();
Init init = (Init) ctx.lookup("InitBeanRemote");
System.out.println("Bean found!");
init.init();
“InitBean”类,其中注入了实体管理器:
@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 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>
tomee.xml配置文件中的数据源定义:
<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>
(奇怪的是我以前运行过这个应用程序,过去工作正常。我正在获取数据库,并运行应用程序。) 感谢任何建议。
答案 0 :(得分:2)
我的错误! (还在学习)。
以上代码和设置都很好。我认为持久性提供程序(openJPA)不仅创建数据库表,还创建数据库本身。我认为,因为数据源定义中包含“JdbcUrl属性”(包含数据库名称)。显然,我需要创建数据库,而持久性提供程序只是创建现有数据库的表。所以,我已经(明确地)使用mysql终端命令“create database Vezbe09”创建了数据库。 ,部署我的应用程序,在触发openJPA的ant任务上面运行,我在我的数据库中得到了表。