2015-04-27 84 views
0

我正在通過開始Java EE 7中的練習,除了我試圖使它們適應使用MySQL而不是Derby。對於chapter 4 JPA exercise,我改變了的persistence.xml這樣:EclipseLink可以創建在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> 

一切都建立罰款,如果我去了數據庫服務器並創建數據庫,罐子運行良好。但我想獲得的EclipseLink在運行時創建的數據庫(如書中暗示德比都行),如果當我運行jar我得到這個錯誤的數據庫不存在:

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

我如何讓EclipseLink在運行時創建MySQL數據庫?

回答

0

你不能,因爲自動創建數據庫的是德比的功能,而不是使用

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

所以,自動創建模式的,可通過的EclipseLink進行使用MySQL與JPA,我不得不在運行時手動創建數據庫? (我已經確認了作品) – Allen

+0

確切地說 - 我們對PostgreSQL有類似的設置,我們也必須先創建數據庫。 – Smutje

相關問題