当我 运行 程序 JPA 不在 MySQL 中创建 table
When I run program JPA does not create table in MySQL
这是我的实体 Class:
package az.bank.entities;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table (name = "cards")
public class Card implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String cardHolder;
private String cardNumber;
private String cardPassword;
private String expiryYear;
private String expiryMonth;
private String cardType;
private double cardBalance;
}
这是我的 persistance.xml 文件:
<?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="BankServicePU" transaction-type="JTA">
<jta-data-source>jdbc/BankService</jta-data-source>
<class>az.bank.entities.Card</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/cards" />
<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>
我创建了名为 jdbc/BankService 的连接池和名为 cards 的 mySQL 方案。但是当我部署和 运行 程序时,它不会在该方案中创建 table。请帮助我在这里做错了什么。
如果您的数据库中已经包含卡table,那么先将其删除。使用
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
我想在你的情况下你没有初始化 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.Whosebug.AppServletContextListener
</listener-class>
</listener>
</web-app>
Starting Servlet 3.0 you can just use @WebListener annotation instead registration in web.xml
.
AppServletContextListener.java
package com.mberazouski.Whosebug;
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.Whosebug.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-数据源
在这种情况下,所有配置都将从您的 tomcat:
的 Resource
块中获取
<?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.Whosebug.domain.Cards</class>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
tomcat 开始的输出:
不过我也建议你看看spring
的方向。使用它的初始化方法,您可以直接从配置文件初始化 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>
希望这会有所帮助。
这是我的实体 Class:
package az.bank.entities;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table (name = "cards")
public class Card implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String cardHolder;
private String cardNumber;
private String cardPassword;
private String expiryYear;
private String expiryMonth;
private String cardType;
private double cardBalance;
}
这是我的 persistance.xml 文件:
<?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="BankServicePU" transaction-type="JTA">
<jta-data-source>jdbc/BankService</jta-data-source>
<class>az.bank.entities.Card</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/cards" />
<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>
我创建了名为 jdbc/BankService 的连接池和名为 cards 的 mySQL 方案。但是当我部署和 运行 程序时,它不会在该方案中创建 table。请帮助我在这里做错了什么。
如果您的数据库中已经包含卡table,那么先将其删除。使用
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
我想在你的情况下你没有初始化 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.Whosebug.AppServletContextListener
</listener-class>
</listener>
</web-app>
Starting Servlet 3.0 you can just use @WebListener annotation instead registration in
web.xml
.
AppServletContextListener.java
package com.mberazouski.Whosebug;
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.Whosebug.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-数据源
在这种情况下,所有配置都将从您的 tomcat:
的Resource
块中获取
<?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.Whosebug.domain.Cards</class>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
tomcat 开始的输出:
不过我也建议你看看spring
的方向。使用它的初始化方法,您可以直接从配置文件初始化 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>
希望这会有所帮助。