我想,你的情況,你不初始化EntityManagerFactory
,這意味着你的EclipseLink不接收命令來創建一個表,甚至連接到數據庫。
在你的情況下,你可以嘗試使用ServletContextListener
,它必須與你的web.xml
文件重組。
簡單的例子:
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<listener>
<listener-class>
com.mberazouski.stackoverflow.AppServletContextListener
</listener-class>
</listener>
</web-app>
開始的Servlet 3.0,你可以只使用@WebListener註釋,而不是登記web.xml
。
AppServletContextListener.java
package com.mberazouski.stackoverflow;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class AppServletContextListener implements ServletContextListener {
private static EntityManagerFactory emf;
public void contextInitialized(ServletContextEvent event) {
emf = Persistence.createEntityManagerFactory("default");
createEntityManager();
}
public void contextDestroyed(ServletContextEvent event) {
emf.close();
}
public static EntityManager createEntityManager() {
if (emf == null) {
throw new IllegalStateException("Context is not initialized yet.");
}
return emf.createEntityManager();
}
}
所以你persistence.xml
是絕對有效的。適用於我的域模型文件看起來像:
RESOURCE_LOCAL
您可以配置通過RESOURCE_LOCAL
的連接。
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<class>com.mberazouski.stackoverflow.domain.Cards</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/rsreu"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
JTA - 數據 - 源
在這種情況下,所有的配置將來自Resource
塊採取從你的Tomcat:
的Tomcat
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="default" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:comp/env/jdbc/EclispeLinkDB</jta-data-source>
<class>com.mberazouski.stackoverflow.domain.Cards</class>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
輸出開始:
但我也建議你看看的方向。使用初始化方法,您可以直接從配置文件初始化EntityManagerFactory
。
的applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="emf" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean ">
<property name="persistenceUnitName" value="default"/>
</bean>
</beans>
希望這會有所幫助。
什麼是持久性提供程序?冬眠?數據庫是否已經包含卡表,並且您希望每次都刪除和替換? –
我的持久性提供者是eclipselink。但我也將其改爲休眠。同樣的問題依然存在。 –
爲什麼不看看你的jpa提供程序的日誌,因爲這是它存在的原因? – DN1