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)
<?xml version="1.0" encoding="UTF-8"?> <datasources> <local-tx-datasource> <jndi-name>PostgresDS</jndi-name> <connection-url>jdbc:postgresql://localhost:5432/postgres</connection-url> <driver-class>org.postgresql.driver</driver-class> <user-name>sa</user-name> <password>sa</password> <min-pool-size>5</min-pool-size> <max-pool-size>20</max-pool-size> <idle-timeout-minutes>5</idle-timeout-minutes> </local-tx-datasource> </datasources>
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"> <jta-data-source>java:/PostgresDS</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties/> </persistence-unit> <persistence-unit name="EjbComponentPU2" transaction-type="JTA"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/PostgresDS</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit> </persistence>
@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. |
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; } }
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(); }
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
... 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 ...
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
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.
在專案資源管理器中找到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 1. Add Book 2. Exit Enter Choice: 2 Book(s) entered so far: 1 1. learn java BUILD SUCCESSFUL (total time: 15 seconds)
存取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 1. Add Book 2. Exit Enter Choice: 2 Book(s) entered so far: 2 1. learn java 2. Learn Spring BUILD SUCCESSFUL (total time: 15 seconds)
上面顯示的輸出狀態書儲存在永續性儲存,並從資料庫中檢索。