1,创建jboss数据源,mysql-ds.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- $Id: mysql-ds.xml,v 1.3.2.3 2006/02/07 14:23:00 acoliver Exp $ -->
<!-- Datasource config for MySQL using 3.0.9 available from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->
<datasources>
<local-tx-datasource>
<jndi-name>chenzhq_mysql</jndi-name>
<connection-url>jdbc:mysql://192.168.40.10:3306/school</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>root</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<!-- should only be used on drivers after 3.22.1 with "ping" support
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-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 - MySQLValidConnectionChecker is preferred for newer drivers
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
2.编写persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistences>
<persistence-unit name="student">
<!--
<jta-data-source>java:chenzhq_oracle_datasource</jta-data-source>
<class>com.chenzhq.ejb.entity.Student</class>
-->
<jta-data-source>java:/chenzhq_mysql</jta-data-source>
<class>com.chenzhq.ejb.entity.Student</class>
</persistence-unit>
</persistences>
3.写个Entity-bean
package com.chenzhq.ejb.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity//Entity标记声明student是个实体bean
@Table (name="student")//for mysql 注册映射表为student
//@Table (name="test.student")//for oracle
public class Student implements Serializable {//注意必须实现可序列化,否则在你调用Remote接口的时候就会报错。
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String sex;
private String classes;
private int points;
@Column (name="classes")//映射到classes列
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
@Id//声明student的主键
//@GeneratedValue
@Column (name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column (name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column (name="points")
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
@Column (name="sex")
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
4,在写个sessionbean调用处理实体bean,ejb3.0中entitybean就像个pojo
package com.chenzhq.ejb.session;
import javax.ejb.Init;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FlushModeType;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import com.chenzhq.ejb.entity.Student;
@Stateful
@Remote ({ProcessStudentRemote.class})
@Local ({ProcessStudentLocal.class})
public class ProcessStudent implements ProcessStudentLocal,ProcessStudentRemote {
//@PersistenceUnit (unitName="student") private static EntityManagerFactory emf;
/* 这里面的大多数方法是我简化写的,只是提及一下,深入了解的化买本书吧!
* //获得对实体bean的上下文引用,unitName对应这Persistence.xml中的uintName
* type可以是EXTEND,或是TRANSACTION,EXTEND的相对与TRANSACTION的生存的时间长
*
*
*/
@PersistenceContext (unitName="student",type=PersistenceContextType.EXTENDED)
private static EntityManagerFactory emf;
private static EntityManager em;
@Init
public void init(){
System.out.println(emf);
}
public boolean createStudent(Student s){
System.out.println(s);
em = emf.createEntityManager();
/*
* 如果你的EntityManager是使用EntityManagerFactoty生成的,那么joinTransaction是必须的
*
*/
em.joinTransaction();
em.setFlushMode(FlushModeType.COMMIT);
System.out.println(em.toString().toUpperCase());
if(em.isOpen()){
//em.merge(s);
em.persist(s);//持久化一个student
em.flush();
}else{
throw new RuntimeException("the entity manager is closed");
}
return true;
}
public boolean queryStudent(Object o){
em = emf.createEntityManager();
em.joinTransaction();
em.setFlushMode(FlushModeType.COMMIT);
System.out.println(em.toString().toUpperCase());
System.out.println(em.find(Student.class, o).getId());
try{
/*
* find和reference的区别再用,如果没有发现find返回null,而reference出现异常。
*/
Student s = em.getReference(Student.class, o);
System.out.print(s.getName());
}catch(Exception e){
e.printStackTrace();
}
return true;
}
public boolean removeStudent(Object o){
em = emf.createEntityManager();
em.joinTransaction();
em.setFlushMode(FlushModeType.COMMIT);
System.out.println(em.toString().toUpperCase());
Student s = em.find(Student.class, o);
em.remove(s);
em.flush();
em.clear();
return true;
}
public boolean updateStudent(String name,Object o){
em = emf.createEntityManager();
em.joinTransaction();
em.setFlushMode(FlushModeType.COMMIT);
System.out.println(em.toString().toUpperCase());
Student s = em.find(Student.class, o);
/*
* 因为是managed的Bean所以容器会将改变的bean同步到database的。
*/
s.setName(name);
return true;
}
}