使用HSQL内存数据库作为JPA数据源 [英] Using HSQL in-memory database as JPA datasource

查看:114
本文介绍了使用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> / *在这里输入什么?* /< / jta-data-source>

<属性>

< property name =hibernate.dialectvalue =org.hibernate.dialect.HSQLDialect/>
< property name =hibernate.hbm2ddl.autovalue =create-drop/>

< / properties>

< / persistence-unit>

< /持久性>


解决方案


/ *进入这里?* /


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

 < persistence xmlns =http://java.sun.com/xml/ns/persistencexmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation = http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsdversion =2.0> 
< persistence-unit name =managertransaction-type =RESOURCE_LOCAL>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< class> com.acme.Foo< / class>
...
<属性>
< property name =javax.persistence.jdbc.drivervalue =org.hsqldb.jdbcDriver/>
< property name =javax.persistence.jdbc.urlvalue =jdbc:hsqldb:mem:testdb/>
< property name =javax.persistence.jdbc.uservalue =sa/>
< property name =javax.persistence.jdbc.passwordvalue =/>
< property name =hibernate.dialectvalue =org.hibernate.dialect.HSQLDialect/>
< property name =hibernate.hbm2ddl.autovalue =update/>
< / properties>
< / persistence-unit>
< /余辉>


I have an in-memory data source:

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

But now I'm stuck. I want to use it as a JPA data source in a J2SE application. I've scoured the entire web but all info is related to 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>

解决方案

/*What to enter here?*/

Well, nothing. In a Java SE environment, you'll have to use the built-in connection pool from your JPA provider and the setup looks like this:

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

这篇关于使用HSQL内存数据库作为JPA数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆