如何在JPA中将数据库用作数据库源 [英] How to use a database as your database source in JPA
问题描述
我是JBoss开发应用程序的新手,因此我专门制作了Ticket Monster的教程。现在,我已经创建了一个Event JPA Entity,并且在运行中,它表明我可以保存一个事件。但是,当我重新启动计算机时,似乎我保存的事件已丢失,因此研究它并找到有关内存数据库的一些信息。我的问题是如何告诉/配置我的项目不使用内存数据库,而是使用典型的数据库,所以每次重新启动计算机时数据都在数据库中。我想将PostgreSQL用于我的数据库。我当前设置为JPA的数据源配置文件是 TestDB
但我的persistence.xml的数据源是
I'm new in developing apps in JBoss so I followed their tutorials specially in making the Ticket Monster. For now, I've created an Event JPA Entity and in the run, it shows that I can save an event. But when I restart my computer, it seems the event I've saved is lost, so research about it and found some info about in-memory database. My problem is how do I tell/configure my project not to use in-memory database but to use the typical database so every time I restarted my computer the data was in the database. I would like to use PostgreSQL for my database. My current datasource profile that is set to JPA is TestDB
but the datasource of my persistence.xml is
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="primary">
<!-- If you are running in a production environment, add a managed
data source, this example data source is just for development and testing! -->
<!-- The datasource is deployed as WEB-INF/ticket-monster-ds.xml, you
can find it in the source at src/main/webapp/WEB-INF/ticket-monster-ds.xml -->
<jta-data-source>java:jboss/datasources/ticket-monsterDS</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
它说< jta-data-source> java:jboss / datasources / ticket-monsterDS< / jta-data-source>
而不是 TestDB
。此外,我不想使用 create-drop
,而是可以判断数据库中是否存在某些内容但不删除但使用它的值,或者如果不是现有。
it said that <jta-data-source>java:jboss/datasources/ticket-monsterDS</jta-data-source>
not the TestDB
. Also, I would like not use the create-drop
but instead a value that could tell if there is something in the database do not drop it but use it and or create if no existing.
更新
以下我的票 - monster-ds.xml
Heres my ticket-monster-ds.xml
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
<!-- The datasource is bound into JNDI at this location. We reference
this in META-INF/persistence.xml -->
<datasource jndi-name="java:jboss/datasources/ticket-monsterDS"
pool-name="ticket-monster" enabled="true"
use-java-context="true">
<connection-url>jdbc:h2:mem:ticket-monster;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
</datasources>
推荐答案
您可以继续使用示例中的H2数据库。 H2可以配置为写入文件。您只需要更改连接字符串。
You can continue to use the H2 database from the example. H2 can be configured to write to a file. You just need to change your connect string.
例如。 < connection-url> jdbc:h2:〜/ test;< connection-url>
查看 H2文档了解更多选项。
这篇关于如何在JPA中将数据库用作数据库源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!