1.数据源的配置oracle-ds.xml(放在jboss安装目录/server/default/deploy下)
<?xml version="1.0" encoding="UTF-8"?>
<!-- ===================================================================== -->
<!-- -->
<!-- JBoss Server Configuration -->
<!-- -->
<!-- ===================================================================== -->
<!-- See http://www.jboss.org/community/wiki/Multiple1PC for information about local-tx-datasource -->
<!-- $Id: oracle-ds.xml 88948 2009-05-15 14:09:08Z jesper.pedersen $ -->
<!-- ==================================================================== -->
<!-- Datasource config for Oracle originally from Steven Coy -->
<!-- ==================================================================== -->
<datasources>
<local-tx-datasource>
<jndi-name>OracleDS</jndi-name>
<connection-url>jdbc:oracle:thin:@123.123.123.123:1521:DBname</connection-url>
<!--
Here are a couple of the possible OCI configurations.
For more information, see http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/java.920/a96654/toc.htm
<connection-url>jdbc:oracle:oci:@youroracle-tns-name</connection-url>
or
<connection-url>jdbc:oracle:oci:@(description=(address=(host=youroraclehost)(protocol=tcp)(port=1521))(connect_data=(SERVICE_NAME=yourservicename)))</connection-url>
Clearly, its better to have TNS set up properly.
-->
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>username</user-name>
<password>password</password>
<!-- Uses the pingDatabase method to check a connection is still valid before handing it out from the pool -->
<!--valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name-->
<!-- Checks the Oracle error codes and messages for fatal errors -->
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool - the OracleValidConnectionChecker is prefered
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<metadata>
<type-mapping>Oracle10g</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
2.实体 Person.java
package ejb3.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
//import javax.persistence.GeneratedValue;
//import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@SuppressWarnings("serial")
@Entity
@Table(name="Person")
public class Person implements Serializable{
private int personId;
private String personName;
private Date personAge;
@Id
// @GeneratedValue(strategy=GenerationType.AUTO) // 如果是自己编码设定主键,应不用写这句话,否则会异常
@Column(name="PERSON_ID")
public int getPersonId() {
return personId;
}
public void setPersonId(int personId) {
this.personId = personId;
}
@Column(name="PERSON_NAME",length=32)
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
@Column(name="PERSON_AGE")
@Temporal(value=TemporalType.DATE)
public Date getPersonAge() {
return personAge;
}
public void setPersonAge(Date personAge) {
this.personAge = personAge;
}
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result
+ personId;
result = PRIME * result
+ ((personName == null) ? 0 : personName.hashCode());
result = PRIME * result
+ ((personAge == null) ? 0 : personAge.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Person other = (Person) obj;
if (personId != other.personId)
return false;
if (personName == null) {
if (other.personName != null)
return false;
} else if (!personName.equals(other.personName))
return false;
if (personAge == null) {
if (other.personAge != null)
return false;
} else if (!personAge.equals(other.personAge))
return false;
return true;
}
}
3。 接口PersonDao.java
public interface PersonDao {
public void insertPerson(Person person);
public void mergePerson(Person person);
public void deletePerson(int personId);
public List<Person> getPersonList();
}
4.PersonDaoBean.java
package ejb3.bean.impl;
import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import ejb3.bean.PersonDao;
import ejb3.entity.Person;
@Stateless
@Remote(PersonDao.class)
public class PersonDaoBean implements PersonDao{
@PersistenceContext(unitName="personBean")
protected EntityManager em;
@Override
public void insertPerson(Person person) {
em.persist(person);
em.flush();
}
@Override
public void mergePerson(Person person) {
em.merge(person);
}
@Override
public void deletePerson(int personId) {
Person person = em.find(Person.class, personId);
if(person != null){
em.remove(person);
}
}
@SuppressWarnings("unchecked")
@Override
public List<Person> getPersonList() {
Query q = em.createQuery("select p from Person p");
return (List<Person>)q.getResultList();
}
}
5.persistence.xml(放在java工程的META-INF目录下)
<?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_1_0.xsd"
version="1.0">
<persistence-unit name="personBean" transaction-type="JTA">
<jta-data-source>java:/OracleDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
6.再建一个web工程,新建jsp
<%@ page language="java" contentType="text/html; charset=windows-31j"
pageEncoding="windows-31j"%>
<%@ page import="ejb3.bean.*, ejb3.bean.impl.*, ejb3.entity.*, javax.naming.*,java.util.*"%>
<table>
<tr>
<td>PersonId</td>
<td>PersonName</td>
<td>PersonAge</td>
</tr>
<%
try{
InitialContext ctx = new InitialContext();
PersonDao obj = (PersonDao)ctx.lookup("PersonDaoBean/remote");
Person p = new Person();
p.setPersonId(1);
p.setPersonName("wl");
p.setPersonAge(new Date());
obj.insertPerson(p);
List<Person> personList = obj.getPersonList();
for(Person tmp : personList){
out.println("<tr><td>" + tmp.getPersonId() + "</td>");
out.println("<td>" + tmp.getPersonName() + "</td>");
out.println("<td>" + tmp.getPersonAge() + "</td></tr>");
}
}catch (NamingException e){
out.println(e.getMessage());
}
%>
</table>