如何在 jpa2 中使用内存中的 derby?

发布于 2024-10-01 07:59:32 字数 893 浏览 5 评论 0原文

无论以下 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

Regardless of the following persistence.xml configuration the database is persisted on disk.

  <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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

私野 2024-10-08 07:59:32

我在内存中使用 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

I'm using Derby in memory for testing and my configuration is very close to yours.

Here is my 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>

Where org.apache.derby:derby:jar:10.6.2.1 is on the classpath.

Now two questions/suggestions:

  • How do you know the database is persisted on disk?
  • If it really does, make sure you're using the persistence.xml you think you are.
白馒头 2024-10-08 07:59:32

将以下内容添加到您的 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>

Add the following to your maven configuration

<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>

The following persistance.xml works pretty well with 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文