中使用數據庫作爲數據庫源我是JBoss開發應用程序的新手,所以我專門製作了Ticket Monster的教程。現在,我創建了一個Event JPA實體,在運行中,它顯示我可以保存一個事件。但是當我重新啓動計算機時,似乎我保存的事件丟失了,因此研究了它並找到了關於內存數據庫的一些信息。我的問題是如何告訴/配置我的項目不使用內存數據庫,而是使用典型數據庫,這樣每次重新啓動計算機時數據都在數據庫中。我想爲我的數據庫使用PostgreSQL。被設置爲JPA我目前的數據源配置文件是TestDB
但我的persistence.xml的數據源是如何在JPA
<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
,而是使用可以判斷數據庫中是否存在某些內容的值,但不要刪除它,而是使用它或創建(如果不存在)。
UPDATE
我的繼承人票怪物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>
我可以看看插入到數據庫的代碼嗎? –
我不知道插入代碼在哪裏,因爲我已經使用JForge Tools創建了一個EntityManager obj的Endpoint。但繼承人的創建EventEndpoint @POST \t @Consumes( 「應用程序/ JSON」) \t公共響應創建(比賽實體){ \t \t em.persist(實體)的方法; \t \t return Response.created( \t \t \t \t UriBuilder.fromResource(EventEndpoint。類) \t \t \t \t \t \t .path(String.valueOf(entity.getId())。build())。build(); \t} – netto
是的,我指的是(使用'entityManager'的代碼)。 –