Toplink Named Queries (JPA) in TomCat 5.5
Target of this technical exercise to make implementation
of Toplink Essentials (i.e. JPA outside EJB3 container)
easier for comprehension.Sun JSF RI has been used for this
test.Toplink Essentials jars [1] have been downloaded from OTN
and placed in WEB-INF/lib folder.
Snapshot of Web Application file layout:-
************************
Entity Bean - Emp.java
************************
package dstu.net;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.OneToOne;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQuery;
import javax.persistence.Version;
import java.sql.Date;
import javax.persistence.Table;
@Entity
@NamedQuery(
name="selectEmps",
query="SELECT i FROM Emp i"
)
@Table(name="emp")
public class Emp {
@Id
protected int empno;
protected String ename;
protected String job;
protected int mgr;
protected Date hiredate;
protected long sal;
protected long comm;
protected int deptno;
public void setEmpno(int empno) {
this.empno=empno;
}
public int getEmpno() {
return empno;
}
public void setEname(String ename) {
this.ename=ename;
}
public String getEname() {
return ename;
}
public void setJob(String job) {
this.job=job;
}
public String getJob() {
return job;
}
public void setMgr(int mgr) {
this.mgr=mgr;
}
public int getMgr() {
return mgr;
}
public void setHiredate(Date hiredate) {
this.hiredate=hiredate;
}
public Date getHiredate() {
return hiredate;
}
public void setSal(long sal) {
this.sal=sal;
}
public long getSal() {
return sal;
}
public void setComm(long comm) {
this.comm=comm;
}
public long getComm() {
return comm;
}
public void setDeptno(int deptno) {
this.deptno=deptno;
}
public int getDeptno() {
return deptno;
}
}
****************************************
DataBean.java invokes Entity Manager
and returns collection of "Emp" beans
****************************************
package dstu.net;
import javax.persistence.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import dstu.net.Emp;
public class DataBean {
public DataBean() {}
public dstu.net.Emp[] getEmps(){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
EntityManager em = emf.createEntityManager();
try {
Collection<dstu.net.Emp> tempCollection =
em.createNamedQuery("selectEmps").getResultList();
dstu.net.Emp[] emps = tempCollection.toArray(new dstu.net.Emp[tempCollection.size()]);
return(emps);
} finally { em.close();}
}
}
***************************************************
File WEB-INF/clasess/META-INF/persistence.xml:-
***************************************************
<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 persistence_1_0.xsd" version="1.0">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<provider>
oracle.toplink.essentials.PersistenceProvider
</provider>
<class>dstu.net.Emp</class>
<properties>
<property name="toplink.logging.level" value="FINE"/>
<property name="toplink.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
<property name="toplink.jdbc.url" value="jdbc:oracle:thin:@192.168.0.100:1522:jdevdbs"/>
<property name="toplink.jdbc.password" value="tiger"/>
<property name="toplink.jdbc.user" value="scott"/>
</properties>
</persistence-unit>
</persistence>
*********************************
File WEB-INF/faces-config.xml:-
*********************************
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<managed-bean>
<managed-bean-name>dataBean</managed-bean-name>
<managed-bean-class>
dstu.net.DataBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
*************************************
JSF Page displaying query results:-
*************************************
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<BODY>
<CENTER>
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">TOPLINK NAMED QUERY</TH></TR>
</TABLE>
<H3>Displaying Emp table from scott's schema</H3>
<h:dataTable value="#{dataBean.emps}"
var="rowVar"
border="1">
<h:column>
<h:outputText value="#{rowVar.empno}"/>
<f:facet name="header">
<h:outputText value="Number"/>
</f:facet>
</h:column>
<h:column>
<h:outputText value="#{rowVar.ename}"/>
<f:facet name="header">
<h:outputText value="Last Name"/>
</f:facet>
</h:column>
<h:column>
<f:verbatim>$</f:verbatim>
<h:outputText value="#{rowVar.sal}"/>
<f:facet name="header">
<h:outputText value="Salary"/>
</f:facet>
</h:column>
<h:column>
<h:outputText value="#{rowVar.hiredate}"/>
<f:facet name="header">
<h:outputText value="Date of hiring"/>
</f:facet>
</h:column>
</h:dataTable>
</CENTER></BODY></HTML>
</f:view>
Runtime image:-