Posted on 2010-01-13 22:45
斷點(diǎn) 閱讀(395)
評(píng)論(0) 編輯 收藏 所屬分類:
EJB3.0
實(shí)體bean:
1.它屬于java持久化規(guī)范(簡(jiǎn)稱JPA)里的技術(shù),Entitybean通過元數(shù)據(jù)在javabean和數(shù)據(jù)庫(kù)表之間建立起映射關(guān)系,然后Java程序員就可以使用面向?qū)ο蟮木幊趟枷雭聿倏v數(shù)據(jù)庫(kù)。
2.通過注解使實(shí)體bean與數(shù)據(jù)庫(kù)表相映射。
3.當(dāng)客戶端遠(yuǎn)程調(diào)用EJB時(shí),數(shù)據(jù)在傳送工程中是需要序列化的 ,業(yè)務(wù)方法是要返回Entitybean對(duì)象到客戶端,顯然Entitybean對(duì)象是要進(jìn)行序列化的過程,所以要實(shí)現(xiàn)序列化接口,即對(duì)象在交互時(shí)需要實(shí)現(xiàn)序列化。
4.Entitybean主鍵值的生成方式。
@GeneratedValue(strategy = GenerationType.IDENTITY)針對(duì)Mysql,
@GeneratedValue(strategy = GenerationType.SEQUENCE)針對(duì)Oracle,
@GeneratedValue(strategy = GenerationType.AUTO)自動(dòng)匹配數(shù)據(jù)庫(kù)。
5.需要保留一個(gè)無參的構(gòu)造函數(shù),是JPA規(guī)范要求的,如果沒有,在運(yùn)行時(shí)是要報(bào)錯(cuò)的。
6.對(duì)象之間的比較,通常采用對(duì)象標(biāo)志屬性來進(jìn)行比較,也就是ID進(jìn)行比較,因此現(xiàn)在重載hashCode、equals兩個(gè)方法。
JPA的出現(xiàn)主要是為了簡(jiǎn)化現(xiàn)有的持久化開發(fā)工作和整合ORM技術(shù),目前實(shí)現(xiàn)的JPA規(guī)范的主流產(chǎn)品有Hibernate、Toplink和OpenJPA,在jboss中采用了Hibernate作為其持久化實(shí)現(xiàn)產(chǎn)品。
根據(jù)JPA規(guī)范的要求:在實(shí)體bean應(yīng)用中,我們需要在應(yīng)用的類路徑下的META-INF目錄加入持久化配置文件persistence.xml。
persistence.xml文件用于指定Entitybean使用的數(shù)據(jù)源及EntityManager對(duì)象的默認(rèn)行為。
persistence.xml的配置說明如下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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" version="1.0">
<!-- 一個(gè)持久化單元是一個(gè)Entitybean的集合,JTA代表全局事物。-->
<persistence-unit name="ztf" transaction-type="JTA">
<jta-data-source>java:/ztfDS</jta-data-source>
<properties>
<!-- 根據(jù)元數(shù)據(jù)生成數(shù)據(jù)庫(kù)的表結(jié)構(gòu)。 -->
<property name="hibernate.hbm2ddl.auto" value="update"/>
<!-- 顯示執(zhí)行的SQL。 -->
<property name="hibernate.show_sql" value="true"/>
<!-- 格式化顯示的SQL 。 -->
<property name="hibernate.format_sql" value="true"/>
<!-- SQL方言。 -->
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
</properties>
</persistence-unit>
</persistence>
注意:因?yàn)閖boss 采用Hibernate,Hibernate 有一項(xiàng)屬性hibernate.hbm2ddl.auto,該屬性指定實(shí)體Bean發(fā)布時(shí)是否同步數(shù)據(jù)庫(kù)結(jié)構(gòu)。
1.如果hibernate.hbm2ddl.auto的值設(shè)為create-drop,在實(shí)體Bean發(fā)布及卸載時(shí)將自動(dòng)創(chuàng)建及刪除相應(yīng)數(shù)據(jù)庫(kù)表(注意:Jboss 服務(wù)器啟動(dòng)或關(guān)閉時(shí)也會(huì)引發(fā)實(shí)體Bean 的發(fā)布及卸載)。
2.如果hibernate.hbm2ddl.auto的值設(shè)為update,以后為了實(shí)體bean的改動(dòng)能反應(yīng)到數(shù)據(jù)表,建議使用update,這樣實(shí)體Bean 添加一個(gè)屬性時(shí)能同時(shí)在數(shù)據(jù)表增加相應(yīng)字段。
posted @ 2009-03-26 12:35 斷點(diǎn) 閱讀(166) | 評(píng)論 (0)