2011-12-23 79 views
1

假設我有以下形式的Oracle存儲過程:休眠 - 傳遞一個實體作爲參數傳遞給一個SQL查詢

procedure validate_entity1(p_id number, p_property1 varchar2, p_property2 varchar2); 

我需要從我的Java應用程序中調用它,所以我做了一個sql-query像這個:

<sql-query name="q1" callable="true"> 
    <!-- . . . --> 
    { call validate_entity1(:id, :property1, :property2) } 
</sql-query> 

然後我創建這個查詢並手動綁定所有這些屬性。我真正想要做的是綁定整個實體,像這樣:

session.createNamedQuery("q1").setEntity("entity1", myEntity1); 

但它不起作用。以某種方式可以做到這一點嗎?

回答

0

我不認爲你可以將實體直接映射到存儲過程參數。 您必須逐個傳遞參數。

如果要調用存儲過程,如果你有春天,試試這個:

我調用過程「rex_datainterface.getfieldtype」,我經過2個參數:

  • recoType( NUMBER)IN。
  • 結果(CURSOR)OUT。

存儲過程的結果是一個

List<RecordsetDTO> 

這裏是代碼:

@Component 
    public class RexFieldTypeDAO { 

    @Resource(name = "REXDataSource") 
    private DataSource lyxsrvREXDataSource; 

// ------------------------------------------------------------------------ 

public RexFieldTypeDAO() { 
} 

// ------------------------------------------------------------------------ 

public RecordSetDTO getFieldTypes(int recoType) { 
    RecordSetDTO res; 

    GetFieldTypeStoredProcedure proc = new GetFieldTypeStoredProcedure(lyxsrvREXDataSource); 
    res = proc.execute(recoType); 

    return res; 
} 

/** 
* Private class used to access Stored Procedure. 
*/ 
private static class GetFieldTypeStoredProcedure extends StoredProcedure { 
    private static final String SQL = "rex_datainterface.getfieldtype"; 

    public GetFieldTypeStoredProcedure(DataSource dataSource) { 
     super(dataSource, SQL); 
     setFunction(true); 
     declareParameter(new SqlOutParameter("result", OracleTypes.CURSOR, new RecordSetDTORowMapper())); 
     declareParameter(new SqlParameter("recoType", OracleTypes.NUMBER)); 
     compile(); 
    } 

    public List<RecordSetDTO> execute(int recoType) { 
     Map<String, Object> inputs = new HashMap<String, Object>(); 

     inputs.put("recoType", recoType); 
     return (List<RecordSetDTO>)super.execute(inputs).get("result"); 
    } 

} 

}

相關問題