Java CDI&x2B;JPA&x2B;EJB+;JTA&x2B;JSF——使用PostgreSQL的JBoss厨房示例?

Java CDI&x2B;JPA&x2B;EJB+;JTA&x2B;JSF——使用PostgreSQL的JBoss厨房示例?,java,postgresql,jboss,jboss7.x,cdi,Java,Postgresql,Jboss,Jboss7.x,Cdi,我正在尝试学习新技术CDI+JPA+EJB+JTA+JSF,因此,我从JBoss社区-->jbossas kitchensink下载了示例项目(在我的eclipsejuno中来自JBoss) 我正在尝试将厨房与我的PostgreSQL数据库连接: 我的数据库名是:sampledb 我的表(成员)结构是:数据库名称:快速启动\u数据库名称 CREATE TABLE member ( id integer, name text, email text, phone_number n

我正在尝试学习新技术
CDI+JPA+EJB+JTA+JSF
,因此,我从
JBoss社区
-->jbossas kitchensink下载了示例项目(在我的eclipsejuno中来自JBoss)

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

我的表(成员)结构是:数据库名称
快速启动\u数据库名称

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>
<?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>
<?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>
<?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>
<?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>
我应该怎么做才能用我的数据库运行这个应用程序来插入数据

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

评论后编辑:根据,我已手动将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>
----////---
运行以下命令:

/subsystem=datasources/jdbc-driver=postgresql-driver:add(driver-name=postgresql-driver, driver-class-name=org.postgresql.Driver, driver-module-name=org.postgresql)
现在使用postgresql驱动程序作为驱动程序名,创建任何必需的数据源等

您可以通过web ui、jboss cli和数据源创建命令(请参见数据源--help、数据源添加--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>

我需要帮助来配置它。。我尝试了

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

首先,我通过SSH连接到openshift控制台。然后,我使用POSTGRES连接的凭据运行了
psql
(您可以从
$POSTGRES\u HOME/bin/psql
)并使用
\ds
\dt
确定休眠序列是一个表而不是序列

所以我放弃了它:
DROP TABLE hibernate\u序列


并将其重新创建为序列:
createsequencehibernate\u序列

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

首先,我通过SSH连接到openshift控制台。然后,我使用POSTGRES连接的凭据运行了
psql
(您可以从
$POSTGRES\u HOME/bin/psql
)并使用
\ds
\dt
确定休眠序列是一个表而不是序列

所以我放弃了它:
DROP TABLE hibernate\u序列


并将其重新创建为序列:
createsequencehibernate\u序列

对于引发的错误:
错误:关系“休眠序列”不存在位置:17

我按照
@martinnuc
的建议创建了一个
序列

要检查数据库中是否有序列,请使用
\ds:列出数据库中的序列

CREATE SEQUENCE hibernate_sequence;
然后我又提出了一些类似->Type Cast的错误,在清除这些错误之后,它就工作了!最后:-)


感谢大家的支持1:)

因为以下错误而引发:
错误:关系“hibernate\u sequence”不存在位置:17

我按照
@martinnuc
的建议创建了一个
序列

要检查数据库中是否有序列,请使用
\ds:列出数据库中的序列

CREATE SEQUENCE hibernate_sequence;
然后我又提出了一些类似->Type Cast的错误,在清除这些错误之后,它就工作了!最后:-)


感谢对Every1的支持:)

或者,您也可以添加@Generator而不是auto,您可以添加sequence并指定PostGre使用的序列生成器,即${table_name}${pk}\seq

然而,这样做的一个退步是使你的应用程序与序列紧密耦合


另一种策略是,如果您强制hibernate创建DDL,则让它创建该hibernate_序列。

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

然而,这样做的一个退步是使你的应用程序与序列紧密耦合


另一个策略是,如果您强制hibernate创建DDL,则让它创建hibernate\u序列。

您的JBoss正在运行吗?您是否已将“PostgreSQL”数据源添加到JBoss中?我的命令提示符下运行JBoss!通过使用standalone.bat,但我没有添加数据源@fascynacja,但是我被困在JBOSS_HOME/bin/JBOSS-cli.sh--connect--file=configure postgresql.cli(步骤)。。它正在打开一个jboss cli文件。。我应该如何继续修复它@Fascynacja即使我在配置中配置了POSTGRESQL,它也不工作!我已经编辑了这个问题。另一个问题是:运行maven命令后出现的JBoss控制台输出是什么?您的JBoss正在运行吗?您是否已将“PostgreSQL”数据源添加到JBoss中?我的命令提示符下运行JBoss!通过使用standalone.bat,但我没有添加数据源@fascynacja,但是我被困在JBOSS_HOME/bin/JBOSS-cli.sh--connect--file=configure postgresql.cli(步骤)。。它正在打开一个jboss cli文件。。我应该如何继续修复它@Fascynacja即使我在配置中配置了POSTGRESQL,它也不工作!我编辑了这个问题另一个问题是:运行maven命令后出现的JBoss控制台输出是什么?我的数据库:
QUICKSTART\u DATABASENAME-\dt public | member | table | postgres&QUICKSTART\u DATABASENAME-\dl-Large-objects-ID | Owner | Description-+--+--+--(0行)
成功了!我创建了一个新的序列,然后纠正了字符串转换为长字符和类型转换时出现的错误!感谢uMy数据库:
QUICKSTART_数据库名称-#\dt
<?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 
CREATE SEQUENCE hibernate_sequence;