0
点赞
收藏
分享

微信扫一扫

EJB remote 和sessionBean,以及新工程调用EJB,实现数据的增删改查


远程接口remote:

package ch03.com.ma.bo;

import java.util.List;

import javax.ejb.Remote;

import ch03.com.ma.entity.Customer;
import ch03.com.ma.entity.CustomerId;



@Remote
public interface ICustomerService {
public void save(Customer cusEO);
public void delete(CustomerId id);
public Customer update(Customer cusEO);
public Customer findById(CustomerId id);
public List<Customer> findAll();
}

 

sessionBean实现远程接口

注意:这里的的双主键是用嵌入式主键,不是复合式主键,是嵌入式主键,可以看上一章的主键设置

package ch03.com.ma.bo.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


import ch03.com.ma.bo.ICustomerService;
import ch03.com.ma.entity.Customer;
import ch03.com.ma.entity.CustomerId;


@Stateless
public class CustomerService implements ICustomerService {

@PersistenceContext(unitName = "hongmin.ma")
private EntityManager entityManager;

public void delete(CustomerId id) {
Customer cusEO = this.findById(id);
entityManager.remove(cusEO);
}

@SuppressWarnings("unchecked")
public List<Customer> findAll(){
try{
String sql = " select c from Customer c";
Query query=entityManager.createQuery(sql);
List<Customer> list=query.getResultList();
return list;
}catch(Exception ex){
ex.printStackTrace();
return null;
}
}

public Customer findById(CustomerId id) {
Customer cusEO = entityManager.find(Customer.class, id);
return cusEO;
}

public void save(Customer cusEO) {
entityManager.persist(cusEO);
}

public Customer update(Customer cusEO) {
Customer result = entityManager.merge(cusEO);
return result;
}

}

 

调用EJB,实现对Customer的增删改查

 

新建一个工程,可以是JAVA,也可以是WEB

导入7个EJB基本包jboss-annotations-ejb3.jar,jboss-ejb3x.jar,jboss-aspect-jdk50-client.jar,jboss-ejb3-client.jar,jboss-aop-jdk50-client.jar,jbossall-client.jar,trove.jar

将EJB工程下的远程接口和实体BEAN连包一起拷到此工程下

新建一个类,在此类下写代码:

package action;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

import ch03.com.ma.bo.ICustomerService;
import ch03.com.ma.entity.Customer;
import ch03.com.ma.entity.Order;


public class Test1 {
private static InitialContext ctx=null;

public static void main(String[] args) {
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
props.put(Context.PROVIDER_URL, "jnp://localhost:1099");

try {
if(ctx==null){
ctx = new InitialContext(props);
}

//ctx=EjbConnection.getConnection();
Object object=ctx.lookup("CustomerService/remote");
ICustomerService remote=(ICustomerService) ctx.lookup("CustomerService/remote");
//客户
//添加
// Customer cusEO=new Customer();
// CustomerId cusId=new CustomerId();
// cusId.setId(3);
// cusId.setName("ccc");
//
// cusEO.setId(cusId);
// cusEO.setShortName("c");
// cusEO.setRegisteredCapital(10000d);
// remote.save(cusEO);
// System.out.println("添加成功");

//更新
// CustomerId id=new CustomerId();
// id.setId(1);
// id.setName("aaa");
// Customer cusEO=remote.findById(id);
// cusEO.setShortName("aaaaaaaaaa");
//
// remote.update(cusEO);
// System.out.println("更新成功");

//查询
// CustomerId id=new CustomerId();
// id.setId(1);
// id.setName("aaa");
// Customer cusEO=remote.findById(id);
// System.out.println(cusEO.getShortName());

//删除不做测试

//查询全部
// List<Customer> list=remote.findAll();
// for (int i=0;i<list.size();i++) {
// System.out.println(list.get(i).getShortName());
// }


} catch (Exception e) {
e.printStackTrace();
}
}


}

举报

相关推荐

0 条评论