2016-09-22 35 views
0

中使用數據庫作爲數據庫源我是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> 
+0

我可以看看插入到數據庫的代碼嗎? –

+0

我不知道插入代碼在哪裏,因爲我已經使用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

+0

是的,我指的是(使用'entityManager'的代碼)。 –

回答

1

您可以繼續從示例使用H2數據庫。 H2可以配置爲寫入文件。你只需要改變你的連接字符串。

例如。 <connection-url>jdbc:h2:~/test;<connection-url>

查看H2 documentation瞭解更多選項。

+0

? – netto

+0

不,H2是一個像Postgress一樣的SQL數據庫。您必須啓動並運行Postgres數據庫,並將jdbc連接URL配置爲指向正在運行的Postgres數據庫(如果這是您要使用的數據庫)。 – MarkOfHall

+0

我提供的例子是使用H2作爲示例的一種方式,並讓您在JVM或計算機重新啓動之間保存的數據保存。 – MarkOfHall

0

因此,在我發佈jboss論壇後,這裏是鏈接Wildfly 10: Cannot upload deployment,Wolfgang Mayer回答說我不應該多次聲明一個數據源。這是一步。 ( - ds.xml中*),不能同時或通過您的項目:

1.You可以通過自己的管理控制檯(9990本地主機)設置數據源。請記住數據源的JNDI(Java命名和目錄接口)。

例子: Datasource Name:PostgresDS JNDI:java:/PostgresDS Connection URL: jdbc:postgresql://host:port/databasename

2.Tell項目或戰爭使用您已經創建了數據源。編輯你的pesistence.xml並把你的數據源的JNDI。

例子:

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 
     <jta-data-source>java:/PostgresDS</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> 

3.(可選)如果您在管理控制檯創建了數據源,刪除* -ds.xml文件在項目文件夾,以防止「數據源已經被註冊」的錯誤。