EJB持久性

EJB2.0中使用的實體bean持久化機制在很大程度上被EJB3.0取代。現在實體bean是一個簡單的POJO映射表。

以下是持久性API的關鍵角色

  • Entity - 持久對象代表數據存儲記錄。這也是可序列化的。

  • EntityManager - 持久性接口做數據操作,如添加/刪除/更新/找到持久化對象(實體)。它還有助於執行查詢使用query接口

  • Persistence unit (persistence.xml) - 持久性單元介紹了持久性機制的屬性。

  • Data Source (*ds.xml) - 數據源描述了數據存儲相關的屬性,如連接URL。用戶名,密碼等。

爲了證明EJB的持久化機制,我們要做好以下幾項工作。

  • Step 1. 在數據庫中創建表.

  • Step 2. 創建實體類對應的表.

  • Step 3. 創建數據源和持久性單元

  • Step 4. 創建一個無狀態EJB EntityManager實例.

  • Step 5. 更新無狀態EJB。添加添加記錄並獲得通過實體管理器從數據庫中記錄的方法。

  • Step 6. 一個基於控制檯應用程序客戶端將訪問無狀態EJB的持久化數據庫中的數據。

創建表

創建一個表 books 在默認的數據庫 postgres.

CREATE TABLE books ( id integer PRIMARY KEY, name varchar(50) );

創建實體類

//mark it entity using Entity annotation //map table name using Table annoation @Entity @Table(name="books") public class Book implements Serializable{ private int id; private String name; public Book(){ } //mark id as primary key with autogenerated value //map database column id with id field @Id @GeneratedValue(strategy= GenerationType.IDENTITY) @Column(name="id") public int getId() { return id; } ... }

創建數據源和持久性單元

DataSource (jboss-ds.xml)

PostgresDS jdbc:postgresql://localhost:5432/postgres org.postgresql.driver sa sa 5 20 5

Persistence Unit (persistence.xml)

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence\_1\_0.xsd"\> <persistence-unit name="EjbComponentPU" transaction-type="JTA"> java:/PostgresDS false <persistence-unit name="EjbComponentPU2" transaction-type="JTA"> org.hibernate.ejb.HibernatePersistence java:/PostgresDS false <property name="hibernate.hbm2ddl.auto" value="update"/>

Create Stateless EJB having EntityManager instance

@Stateless public class LibraryPersistentBean implements LibraryPersistentBeanRemote { //pass persistence unit to entityManager. @PersistenceContext(unitName="EjbComponentPU") private EntityManager entityManager; public void addBook(Book book) { entityManager.persist(book); } public List<Book> getBooks() { return entityManager.createQuery("From Books").getResultList(); } ... }

構建EJB模塊後,我們需要一個無狀態的bean,我們將在下一節要創建客戶端來訪問。

示例應用程序

讓我們創建一個測試EJB應用程序來測試EJB的持久化機制。

Step

描述

1

Create a project with a name EjbComponent under a package com.tutorialspoint.entity as explained in the EJB - Create Application chapter. You can also use the project created in EJB - Create Application chapter as such for this chapter to understand ejb persistence concepts.

2

Create Book.java under package com.tutorialspoint.entity and modify it as shown below.

3

Create LibraryPersistentBean.java and LibraryPersistentBeanRemote as explained in the EJB - Create Application chapter and modify them as shown below.

4

Create jboss-ds.xml in EjbComponent > setup folder and persistence.xml in EjbComponent > src > conf folder. These folder can be seen in files tab in Netbeans. Modify these files as shown above.

5

清理並生成應用程序以確保業務邏輯是按要求工作。

6

最後,將應用程序部署在JBoss應用服務器上的jar文件的形式。 JBoss應用服務器將自動開始瀏覽網頁,如果它尚未啓動。

7

Now create the ejb client, a console based application in the same way as explained in theEJB - Create Application chapter under topic Create Client to access EJB. Modify it as shown below.

EJBComponent (EJB Module)

Book.java

package com.tutorialspoint.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EntityListeners; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="books") public class Book implements Serializable{ private int id; private String name; public Book(){ } @Id @GeneratedValue(strategy= GenerationType.IDENTITY) @Column(name="id") public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

LibraryPersistentBeanRemote.java

package com.tutorialspoint.stateless; import com.tutorialspoint.entity.Book; import java.util.List; import javax.ejb.Remote; @Remote public interface LibraryPersistentBeanRemote { void addBook(Book bookName); List<Book> getBooks(); }

LibraryPersistentBean.java

package com.tutorialspoint.stateless; import com.tutorialspoint.entity.Book; import java.util.List; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @Stateless public class LibraryPersistentBean implements LibraryPersistentBeanRemote { public LibraryPersistentBean(){ } @PersistenceContext(unitName="EjbComponentPU") private EntityManager entityManager; public void addBook(Book book) { entityManager.persist(book); } public List<Book> getBooks() { return entityManager.createQuery("From Book").getResultList(); } }

  • As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.

  • JBoss has automatically created a JNDI entry for our session bean -LibraryPersistentBean/remote.

  • We'll using this lookup string to get remote business object of type -com.tutorialspoint.stateless.LibraryPersistentBeanRemote

JBoss應用服務器的日誌輸出

...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface
LibraryPersistentBean/remote-com.tutorialspoint.stateless.LibraryPersistentBeanRemote - EJB3.x Remote Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=EjbComponent.jar,name=LibraryPersistentBeanRemote,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB: com.tutorialspoint.stateless.LibraryPersistentBeanRemote ejbName: LibraryPersistentBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface
LibraryPersistentBean/remote-com.tutorialspoint.stateless.LibraryPersistentBeanRemote - EJB3.x Remote Business Interface
...

EJBTester (EJB Client)

jndi.properties

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost

  • These properties are used to initialize the InitialContext object of java naming service

  • InitialContext object will be used to lookup stateless session bean

EJBTester.java

package com.tutorialspoint.test; import com.tutorialspoint.stateless.LibraryPersistentBeanRemote; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import java.util.Properties; import javax.naming.InitialContext; import javax.naming.NamingException; public class EJBTester { BufferedReader brConsoleReader = null; Properties props; InitialContext ctx; { props = new Properties(); try { props.load(new FileInputStream("jndi.properties")); } catch (IOException ex) { ex.printStackTrace(); } try { ctx = new InitialContext(props); } catch (NamingException ex) { ex.printStackTrace(); } brConsoleReader = new BufferedReader(new InputStreamReader(System.in)); } public static void main(String[] args) { EJBTester ejbTester = new EJBTester(); ejbTester.testEntityEjb(); } private void showGUI(){ System.out.println("**********************"); System.out.println("Welcome to Book Store"); System.out.println("**********************"); System.out.print("Options

  1. Add Book
  2. Exit
    Enter Choice: "); } private void testEntityEjb(){ try { int choice = 1; LibraryPersistentBeanRemote libraryBean = LibraryPersistentBeanRemote)ctx.lookup("LibraryPersistentBean/remote"); while (choice != 2) { String bookName; showGUI(); String strChoice = brConsoleReader.readLine(); choice = Integer.parseInt(strChoice); if (choice == 1) { System.out.print("Enter book name: "); bookName = brConsoleReader.readLine(); Book book = new Book(); book.setName(bookName); libraryBean.addBook(book); } else if (choice == 2) { break; } } List<Book> booksList = libraryBean.getBooks(); System.out.println("Book(s) entered so far: " + booksList.size()); int i = 0; for (Book book:booksList) { System.out.println((i+1)+". " + book.getName()); i++; } } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); }finally { try { if(brConsoleReader !=null){ brConsoleReader.close(); } } catch (IOException ex) { System.out.println(ex.getMessage()); } } } }

EJBTester is doing the following tasks.

  • Load properties from jndi.properties and initialize the InitialContext object.

  • In testStatefulEjb() method, jndi lookup is done with name - "LibraryStatefulSessionBean/remote" to obtain the remote business object (stateful ejb).

  • Then user is shown a library store User Interface and he/she is asked to enter choice.

  • If user enters 1, system asks for book name and saves the book using stateless session bean addBook() method. Session Bean is persisting the book in database via EntityManager call.

  • If user enters 2, system retrieves books using stateful session bean getBooks() method and exits.

  • Then another jndi lookup is done with name - "LibraryStatelessSessionBean/remote" to obtain the remote business object (stateless ejb) again and listing of books is done.

運行客戶端訪問EJB

在項目資源管理器中找到EJBTester.java。右鍵點擊上EJBTester類,並選擇 run file.

在Netbeans控制檯驗證以下輸出。

run:
**********************
Welcome to Book Store
**********************
Options

  1. Add Book
  2. Exit
    Enter Choice: 1
    Enter book name: Learn Java
    **********************
    Welcome to Book Store
    **********************
    Options
  3. Add Book
  4. Exit
    Enter Choice: 2
    Book(s) entered so far: 1
  5. learn java
    BUILD SUCCESSFUL (total time: 15 seconds)

再次運行客戶端來訪問EJB。

訪問EJB之前重新啓動JBoss。

在項目資源管理器中找到EJBTester.java。右鍵點擊上EJBTester類,並選擇 run file.

在Netbeans控制檯驗證以下輸出。

run:
**********************
Welcome to Book Store
**********************
Options

  1. Add Book
  2. Exit
    Enter Choice: 1
    Enter book name: Learn Spring
    **********************
    Welcome to Book Store
    **********************
    Options
  3. Add Book
  4. Exit
    Enter Choice: 2
    Book(s) entered so far: 2
  5. learn java
  6. Learn Spring
    BUILD SUCCESSFUL (total time: 15 seconds)
  • 上面顯示的輸出狀態書存儲在持久性存儲,並從數據庫中檢索。