50
51 } 這個會話bean本身也有不明確的地方。它使用了JPA,但沒有聲明將要使用哪一個特定的持久性提供者(事實上,定義一個工業標準的持久性API的主要目的之一是為了能夠從一個提供者切換到另一個)。
這個bean并未直接與一個 JPA提供者建立連接,而是依靠容器新獲得的功能(即,從JEE 5以來)在需要時注入JPA連接。另外,有個好消息是Weblogic Server 10.0是第一個兼容JEE 5的應用服務器。
容器能夠以兩種形式向會話Bean注入JPA依賴關系:@PersistenceUnit或@PersistenceContext注釋。我們所使用的是@PersistenceContext(JPAServiceBean.java的第 30和31行)。
如果讓我將它與JDBC進行粗略的類比,那么@PersistenceUnit相當于一個DataSource,而@PersistenceContext則相當于一個Connection。在JPA術語當中,這一對概念是由JPA定義的接口EntityManagerFactory和EntityManager分別實現的。
通過這些接口和少數其他的方式(例如,Query),JPA已經定義了一個比傳統的JDBC通過SQL提供的方式更高層的面向對象抽象與數據庫進行交互。通過JPA,您可以借助面向對象視圖與底層數據庫進行交互。應用程序可以通過 EntityManager接口中的方法創建、更新、查詢和刪除對象――JPA提供者與應用程序協同工作截聽這些對象級的改變并且適當地將它們與相應的INSERT、UPDATE、SELECT和DELETE數據庫操作映射起來。
這個基本的JPA應用程序已經完成,下面是持久性類。
Message.java
01 package service;
02
03 import java.io.Serializable;
04 import java.util.Date;
05
06 import javax.persistence.*;
07
08 /**
09 * A simple persistent message. The entity is declared to be serializable to
10 * pass across processes.
11 *
12 * The message is assigned an identifier by the persistence provider.
13 * A timestamp is also set to the message when it is constructed.
14 *
15 * Annotates the fields with their persistent properties.
16 *
17 * @author ppoddar
18 *
19 */
20 @SuppressWarnings("serial")
21 @Entity
22 public class Message implements Serializable {
23 /**
24 * Annotates the field as primary identity for this instance. Also the
25 * persistence provider will generate the identity value. That is why
26 * there is no corresponding setter method.
27 */
28 @Id
文章來源于領測軟件測試網 http://www.kjueaiud.com/