How to use a database as your database source in JPA

前端 未结 2 722
忘了有多久
忘了有多久 2021-01-26 05:56

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 th

相关标签:
2条回答
  • 2021-01-26 06:34

    So after I posted on the jboss forum, here's the link Wildfly 10: Cannot upload deployment , Wolfgang Mayer answered that I should not declared a datasource more than once. Here's the step.

    1.You can set a datasource via your Admin Console(localhost:9990) or via your Project(*-ds.xml), not both. Please remember the JNDI(Java Naming and Directory Interface) of your Datasource.

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

    2.Tell your project or war to use your datasource you've created. Edit your pesistence.xml and put the JNDI of your datasource.

    Example:

    <?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.(Optional) If you created your datasource in Admin Console, delete your *-ds.xml file in your project folder to prevent the "Datasource is already registered" error.

    0 讨论(0)
  • 2021-01-26 06:44

    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.

    eg. <connection-url>jdbc:h2:~/test;<connection-url>

    See the H2 documentation for further options.

    0 讨论(0)
提交回复
热议问题