2010-11-06 44 views
5

無論以下persistence.xml配置如何,數據庫都保存在磁盤上。如何使用jpa2在內存中使用德比?

<persistence-unit name="com.mysimpatico_inmemory_persistence_nbm_1.0-SNAPSHOTPU" transaction-type="RESOURCE_LOCAL"> 
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
<class>com.mysimpatico.memoplatform.persistence.entities.Expression</class> 
<properties> 
    <property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:tempDb;create=true"/> 
    <property name="javax.persistence.jdbc.password" value=""/> 
    <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/> 
    <property name="javax.persistence.jdbc.user" value=""/> 
</properties> 

http://wiki.apache.org/db-derby/InMemoryBackEndPrimer

回答

9

我在內存中使用Derby進行測試,我的配置與您的配置非常接近。

這裏是我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?> 
<persistence 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" version="2.0"> 
    <persistence-unit name="TestPu" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
    <class>com.acme.Foo</class> 
    <class>com.acme.Bar</class> 
    <exclude-unlisted-classes>true</exclude-unlisted-classes> 
    <properties> 
     <!-- Common properties --> 
     <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/> 
     <property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:test-jpa;create=true"/> 
     <property name="javax.persistence.jdbc.user" value="APP"/> 
     <property name="javax.persistence.jdbc.password" value="APP"/> 

     <!-- EclipseLink specific properties --> 
     <property name="eclipselink.target-database" value="Derby"/> 
     <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> 
     <property name="eclipselink.debug" value="ALL"/> 
     <property name="eclipselink.weaving" value="static"/> 
     <property name="eclipselink.logging.level" value="FINEST"/> 
     <property name="eclipselink.logging.level.sql" value="FINEST"/> 
     <property name="eclipselink.logging.level.cache" value="FINEST"/> 
    </properties> 
    </persistence-unit> 
</persistence> 

org.apache.derby:derby:jar:10.6.2.1是在classpath。

現在兩個問題/建議:

  • 你怎麼知道數據庫堅持在磁盤上?
  • 如果確實如此,請確保您使用的是您認爲自己的persistence.xml
+0

它只是我還是這個答案的持久性XML不能正確顯示?我甚至試圖解決它,但它顯示錯誤都是一樣的.. – Ben 2016-08-19 11:30:28

4

以下添加到您的Maven配置

<dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-entitymanager</artifactId> 
    <version>3.3.2.GA</version> 
    <scope>test</scope> 
</dependency> 
<dependency> 
    <groupId>org.apache.derby</groupId> 
    <artifactId>derby</artifactId> 
    <version>10.5.3.0</version> 
    <scope>test</scope> 
</dependency> 

以下persistance.xml效果很好用Hibernate。

<persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <class>myapp.model.entities.Group</class> 
    <class>myapp.model.entities.User</class> 
    <exclude-unlisted-classes>true</exclude-unlisted-classes> 
    <properties> 
     <property name="hibernate.connection.url" value="jdbc:derby:memory:jpa"/> 
     <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver"/> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/> 
     <property name="hibernate.hbm2ddl.auto" value="create"/> 
     <property name="hibernate.connection.username" value=""/> 
     <property name="hibernate.connection.password" value=""/> 
    </properties> 
</persistence-unit> 
+0

這是否意味着它不可能與eclipselink? – simpatico 2010-11-06 22:41:05

+0

這絕對是可能的,沒有什麼強迫你切換到休眠狀態。持久性提供者與您的問題IMO無關。 – 2010-11-06 22:58:34

+0

是的,但是您使用hibernate屬性而不是使用javax.persistence。*屬性來定義所有內容。這使得它完全依賴於休眠。它必須使用通用的javax.persistence。*屬性重寫。 – Ben 2016-08-19 11:10:26