Month: April 2012

Hibernate, postgreSQL, jboss-7

Posted on Updated on

When we start a project,  The most-time consuming task is to initialize the environment and setup a new connection (ORM, Database, EJB etc). Today, I will explain how to setup database connection using Hibernate, Java2ee, JBOSS 7, Eclipse Indigo.

Step by step:
1. Install Eclipse 3.7.2 indigo ide for java-ee developer(http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/indigosr2)
2. Install Jboss Tools 3.3 for eclipse indigo (https://www.jboss.org/tools/download/installation/update_3_3.html)
3. Create a new Dynamic Web project under Jboss Central

Dynamic Web Project

4. Click Modify button to open Project Facet, and the following screen will pop up:

Project Facets

5. Tick JPA to enable database connection via hibernate.Then  Click Ok to save and close the windows.
6. Unfortunately, the configuration screen to build persistence.xml doesn’t function very well, I need to manually modify it.
 Following is the persistence.xml example to connect to postgresql:

<?xml version=”1.0″ encoding=”UTF-8″?>
<persistence version=”2.0″ xmlns=”http://java.sun.com/xml/ns/persistence&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd”&gt;
   
    <persistence-unit name=”nbs_web” transaction-type=”RESOURCE_LOCAL”
        >
        <class>com.etrade.managedbean.UserBean</class>
        <properties>
            <property name=”javax.persistence.jdbc.url” value=”jdbc:postgresql://localhost:5432/test”/>
            <property name=”javax.persistence.jdbc.user” value=”test”/>
            <property name=”javax.persistence.jdbc.password” value=”test”/>
            <property name=”javax.persistence.jdbc.driver” value=”org.postgresql.Driver”/>
          
            <property name=”hibernate.dialect” value=”org.hibernate.dialect.PostgreSQLDialect”/>
            <property name=”hibernate.connection.driver_class” value=”org.postgresql.Driver”/>
            <property name=”hibernate.connection.url” value=”jdbc:postgresql://localhost:5432/test”/>
            <property name=”hibernate.default_schema” value=”public”/>
            <property name=”hibernate.connection.username” value=”test”/>
            <property name=”hibernate.connection.password” value=”test”/>
          
          
          
            <property name=”hibernate.show_sql” value=”true”/>
            <property name=”hibernate.format_sql” value=”true”/>
          
        </properties>
                   

    </persistence-unit>
</persistence>

8. Following is the directory structure , where you should put your  persistence.xml and postgresql driver:

9. Database setup is ready. Now we will need to do the java code.