How to use a database as your database source in JPA

邮差的信 提交于 2019-12-02 10:37:18

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!