<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    編程生活

       :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      113 隨筆 :: 0 文章 :: 18 評論 :: 0 Trackbacks
    openJPA的用戶說明文檔中說明,在用createNativeQuery時,如果不是SELEC,則統(tǒng)一按存儲過程調(diào)用處理,但Oracle的存儲過程返回結(jié)果集的情況與MSSQL和Sybase等數(shù)據(jù)庫均不相同,它是返回一個引用游標(biāo),而不是直接返回結(jié)果集,即對CallableStatement不能直接調(diào)用executeQuery來取得結(jié)果集,而必須先用excute,然后用statement.getObject(int index)來取得結(jié)果集,因此,必須對源程序進行修改(0.9.7-1.0.0均要改)。按openJPA的設(shè)計思想,我在DBDictionary.java中(openjpa-jdbc組件下)增加兩個方法

    public class DBDictionary
        
    implements Configurable, ConnectionDecorator, JoinSyntaxes,
        LoggingConnectionDecorator.SQLWarningHandler 


    /**
         * register stored procedure resultset parameters
         * 
    @param stmnt
         * 
    @throws SQLException
         
    */

        
    public void regStoredProcedureResultParam(CallableStatement stmnt) throws SQLException {
        }

        
        
    /**
         * get stored procedure resultset
         * 
    @param stmt
         * 
    @return 
         * 
    @throws SQLException
         
    */

        
    public ResultSet getStoredProcedureOutResult(CallableStatement stmnt) throws SQLException {
          
    return null;
        }
     
    }
    然后修改OracleDictionary.java(openjpa-jdbc組件下),具體實現(xiàn)這兩個方法
    public class OracleDictionary
        
    extends DBDictionary {

        
    /**
         * register stored procedure resultset parameters
         * 
    @param stmnt
         * 
    @throws SQLException
         
    */

        
    public void regStoredProcedureResultParam(CallableStatement stmnt) throws SQLException {
          stmnt.registerOutParameter(
    1-10);
        }

        
        
    /**
         * get stored procedure resultset
         * 
    @param stmt
         * 
    @return 
         * 
    @throws SQLException
         
    */

        
    public ResultSet getStoredProcedureOutResult(CallableStatement stmnt) throws SQLException {
          stmnt.execute();
          
    return (ResultSet) stmnt.getObject(1);
        }
        
    }

    對于其它的數(shù)據(jù)庫,請打開對應(yīng)的Dictionary.java,進行針對性修改(我只用Oracle)
    然后再修改SQLStoreQuery.java
    public class SQLStoreQuery extends AbstractStoreQuery {
    private static class SQLExecutor extends AbstractExecutor {

    public ResultObjectProvider executeQuery(StoreQuery q, Object[] params, Range range) {
          JDBCStore store 
    = ((SQLStoreQuery) q).getStore();
          DBDictionary dict 
    = store.getDBDictionary();
          String sql 
    = q.getContext().getQueryString();

          List paramList;
          
    if (params.length > 0{
            paramList 
    = new ArrayList(Arrays.asList(params));
            
    try {
              sql 
    = substituteParams(sql, paramList);
            }
     catch (IOException ioe) {
              
    throw new UserException(ioe);
            }

          }
     else
            paramList 
    = Collections.EMPTY_LIST;

          SQLBuffer buf 
    = new SQLBuffer(dict).append(sql);
          Connection conn 
    = store.getConnection();
          JDBCFetchConfiguration fetch 
    = (JDBCFetchConfiguration) q.getContext().getFetchConfiguration();

          
    boolean wrcall = false;
          ResultObjectProvider rop;
          PreparedStatement stmnt 
    = null;
          
    try {
            
    // use the right method depending on sel vs. proc, lrs setting
            if (_select && !range.lrs)
              stmnt 
    = buf.prepareStatement(conn);
            
    else if (_select)
              stmnt 
    = buf.prepareStatement(conn, fetch, -1-1);
            
    else {
              
    // stored procedure
              if (!range.lrs)
                stmnt 
    = buf.prepareCall(conn);
              
    else
                stmnt 
    = buf.prepareCall(conn, fetch, -1-1);
              
    // if the stored procedure has resultset,set the out resultset param
              if (q.getContext().getResultType() != null{
                dict.regStoredProcedureResultParam((CallableStatement) stmnt);
                wrcall 
    = true;
              }

            }

            
    int index = 0;
            
    for (Iterator i = paramList.iterator(); i.hasNext();)
              dict.setUnknown(stmnt, 
    ++index, i.next(), null);

            ResultSetResult res 
    = new ResultSetResult(conn, stmnt, wrcall ? dict
                .getStoredProcedureOutResult((CallableStatement) stmnt) : stmnt.executeQuery(), store);
            
    if (_resultMapping != null)
              rop 
    = new MappedQueryResultObjectProvider(_resultMapping, store, fetch, res);
            
    else if (q.getContext().getCandidateType() != null)
              rop 
    = new GenericResultObjectProvider((ClassMapping) _meta, store, fetch, res);
            
    else
              rop 
    = new SQLProjectionResultObjectProvider(store, fetch, res, q.getContext().getResultType());
          }
     catch (SQLException se) {
            
    if (stmnt != null)
              
    try {
                stmnt.close();
              }
     catch (SQLException se2) {
              }

            
    try {
              conn.close();
            }
     catch (SQLException se2) {
            }

            
    throw SQLExceptions.getStore(se, dict);
          }


          
    if (range.start != 0 || range.end != Long.MAX_VALUE)
            rop 
    = new RangeResultObjectProvider(rop, range.start, range.end);
          
    return rop;
        }

    用maven重新編譯和打包,即可。
    用法:
    EntityManager em = ...
    Query q = em.createNativeQuery("{?=call a()}", A.class);
    List<?> list q.getResultList();
    posted on 2007-10-30 09:14 wilesun 閱讀(1527) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国产亚洲精品不卡在线| 在线观看国产一区亚洲bd| 在线看片人成视频免费无遮挡| 一级毛片在播放免费| 亚洲乱码av中文一区二区| 久久亚洲精品成人| 亚洲午夜激情视频| 精品久久久久久久免费加勒比| 足恋玩丝袜脚视频免费网站| 丁香花在线视频观看免费 | 国产日韩一区二区三免费高清| 久久亚洲精品无码av| 亚洲伊人久久精品| 亚洲一区二区三区高清| 亚洲无线码在线一区观看| 亚洲第一成人影院| 四虎免费永久在线播放| 青青在线久青草免费观看| 色欲色香天天天综合网站免费| 国产精品免费久久久久影院| 免费一级毛片在线播放放视频 | 67194国产精品免费观看| 久久一本岛在免费线观看2020 | 亚洲AV永久纯肉无码精品动漫| ZZIJZZIJ亚洲日本少妇JIZJIZ| 国产成人免费全部网站| 日韩免费三级电影| 女人18毛片a级毛片免费| 日本精品人妻无码免费大全| 国产福利在线免费| 色窝窝免费一区二区三区| 无码人妻一区二区三区免费手机 | 亚洲日产韩国一二三四区| 久久久久亚洲av成人无码电影| 亚洲第一区在线观看| 免费人成视网站在线观看不卡| 国产一级一片免费播放i| 四虎影视永久免费观看网址| 亚洲成?v人片天堂网无码| 在线观看午夜亚洲一区| 亚洲精品白浆高清久久久久久|