public class StudentDAOBean implements StudentDAO { @Inject private EntityManager manager;
public void create(String first, String last, String grade, String email) { Student student = new Student(); student.setName(new Name(first,last)); student.setGrade(grade); student.setEmail(email); manager.create(student); }
public Student find(Name name) { return manager.find(Student.class, name); }
public List findByFirstName(String name) { return manager.createQuery("from Student s where s.name.last = :name").setParameter("name", name).listResults();
}
public List findByLastName(String name) { return manager.createQuery("from Student s where s.name.first = :name").setParameter("name", name).listResults(); }
public List findByEmail(String email) { return manager.createQuery("from Student s where s.email = :email").setParameter("email", email).listResults(); }
public void merge(Student s) { manager.merge(s); }
public class Client { public static void main(String[] args) throws NamingException { InitialContext ctx = new InitialContext(); StudentDAO dao = (StudentDAO) ctx.lookup(StudentDAO.class.getName()); dao.create("晁","岳攀","8","smallnest@kuaff.com"); dao.create("朱","立焕","6","zhuzhu@kuaff.com"); Name name = new Name("朱","立焕");
//List list = dao.findByEmail("zhuzhu@kuaff.com");
Student s = dao.find(name); System.out.printf("%s %s的email:%s%n",s.getName().getFirst(),s.getName().getLast(),s.getEmail()); /* for(Object o:list) { Student s = (Student)o; System.out.printf("%s %s的email:%s%n",s.getName().getFirst(),s.getName().getLast(),s.getEmail()); } */ } }
这个客户端用来测试。
请运行{$JBOSS_HOME}/bin目录下的run.bat: run –c all,启动JBOSS。