在这个简单的例子中,一个使用JPA的无状态会话Bean实现了这个服务。
JPAServiceBean.java
01 package session;
02
03 import javax.ejb.Remote;
04 import javax.ejb.Stateless;
05 import javax.persistence.EntityManager;
06 import javax.persistence.PersistenceContext;
07
08 import service.JPAService;
09 import service.Message;
10
11 /**
12 * A Stateless Session bean that uses an injected JPA EntityManager to implement
13 * the contract of {@link JPAService}.
14 *
15 * @author ppoddar
16 *
17 */
18 @Stateless
19 @Remote(JPAService.class)
20 public class JPAServiceBean {
21 /**
22 * Inject an EntityManager for a persistent unit simply named
23 * test.
24 * It is this name which must be specified in the configuration file
25 * META-INF/persistence.xml as
26 * 27 *
28 *
29 */
30 @PersistenceContext(unitName="test")
31 EntityManager em;
32
33 /**
34 * Returns the EntityManager class name that provides the persistence
35 * service.
36 *
37 * NOTE: As the entity manager is injected by the container, it is
38 * often a proxy. Hence the return value is the class name of the
39 * delegate of the actual injected instance
40 */
41 public String getProvider() {
42 return em.getDelegate().getClass().getName();
43 }
44
45 public Message log(String message) {
46 Message result = new Message(message);
47 em.persist(result);
48 return result;
49 }
文章来源于领测软件测试网 https://www.ltesting.net/