我想使用纯JPA 2创建动态时间表。就像在hibernate中一样,我们使用SchemaExport创建动态表。
代码:
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/employee;create=true"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name = "hibernate.show_sql" value = "true" />
<properties>
使用create=true
时会发生以下异常:
WARN: HHH000342: Could not obtain connection to query metadata : Unknown database 'employee;create=true'
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: EmployeeService] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
答案 0 :(得分:0)
您可以在create=true
网址中指定persistence.xml
。
<persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
...
<properties>
---
<property name="javax.persistence.jdbc.url"
value="jdbc:???://localhost:port/DBName;create=true"/>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="javax.persistence.jdbc.password" value="pwd"/>
</properties>
</persistence-unit>
</persistence>