快速业务通道

EJB 3.0开发指南之多对多和一对一 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-24
public void createData(); public List findByName(String name); }

EntityTestBean.java

package com.kuaff.ejb3.relationships; import javax.ejb.EntityManager; import javax.ejb.Inject; import javax.ejb.Stateless; import java.util.HashSet; import java.util.Set; import java.util.List; @Stateless public class EntityTestBean implements EntityTest { private @Inject EntityManager manager; public void createData() { Teacher teacher1 = new Teacher(); Teacher teacher2 = new Teacher(); Set<Student> students1 = new HashSet<Student>(); Set<Student> students2 = new HashSet<Student>(); Student student1 = new Student(); Student student2 = new Student(); Student student3 = new Student(); Dossier dossier1 = new Dossier(); Dossier dossier2 = new Dossier(); Dossier dossier3 = new Dossier(); teacher1.setId(new Long(1)); teacher1.setName("hushisheng"); teacher1.setInfo("胡时胜教授,博士生导师"); manager.create(teacher1); teacher2.setId(new Long(2)); teacher2.setName("liyongchi"); teacher2.setInfo("李永池教授,博士生导师"); manager.create(teacher2); student1.setFirst("晁"); student1.setLast("岳攀"); dossier1.setResume("这是晁岳攀的档案"); student1.setDossier(dossier1); students1.add(student1); student2.setFirst("赵"); student2.setLast("志伟"); dossier2.setResume("这是赵志伟的档案"); student2.setDossier(dossier2); students1.add(student2); student3.setFirst("田"); student3.setLast("明"); dossier3.setResume("这是田明的档案"); student3.setDossier(dossier3); students2.add(student3); teacher1.setStudents(students1); teacher2.setStudents(students2); } public List findByName(String name) { return manager.createQuery("from Teacher t where t.name = :name").setParameter("name", name).listResults(); } }

EJB 3.0开发指南之多对多和一对一(4)

时间:2009-11-27 天极

在这个会话Bean中提供了创建各个实体Bean的方法,并提供了查找老师的方法。

Client.java

package com.kuaff.ejb3.secondary; import javax.naming.InitialContext; import javax.naming.NamingException; import java.util.List; public class Client { public static void main(String[] args) throws NamingException { InitialContext ctx = new InitialContext(); StudentDAO dao = (StudentDAO) ctx.lookup(StudentDAO.class.getName()); int id = dao.create("晁","岳攀","8","smallnest@kuaff.com","男"); dao.create("朱","立焕","6","zhuzhu@kuaff.com","女"); List list = dao.findAll(); for(Object o:list) { Student s = (Student)o; System.out.printf("%s%s的性别:%s%n",s.getName().getFirst(),s.getName().getLast(),s.getGender()); dao.evict(s); } } }

这个客户端用来测试。

请运行{$JBOSS_HOME}/bin目录下的run.bat: run –c all,启动JBOSS。

http://localhost:8080/jmx-console/HtmlAdaptor?action=inspectMBean&name=jboss%3Aservice%3DHypersonic%2Cdatabase%3DlocalDB,然后调用startDat

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号