我是 JBoss 开发应用程序的新手,所以我专门按照他们的教程制作了 Ticket Monster。现在,我已经创建了一个事件 JPA 实体,并且在运行中,它显示我可以保存事件。但是当我重新启动计算机时,我保存的事件似乎丢失了,因此对其进行了研究并找到了一些有关内存数据库的信息。我的问题是如何告诉/配置我的项目不使用内存数据库而是使用典型数据库,以便每次重新启动计算机时数据都在数据库中。我想使用 PostgreSQL 作为我的数据库。我当前设置为 JPA 的数据源配置文件是 TestDB
但我的 persistence.xml 的数据源是
<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
而是一个可以判断数据库中是否有某些内容的值,不要删除它,而是使用它,或者如果不存在则创建它。
更新
这是我的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 可以配置为写入文件。您只需要更改连接字符串即可。
例如。<connection-url>jdbc:h2:~/test;<connection-url>
请参阅H2 documentation了解更多选项。
关于java - 如何在 JPA 中使用数据库作为数据库源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39628608/