简体   繁体   中英

CDI + JPA + EJB + JTA + JSF --JBoss-Kitchen Example with PostgreSQL?

I am trying to learn the new technologies CDI + JPA + EJB + JTA + JSF so, i have downloaded the sample project from JBoss-Community --> jboss-as-kitchensink(from JBoss in my Eclipse juno).

I am Trying to connect the kitchen with my PostgreSQL database:
My database name is : sampledb

My Table(Member) Structure is : Database name : QUICKSTART_DATABASENAME

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

I have edited this files in my kitchen Project :

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>

I Added the Dependency in pom.xml :

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

But i'm getting error:

[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>

What should i do so that i can run this application with my database to insert the data.

Do i need to change any code in .java files or any .xml file to make it work?

EDITED after Comments: By Following this link ,I have configured my PostgreSQL into JBoss manually by setting up the standalone-full.xml file of JBoss..

    ----///----    <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>
----////---

Answer :

Install the PostgreSQL driver and use the XML so that u can make it work.

Follow The Steps : (Suggested By @Craig Ringer ) is Very HelpFull

Download PgJDBC . I'm assuming you're using postgresql-9.2-1003.jdbc4.jar, the current version at time of writing. Adjust any filenames to match if you need a different version.

Now deploy the JDBC driver to JBoss AS 7 by putting it in the deployments folder or using the deploy command in jboss-cli. This will work for most, but not all, purposes.

Alternately, you an define a PostgreSQL JDBC driver module:

Create the path $JBOSS_HOME/modules/org/postgresql/main. The modules/org part should already exist, make directories for the rest.

In $JBOSS_HOME/modules/org/postgresql/main/modules.xml with the following content, changing the resource-root entry for the PgJDBC driver to refer to the driver you wish to use.

 <?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

Run the command:

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

Now create any required data sources, etc, using postgresql-driver as the driver name.

You can create a datasource via the web ui, with jboss-cli with the data-source create command (see data-source --help, data-source add --help), or by deploying a -ds.xml file like this:

<?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>

For KitchenSink Example: I Tried With These, It Worked till Connection and All the Things.Still It Should be updated After Later...

Follow this link Till Creating a database and a user & continue the rest from here ! Change Persistance To : 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>

But Where i'm Stuck is : I'm Unable to insert the Data Inot the Database! It is raising an error as

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

I need A Help To Configure It .. I tried in MANY ways

I dont know if you still need help with this but I encounted similar error when deploying my application to the Openshift.

First I connected over SSH to openshift console. Then I ran psql (you can run psql on your host from $POSTGRES_HOME/bin/psql ) with credentials for my postgres connection and used \\ds and \\dt to determine that hibernate_sequence was a table and not a sequence.

So I dropped it: DROP TABLE hibernate_sequence;

And recreated it as a sequence: CREATE SEQUENCE hibernate_sequence;

For the Error Raised as: ERROR: relation "hibernate_sequence" does not exist Position: 17

i had created a SEQUENCE as Suggested by @Martin Nuc .

To Check Whether there is a Sequence in the Database or not use \\ds : Lists the Sequences in the database.

CREATE SEQUENCE hibernate_sequence;

Then i had raised with few more errors like -> Type Cast like things then after Clearing those Errors It Worked ! Finally :-)

Thanks for the Support Every1 :)

Alternatively you can also add @Generator instead of being auto, you can add sequence and specify a sequence generator that is used by PostGre which is ${table_name}_${pk}_seq.

Nevertheless, One draw back of doing this is to making your app tightly coupled to sequences.

Another strategy is to make hibernate create that hibernate_sequence if you force it to create DDL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM