2015年7月1日 星期三

Create a sample project(2) - Setup PostgreSQL on JBoss

After finishing the Maven hell, we can finally start the fun part. I choice the PostgerSQL be our database. There are some reasons. First, it's free. Then, it is more like Oracle, such SQL syntax, design model. So, this is why I don't choice MySQL. Moreover, why  not choice RMDB instead of NoSQL. There some pros and cons. RMDB is more convenient by using SQL. But , NoSQL is more powerful on big data. Anyway, I just choice a simple for starting. After then, I will also study MongoDB.

Actually, this is a simple one to install PostgreSQL on your computer, you can choice raphic installation which conclude pgAdmin. So, you can build db and execute SQL command on pgAdmin.
http://www.postgresql.org/download/
Otherwise, I not sure homebrew wheather contain pgAdmin.

All right, after we install PostgreSQL, we should build an example database on it. So, we create a new database called TEST. Note that using capital is prefer for naming rule, it will effect the SQL command after. Then, we use JPA tool to create the schema of example table.

We use previous project for our practice,

First, setup the connection of DB.
On project -> property -> Hibernate, setup up the Postgre Driver

Second, use JPA Tool to generate
Project -> JPA Tool -> Generate xxx
In here, we change Entity to Table


Next, we should setup the JBoss db connection, or used db configuration on JBoss.
Actually, this is hard part. If I don't see the document, I will never figure it out.
Anyway, we change the DB configuration on this step. Maybe we try not to config JBoss server config file.

I have use maven to import Postgre driver, but the result is fail. I don't know why

You can see this blog to config the database connection
http://wei-meilin.blogspot.tw/2012/07/jeap-6-datasource.html

I choice the module method to setup up. Note that please use the JDBC4(not JDBC41) for setup.

There are some tips you need to know on our prototype project.

  1. Remove spring-quickstart-ds.xml
  2. Change the value of hibernate.hbm2dll.auto on persistence.xml
  3. Setup up datasource and driver on standalone.xml(or domain.xml) which is in JBoss folder(...this depend on that which you need is standalone or domain).
  4. If you change the datasource JNDI name, you should change the JNDI value of dataSource on infrastructure.xml.
  5. You also need to add dependence of PostgreSQL.


standalone.xml
<datasources>

    <datasource jndi-name="java:jboss/datasources/SpringQuickstartDS" pool-name="PostgresDS" enabled="true" use-java-context="true">

        <connection-url>jdbc:postgresql://172.0.0.1:5432/Test</connection-url>

        <driver>postgres</driver>

        <security>

            <user-name>postgres</user-name>

            <password>xxxx</password>

        </security>

    </datasource>

    <drivers>

        <driver name="postgres" module="com.postgresql">

            <xa-datasource-class>org.postgresql.Driver</xa-datasource-class>

        </driver>

    </drivers>

</datasources>


pom.xml
<dependency>

 <groupId>postgresql</groupId>

 <artifactId>postgresql</artifactId>

 <version>9.1-901.jdbc4</version>

</dependency>

Pardon me for such mess article. I avoided to write  to much detail for some reasons. Maybe I will reorganize this article one day. Finally, we create the Database connection. JPA and its annotation are such great tools to create relation with the database. But, we should not focus on that, because it so easy to use. Next time, we we will write the our backend code by using SpringMVC and its unit test. See you~








沒有留言:

張貼留言