2010-09-27 95 views
20

我有一個在內存中的數據源:使用HSQL的內存數據庫作爲數據源JPA

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> 

回答

55

/*什麼進入這裏?*/

好了,什麼都沒有。在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> 
+0

謝謝,它的工作原理。我現在有另一個問題:http://stackoverflow.com/questions/3805478/internal-hsql-database-complains-about-privileges – 2010-09-27 15:50:37

+0

@Bart不客氣。 – 2010-09-27 16:05:06

+1

@Bart BTW,認識一個好答案的常用方法是對它進行upvoting;)請參閱[接受答案沒有upvoting?](http://meta.stackexchange.com/questions/686/accepting-answer-without-upvoting)。 – 2010-09-27 16:06:20