EclipseLink可以创建persistence.xml中指定的数据库吗?

时间:2015-04-27 06:17:22

标签: mysql jpa eclipselink

我正在开始Java EE 7的练习,除了我试图让它们适应使用MySQL而不是Derby。对于chapter 4 JPA exercise,我将persistence.xml更改为:

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
             version="2.1">

  <persistence-unit name="chapter04PU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>org.agoncal.book.javaee7.chapter04.Book</class>
    <properties>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
      <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
      <property name="javax.persistence.schema-generation.scripts.create-target" value="chapter04Create.ddl"/>
      <property name="javax.persistence.schema-generation.scripts.drop-target" value="chapter04Drop.ddl"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/chapter04DB"/>
      <property name="javax.persistence.jdbc.user" value="USER"/>
      <property name="javax.persistence.jdbc.password" value="PASS"/>
      <property name="javax.persistence.sql-load-script-source" value="insert.sql"/>
      <property name="eclipselink.logging.level" value="INFO"/>
      <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
    </properties>
  </persistence-unit>

  <persistence-unit name="chapter04TestPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>org.agoncal.book.javaee7.chapter04.Book</class>
    <properties>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
      <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
      <property name="javax.persistence.schema-generation.scripts.create-target" value="create.sql"/>
      <property name="javax.persistence.schema-generation.scripts.drop-target" value="drop.sql"/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:chapter0`enter code here`4DB;create=true"/>
      <property name="javax.persistence.sql-load-script-source" value="insert.sql"/>
      <property name="eclipselink.logging.level" value="INFO"/>
    </properties>
  </persistence-unit>

</persistence>

一切都很好,如果我去数据库服务器并创建数据库,jar运行正常。但是我试图让EclipseLink在运行时创建db(正如本书暗示Derby会这样做),如果运行jar时db不存在,我会收到此错误:

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'chapter04DB'

如何让EclipseLink在运行时创建MySQL数据库?

1 个答案:

答案 0 :(得分:1)

您不能,因为自动创建数据库是Derby功能,而不是自动创建架构,这可以由EclipseLink使用

<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>