細(xì)心!用心!耐心!

          吾非文人,乃市井一俗人也,讀百卷書,跨江河千里,故申城一游; 一兩滴辛酸,三四年學(xué)業(yè),五六點(diǎn)粗墨,七八筆買賣,九十道人情。

          BlogJava 聯(lián)系 聚合 管理
            1 Posts :: 196 Stories :: 10 Comments :: 0 Trackbacks
          JPA本地查詢(Native Query)(二)
          2010-01-20 10:49

          OK,現(xiàn)在對(duì)同一個(gè)查詢,再把結(jié)果集全部映射進(jìn)實(shí)體對(duì)象。

           

          首先

          @NamedNativeQueries

          (

              {

                 @NamedNativeQuery(

                     name="ReturnOrderListWithFullEntityType",

                        query="select o.id as order_id,o.create_date as order_creation_date,o.description as order_description,o.sum_price as order_sum_total,

          c.name as customer_name,c.ctype as customer_type,c.id as customer_id from orders o join customer c on o.cust_id=c.id where o.cust_id=?1",

                     resultSetMapping="ReturnOrderListWithFullEntityType"),

                  。。。。。。。。。。。。??赡苓€有更多的本地查詢?cè)O(shè)置

          }

          )

          改變本地查詢的resultSetMapping

           

              @SqlResultSetMapping

              (

                 name="ReturnOrderListWithFullEntityType",

                 entities=

                 {

                     @EntityResult

                     (

                        entityClass=entity.Order.class,

                        fields=

                        {

              @FieldResult(name="id",column="order_id"),

                                   @FieldResult(name="date",column="order_creation_date"),

                            @FieldResult(name="desc",column="order_description"),

              @FieldResult(name="sum",column="order_sum_total")

                        }

                     ),

           

                     @EntityResult

                     (

                        entityClass=entity.Customer.class,

                        discriminatorColumn="customer_type",

                        fields=

                        {

                            @FieldResult(name="id",column="customer_id"),

                            @FieldResult(name="name",column="customer_name")

                        }

                     )

                 },

                 columns={}

              )

           

          entities屬性是一個(gè)包含與返回結(jié)果集相映射的所有實(shí)體的一個(gè)數(shù)組。這條查詢得到了訂單和用戶的信息,所以,與結(jié)果集映射的實(shí)體,就應(yīng)該是OrderCustomer。

          每一個(gè)映射實(shí)體用@EntityResult表示,entityClass表示實(shí)體類,fields表示該實(shí)體類中的屬性,與查詢結(jié)果中的哪些個(gè)列相映射。而每一個(gè)“屬性<->列”的映射關(guān)系,又保存在@FieldResult里面。name是實(shí)體屬性,column是查詢列(或列別名)

           

          要注意的是,如果要將結(jié)果集映射到實(shí)體對(duì)象,則

          1Select 語句中必須包含實(shí)體所映射的表中的PK,也就是 select o.id as order_id

           

          2. 反過來,“屬性<->列”的映射,也必須將表的PK映射到實(shí)體類的用@Id annotation批注的字段或?qū)傩陨稀?/span>

           

          否則,JPA就會(huì)拋出一個(gè)exception

          oracle.toplink.essentials.exceptions.QueryException

          Exception Description: The primary key read from the row [DatabaseRecord(

              orders.ID => null

              orders.CREATE_DATE => 2009-05-27 00:00:00.0

              orders.SUM_PRICE => 36817.0

              orders.DESCRIPTION => This is an order creation example.

              orders.cust_id => null)] during the execution of the query was detected to be null. Primary keys must not contain null.

           

           

           

           

           

          再看一下關(guān)于Customer實(shí)體的映射,與Order不一樣的地方,是discriminatorColumn="customer_type" (可能為列別名)

          加上這個(gè)屬性的原因是,Customer類是一個(gè)父類,以供其他子類繼承,而J PA的內(nèi)在多態(tài)性機(jī)制將會(huì)獲取到Customer的實(shí)際類型。根據(jù)J PA @Inheritance批注,得知,父類實(shí)體的表中必須有一個(gè)字段(默認(rèn)為DTYPE)來表示各個(gè)子類的類型。所以當(dāng)要將查詢結(jié)果集保存為實(shí)體的時(shí)候,它必須要知道你所保存的這個(gè)Customer實(shí)體到底是哪種類型。

          所以,你還必須在select子句中取得這個(gè)Discrimator Type的字段。

          否則又會(huì)拋出異常: 

          oracle.toplink.essentials.exceptions.QueryException

          Exception Description: Custom SQL failed to provide descriminator column : , as defined in SQLResultSetMapping : ReturnOrderListWithFullEntityType.

          或者:

          oracle.toplink.essentials.exceptions.QueryException

          Exception Description: Custom SQL failed to provide descriminator column : customer_type, as defined in SQLResultSetMapping : ReturnOrderListWithPartEntityPartScalarType.

           

           

          通過這種映射方式,我們可以推測,得到的結(jié)果集List中,數(shù)據(jù)會(huì)是這樣:

          [ {“Order對(duì)象1”, “Customer對(duì)象1”}, {“Order對(duì)象2”, “Customerr對(duì)象2”}, …… ]

           

          經(jīng)過運(yùn)行測試程序,得到了我們的推論

           

          *****ReturnOrderListWithFullEntityType*****

          entity.Order@48edb5 entity.GoldenCustomer@1ee2c2c  

          entity.Order@1402d5a entity.GoldenCustomer@1ee2c2c  

          entity.Order@1e13e07 entity.GoldenCustomer@1ee2c2c  

          entity.Order@9cfec1 entity.GoldenCustomer@1ee2c2c  

          entity.Order@747fa2 entity.GoldenCustomer@1ee2c2c  

          ……

          看來的確是保存了對(duì)象,而且注意第二個(gè)對(duì)象不是Customer而是GoldenCustomer,這說明,JPA自動(dòng)將數(shù)據(jù)映射進(jìn)了GoldenCustomer實(shí)體(盡管我們使用的是Customer實(shí)體進(jìn)行保存 “entityClass=entity.Customer.class”


          posted on 2012-06-15 21:38 張金鵬 閱讀(7806) 評(píng)論(1)  編輯  收藏 所屬分類: JPA

          Feedback

          # re: JPA本地查詢(Native Query)(二)[未登錄] 2016-04-26 15:42 abc
          fdfdf  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 中卫市| 疏附县| 临沂市| 南木林县| 南漳县| 太仓市| 会宁县| 新安县| 马关县| 青河县| 绥棱县| 固镇县| 林西县| 安泽县| 乌海市| 玛沁县| 巴青县| 巴中市| 安吉县| 遵化市| 旬邑县| 虹口区| 庆城县| 宁远县| 新河县| 陆川县| 将乐县| 禄丰县| 蕉岭县| 屯留县| 蕲春县| 剑川县| 阿图什市| 泰和县| 荣昌县| 墨江| 保定市| 迁西县| 拜城县| 体育| 于田县|