【问题标题】:CDI + JPA + EJB + JTA + JSF --JBoss-Kitchen Example with PostgreSQL?CDI + JPA + EJB + JTA + JSF --JBoss-Kitchen 使用 PostgreSQL 的例子?
【发布时间】:2013-09-21 09:09:01
【问题描述】:

我正在尝试学习新技术CDI + JPA + EJB + JTA + JSF 所以,我已经从JBoss-Community 下载了示例项目--> jboss-as-kitchensink(来自我的 Eclipse juno 中的 JBoss)。

我正在尝试将 kitchen 与我的 PostgreSQL 数据库连接:
我的数据库名称是:sampledb

我的表(成员)结构是:数据库名称QUICKSTART_DATABASENAME

CREATE TABLE member
(
  id integer, 
  name text,
  email text,
  phone_number numeric
);

我在我的厨房项目中编辑了这些文件:

persistence.xml

<persistence version="2.0"
   xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
   <persistence-unit name="primary">
     <jta-data-source>java:jboss/datasources/PostgreSQLDS</jta-data-source>
      <properties>
         <!-- Properties for Hibernate -->
          <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
           <property name="hibernate.show_sql" value="true"/>

      </properties>
   </persistence-unit>
</persistence>

我在pom.xml中添加了依赖:

<!-- PostgreSQL Dependency -->
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901.jdbc4</version>
    </dependency>

但我收到错误:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.554s
[INFO] Finished at: Sat Sep 21 14:01:48 IST 2013
[INFO] Final Memory: 20M/175M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.jboss.as.plugins:jboss-as-maven-plugin:7.2.Final:deploy (default-cli) on project jboss-as-kitchensink: Deployment f
ailed and was rolled back. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
C:\Users\mypc\java\workspace\jboss-as-kitchensink>

我应该怎么做才能使用我的数据库运行这个应用程序来插入数据。

我是否需要更改 .java 文件或任何 .xml 文件中的任何代码才能使其正常工作?

评论后编辑:Following this link,我已手动将我的 PostgreSQL 配置为 JBoss 通过设置JBoss的standalone-full.xml文件..

    ----///----    <datasources>
                        <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
                            <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
                            <driver>h2</driver>
                            <security>
                                <user-name>sa</user-name>
                                <password>sa</password>
                            </security>
                        </datasource>

                        <datasource jndi-name="java:jboss/datasources/PostgreSQLDS" pool-name="PostgreSQLpool" enabled="true" use-java-context="true">
                            <connection-url>jdbc:postgresql://localhost:5432/QUICKSTART_DATABASENAME</connection-url>
                            <driver>org.postgresql.Driver</driver>
                            <security>
                                <user-name>postgres</user-name>
                                <password>postgres</password>
                            </security>
                        </datasource>
                        <drivers>
                            <driver name="h2" module="com.h2database.h2">
                                <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                            </driver>

                            <driver name="postgresql" module="org.postgresql">
                                <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
                            </driver>
                        </drivers>
                    </datasources>
----////---

答案:

安装 PostgreSQL 驱动并使用 XML,这样你就可以让它工作了。

按照步骤操作:(@Craig Ringer 建议)非常有帮助

Download PgJDBC。我假设您使用的是 postgresql-9.2-1003.jdbc4.jar,这是撰写本文时的当前版本。如果您需要不同的版本,请调整任何文件名以匹配。

现在将 JDBC 驱动程序部署到 JBoss AS 7,方法是将其放在部署文件夹中或使用 jboss-cli 中的部署命令。这适用于大多数(但不是全部)目的。

或者,您可以定义一个 PostgreSQL JDBC 驱动程序模块:

创建路径 $JBOSS_HOME/modules/org/postgresql/main。 modules/org 部分应该已经存在,为其余部分创建目录。

在带有以下内容的 $JBOSS_HOME/modules/org/postgresql/main/modules.xml 中,更改 PgJDBC 驱动程序的资源根条目以引用您希望使用的驱动程序。

 <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.1" name="org.postgresql">
         <resources>
             <resource-root path="postgresql-9.2-1003.jdbc4.jar"/>
         </resources>
         <dependencies>
             <module name="javax.api"/>
             <module name="javax.transaction.api"/>
             <module name="javax.servlet.api" optional="true"/>
         </dependencies>
     </module>

Into the same directory as modules.xml place postgresql-9.2-1003.jdbc4.jar
Open jboss-cli by running $JBOSS_HOME/bin/jboss-cli --connect

运行命令:

/subsystem=datasources/jdbc-driver=postgresql-driver:add(driver-name=postgresql-driver, driver-class-name=org.postgresql.Driver, driver-module-name=org.postgresql)

现在创建任何所需的数据源等,使用 postgresql-driver 作为驱动程序名称。

您可以通过 web ui 创建数据源,使用 jboss-cli 和 data-source create 命令(参见 data-source --help, data-source add --help),或通过部署 -ds.xml像这样的文件:

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
  <datasource jndi-name="java:/datasources/PostgresqlDS" enabled="true" use-java-context="true"  
        pool-name="PostgresqlDS">
    <connection-url>jdbc:postgresql:dbname</connection-url>
    <driver>postgresql-driver</driver>
    <security>
      <user-name>username</user-name>
      <password>password</password>
    </security>
  </datasource>
</datasources>

对于 KitchenSink 示例:我尝试了这些,它一直工作到连接和所有东西。仍然应该稍后更新......

Follow this link直到创建一个数据库和一个用户并从这里继续剩下的! 将持久性更改为: persitance.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
   <persistence-unit name="primary">
      <jta-data-source>java:jboss/datasources/KitchenQuickStartDS</jta-data-source>
      <properties>
         <!-- Properties for Hibernate -->
         <property name="hibernate.show_sql" value="true" />
      </properties>
   </persistence-unit>
</persistence>

test-persistance.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
   xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
   <persistence-unit name="primary">
      <jta-data-source>java:jboss/datasources/KitchensinkQuickstartTestDS</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>

test-ds.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <datasource jndi-name="java:jboss/datasources/KitchenQuickStartTestDS"
        pool-name="KitchenQuickStartTestDS" enabled="true"
        use-java-context="true">
      <connection-url>jdbc:postgresql://localhost:5432/QUICKSTART_DATABASENAME</connection-url>
        <driver>postgresql-driver</driver>
        <security>
            <user-name>username</user-name>
            <password>password</password>
        </security>
   </datasource>
</datasources>

kitchensink-quickstart-ds.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/KitchenQuickStartDS"
        pool-name="KitchenQuickStartDS" enabled="true"
        use-java-context="true">
        <connection-url>jdbc:postgresql://localhost:5432/QUICKSTART_DATABASENAME</connection-url>
        <driver>postgresql-driver</driver>
        <security>
            <user-name>username</user-name>
            <password>password</password>
        </security>
    </datasource>
</datasources>

但我陷入困境的是: 我无法在数据库中插入数据!它引发了一个错误

ERROR: relation "hibernate_sequence" does not exist Position: 17 

我需要帮助来配置它.. 我在 MANY ways 尝试过

【问题讨论】:

  • 你的 JBoss 运行了吗?您是否已将“PostgreSQL”数据源添加到您的 JBoss?
  • 我有 JBoss 在我的命令提示符下运行!通过使用standalone.bat,但我没有添加数据源!@fascynacja
  • Configuring PostgreSQL to JBOSS,但我被困在 JBOSS_HOME/bin/jboss-cli.sh --connect --file=configure-postgresql.cli(STEP).. 它打开一个文件 jboss-cli ..我应该如何继续修复它! @fascynacja
  • 即使我在配置中配置了 POSTGRESQL,它也不起作用!我已经编辑了问题
  • 另一个问题是:JBoss 控制台输出是什么 - 在您运行 maven 命令后出现?

标签: java postgresql jboss jboss7.x cdi


【解决方案1】:

我不知道您是否还需要这方面的帮助,但我在将我的应用程序部署到 Openshift 时遇到了类似的错误。

首先我通过 SSH 连接到 openshift 控制台。然后我运行psql(您可以从$POSTGRES_HOME/bin/psql 在您的主机上运行psql),并使用我的postgres 连接凭据并使用\ds\dt 来确定hibernate_sequence 是一个表而不是一个序列。

所以我放弃了它:DROP TABLE hibernate_sequence;

并将其重新创建为一个序列:CREATE SEQUENCE hibernate_sequence;

【讨论】:

  • 我的数据库:QUICKSTART_DATABASENAME-# \dt public | member | table | postgres &amp; QUICKSTART_DATABASENAME-# \dl Large objects ID | Owner | Description ----+-------+------------- (0 rows)
  • 成功了!我创建了一个新序列,然后纠正了在将字符串转换为长字符串和类型转换时略微引发的错误!谢谢你
【解决方案2】:

对于引发的错误:ERROR: relation "hibernate_sequence" does not exist Position: 17

按照@Martin Nuc 的建议,我创建了一个SEQUENCE

要检查数据库中是否存在序列,请在数据库中使用\ds : Lists the Sequences

CREATE SEQUENCE hibernate_sequence;

然后我提出了更多错误,例如 -> 类型转换之类的东西,然后在清除那些错误之后它起作用了!最后:-)

感谢大家的支持 :)

【讨论】:

    【解决方案3】:

    或者,您也可以添加@Generator 而不是自动,您可以添加序列并指定 PostGre 使用的序列生成器 ${table_name}_${pk}_seq。

    尽管如此,这样做的一个缺点是使您的应用与序列紧密耦合。

    如果你强制它创建 DDL,另一个策略是让 hibernate 创建那个 hibernate_sequence。

    【讨论】:

      猜你喜欢
      • 2014-06-11
      • 2014-06-05
      • 2011-08-06
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      • 2011-06-30
      相关资源
      最近更新 更多