使用HSQL内存数据库作为JPA数据源

时间:2010-09-27 13:47:27

标签: hibernate jpa persistence jpa-2.0 hsqldb

我有一个内存数据源:

java.sql.Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:testdb", "sa", "");            
emf = Persistence.createEntityManagerFactory("manager");

但现在我被卡住了。我想将它用作J2SE应用程序中的JPA数据源。我已经浏览了整个网络,但所有信息都与J2EE有关。

<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="manager">

        <jta-data-source>/*What to enter here?*/</jta-data-source>

        <properties>

            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />

        </properties>

    </persistence-unit>

</persistence>

1 个答案:

答案 0 :(得分:56)

  

/ *这里输入什么?* /

嗯,没什么。在Java SE环境中,您必须使用JPA提供程序中的内置连接池,设置如下所示:

<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="manager" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.acme.Foo</class>
    ...
    <properties>
      <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb"/>
      <property name="javax.persistence.jdbc.user" value="sa"/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
  </persistence-unit>
</persistence>