5

不管下面的 persistence.xml 配置如何,数据库都保存在磁盘上。

  <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

4

2 回答 2

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

现在有两个问题/建议:

  • 你怎么知道数据库持久化在磁盘上?
  • 如果确实如此,请确保您使用的是persistence.xml您认为的。
于 2010-11-06T22:57:09.767 回答
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>
于 2010-11-06T22:31:13.217 回答