5. Add persisistence
1. Add persistence.xml in WebContent/WEB-INF/classes/META-INF
1.1 create classes/META-INF directory on WebContent/WEB-INF directory
1.2 create persistence.xml in WebContent/WEB-INF/classes/META-INF
(you need to configure this file to fit with your environment)
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.0">
<persistence-unit name="test">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/testDatasource</jta-data-source>
<properties>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<property name="hibernate.connection.username" value="test"/>
<property name="hibernate.connection.password" value="test"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9iDialect" />
</properties>
</persistence-unit>
</persistence>
2. Add test-ds.xml (datasource) in JBOSS deploy directory
(you need to configure this file to fit with your environment)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE datasources
PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
"http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd">
<datasources>
<local-tx-datasource>
<jndi-name>testDatasource</jndi-name>
<use-java-context>true</use-java-context>
<connection-url>jdbc:oracle:thin:@hostname:1521:SID</connection-url>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<user-name>test</user-name>
<password>test</password>
<max-pool-size>100</max-pool-size>
</local-tx-datasource>
</datasources>
3. If oracle, make sure ojdbc15-xxx.jar or relevant file is placed in $JBOSS_HOME/server/default/lib (if using default)
For other database place driver jar file in there.
4. create data model (this is just example. you will need to setup your own database and table)
I placed ZoneCodes.java in seam3-test/src/com/test directory
package com.test;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "zone_codes")
public class ZoneCodes {
@Id
@Column(name = "zone_code")
private String code;
@Column(name = "zone_description")
private String description;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
5. create controller
I placed ZoneController.java in seam3-test/src/com/test directory
package com.test;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateful;
import javax.enterprise.inject.Produces;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.jboss.seam.faces.context.RenderScoped;
import org.jboss.seam.international.status.Messages;
/**
* This class performs search and outject result
*/
@Named
@Stateful
@RenderScoped
public class ZoneController {
@PersistenceContext
private EntityManager em;
@Inject
Messages messages;
private ListzoneCodes = new ArrayList ();
/**
* Perform search
* This retrieves all ZoneCodes from database
*/
public void search() {
Query q = em.createQuery("from ZoneCodes");
zoneCodes = q.getResultList();
messages.info("Search completed. {0} records found", zoneCodes.size());
}
/**
* Reset results
*/
public void reset() {
zoneCodes = new ArrayList();
}
// out-jection
@Produces
@Named
public ListgetZoneCodes() {
return zoneCodes;
}
}
The difference between Seam 2 and Seam 3 is quite significant.
Most of major concept in Seam 2 are absorbed in CDI (jboss 6 comes with it), and Seam 3 is providing extras on top of it.
So, when I made seam 3 working on JBoss 6, most of major seam 2 concepts absorbed into CDI. That ands up some people complaining that this article is not seam 3 exemple. Instead it is just CDI example.
I agree. So, I had to provide Seam 3 is still working on my example. Hence, I added couple of 'seam 3' specific features in it.
6. Add index_db.xhtml in WebContent directory
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<f:view>
<h:head>
</h:head>
<h:body styleClass="page">
<h1>Persistence test</h1>
<h:form id="zoneSelectionForm">
<div>
<h:form id="searchForm" prependId="false">
<a4j:commandButton id="search" value="Search" action="#{zoneController.search}" render=":searchResults"/>
#{' '}
<a4j:commandButton id="reset" value="Clear" action="#{zoneController.reset}" render=":searchResults"/>
</h:form>
</div>
<h:panelGroup id="searchResults">
<h:messages />
<h:dataTable id="zones" value="#{zoneCodes}" var="zone" rendered="#{not empty zoneCodes}">
<h:column>
<f:facet id="nameFct" name="header">Code</f:facet>
#{zone.code}
</h:column>
<h:column>
<f:facet id="addrFct" name="header">Description</f:facet>
#{zone.description}
</h:column>
</h:dataTable>
</h:panelGroup>
</h:form>
</h:body>
</f:view>
</html>
7. deploy it onto Jboss 6
8. http://localhost:8080/seam3-test/index_db.xhtml
Sample output is like below

3 comments:
your example is wrong, you add the jar of seam 3, but not use this in the code.
The example have a equal result without the jar of seam 3.
Its a example for use richfaces, not seam 3.
> Juancin
You are absolutely right.
I was just confused with seam 2.
In seam 2 inject/outjection was done through seam, but CDI replaces injection/outjection in seam 3.
So, I added couple of seam access in the example: RenderScoped, Messages.
I haven't gone too far on Seam 3 yet, but I think this should be good example for other beginners too.
Post a Comment