一ejb 服务端 基于ejb容器 ,客户端src下需要配置jndi配置文件
*实体对象
@Entity public class Person implements Serializable { public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Id @GeneratedValue private int id; private String name; }
*实体管理接口
import java.util.List; public interface PersonManager { public void addPerson(Person person); public Person getPerson(int id); public Person getPerson2(int id); public List<Person> getPersons(); public void updatePerson(Person person); public void deletePerson(int id); }
*实体管理的实现
@Stateless @Remote @Local public class PersonImpl implements PersonManager { @PersistenceContext(unitName="test") private EntityManager entityManager; public void addPerson(Person person) { entityManager.persist(person); //throw new RuntimeException("ddddddddddd"); } public Person getPerson(int id) { //相当于hibernate中的load,是懒加载的 Person p=entityManager.getReference(Person.class, id); System.out.println(p.getName()); return p; } public Person getPerson2(int id){ //相当于hibernate中的get Person p=entityManager.find(Person.class, id); return p; } public List<Person> getPersons(){ return entityManager.createQuery(" from Person").getResultList(); } public void updatePerson(Person person){ entityManager.merge(person); } public void deletePerson(int id){ entityManager.remove(getPerson(id)); } }
persistence.xml的配置文件
<?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="test" transaction-type="JTA"> <jta-data-source>java:/MySqlDS</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit> </persistence>
二客户端
public class Client { /** * @param args */ public static void main(String[] args) throws Exception { InitialContext context =new InitialContext(); //UserTransaction um=(UserTransaction )context.lookup("UserTransaction"); //um.begin(); PersonManager pm=(PersonManager)context.lookup("PersonImpl/remote"); // Person p=new Person(); // p.setName("www"); // pm.addPerson(p); //Person p=pm.getPerson(5); //System.out.println(p.getName()); // Person p=pm.getPerson2(5); // System.out.println(p.getName()); //System.out.println(pm.getPersons().size()); //um.commit(); // Person p=new Person(); // p.setId(5); // p.setName("xxxxxxxxxxxxx"); // // pm.updatePerson(p); // pm.deletePerson(5); } }
三 jboss 数据源的配置
jboss 安装目录D:\jboss-4.0.5.GA\server\default\deploy 下放入
mysql-ds.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- $Id: mysql-ds.xml 41016 2006-02-07 14:23:00Z acoliver $ --> <!-- 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>MySqlDS</jndi-name> <connection-url>jdbc:mysql://localhost:3306/ejb</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>root</user-name> <password>bjsxt</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>