Results 1 to 10 of 18

Thread: Common approach to stored procedure

Hybrid View

  1. #1
    Join Date
    Aug 2005
    Location
    Vancouver, Canada
    Posts
    15

    Default Common approach to stored procedure

    What is the common approach to using a stored procedure within a spring & hibernate mix?

    This is what Im currently implementing and just wanted some general feedback:
    1) I implemented a class that extended the StoredProcedure class.

    2) I created a DAO that extends HibernateDaoSupport and inject the dao impl with a datasource object and session factory object. I do this because the class that implements the StoredProcedure class requires a datasource passed into the constructor, plus I didnt think I should call it directly from the service class.

    3) To call the stored procedure, I have a method in the DAO that will be called from within a service object.

    Is this a good approach?

  2. #2

    Default

    This is indeed the best approach. One additional comment: make sure you flush the Hibernate session. This is required if you do updates or inserts in the transaction before you call the stored procedure that have to visible by the stored procedure. Without flush these changes will not be persisted to the database.

  3. #3
    Join Date
    Nov 2005
    Posts
    5

    Thumbs up

    It'll be great if you can post some examples, like the code for your StoredProcedure class, the dao and a client.
    You should include them as best practices.

    Regards

  4. #4
    Join Date
    Aug 2005
    Location
    Vancouver, Canada
    Posts
    15

    Default

    Hope this helps.

    Here is my class that extends the StoredProcedure class:

    Code:
    public class CallGetPolicyNumber extends StoredProcedure {
        private static final Log log = LogFactory.getLog(CallGetPolicyNumber.class);
    
        private static final String STORED_PROC_NAME = "dbproc_get_policy_number";
    
        public CallGetPolicyNumber(DataSource ds) {
            super(ds, STORED_PROC_NAME);
    
            declareParameter(new SqlParameter("p_product_code", Types.VARCHAR));
            declareParameter(new SqlOutParameter("p_policy_number", Types.VARCHAR));
            declareParameter(new SqlParameter("p_link_type", Types.VARCHAR));
            declareParameter(new SqlParameter("p_agent_code", Types.VARCHAR));
            declareParameter(new SqlParameter("p_auth_number", Types.DECIMAL));
            compile();
        }
    
        public String execute(String productCode, String policyNumber,
                String linkType, String partnerCode, BigDecimal authorization) {
            Map inParams = new HashMap(5);
            inParams.put("p_product_code", productCode);
            inParams.put("p_policy_number", policyNumber);
            inParams.put("p_link_type", linkType);
            inParams.put("p_agent_code", partnerCode);
            inParams.put("p_auth_number", authorization);
    
            Map outParams = execute(inParams);
            if (outParams.size() > 0) {
                return outParams.get("p_policy_number").toString();
            } else {
                return null;
            }
        }
    }
    Here is my DAO class:
    Code:
    public class HibPolicyDAO extends HibernateDaoSupport implements PolicyDAO {
        private static final Log log = LogFactory.getLog(HibPolicyDAO.class);
    
        private DataSource dataSource;
    
        public void setDataSource(DataSource dataSource) {
            this.dataSource = dataSource;
        }
    
        public String getPolicyNumber(Policy policy)
                throws UnableToRetrieveException {
            CallGetPolicyNumber proc = new CallGetPolicyNumber(dataSource);
            String policyNumber = proc.execute(policy.getProductCode(), policy
                    .getPolicyNumber(), null, policy.getPartner().getPartnerCode(),
                    policy.getAuthorization());
            
            return policyNumber;
        }
    }
    Here is my service class:

    Code:
    public class PolicyServiceImpl implements PolicyService {
        private static final Log log = LogFactory.getLog(PolicyServiceImpl.class);
    
        private PolicyDAO policyDAO;
    
        public void setPolicyDAO(PolicyDAO policyDAO) {
            this.policyDAO = policyDAO;
        }
    
        public Policy processPolicy(Policy policy) throws PolicyException {
            // -------------------------------------------------
            // Get policy Number
            // -------------------------------------------------
            String policyNumber = "";
            try {
                log.debug("Getting Policy Number");
                policyNumber = policyDAO.getPolicyNumber(policy);
                policy.setPolicyNumber(policyNumber);
            } catch (UnableToRetrieveException e) {
                log.error(e + " Policy [" + policy + "]");
                throw new PolicyException("error.get.policy.number");
            }
         
    ................ other business logic

  5. #5
    Join Date
    Nov 2005
    Posts
    5

    Smile

    awsome, thxs mate.
    Cheers

  6. #6
    Join Date
    Nov 2005
    Posts
    1

    Smile

    Hi,

    The example seems to provide a good understanding to call stored procs.

    I am trying to use the same method,but not able to provide right data source.

    Can you plz provide some inputs as to how i can get the datasource in the DAO.

    I'm using hibernate with spring in this case

  7. #7
    Join Date
    Oct 2010
    Posts
    1

    Default

    Hi,

    I have tried the same way.. but parameters are not correctly passed to procedure.. I'm trying to invoke procedure inside a package.. All the parameters seems to have null values after passing.. Does any one have same experience? Please help..

  8. #8
    Join Date
    Jun 2011
    Posts
    1

    Default

    I followed the same approach (Refering to jakim8915 post) which you have mentioned, in calling the stored procedure, it throws the following exception
    (I have created Prcedure "getStateName" which takes one parameter (StateID) and should return State Name.). Can you please tell me what is that I am missing. Thanks in advance.

    Exception:
    org.springframework.dao.TransientDataAccessResourc eException: CallableStatementCallback; SQL [{call getStateName(?)}]; Callable statments not supported.; nested exception is java.sql.SQLException: Callable statments not supported.
    at org.springframework.jdbc.support.SQLStateSQLExcept ionTranslator.doTranslate(SQLStateSQLExceptionTran slator.java:106)
    at org.springframework.jdbc.support.AbstractFallbackS QLExceptionTranslator.translate(AbstractFallbackSQ LExceptionTranslator.java:72)
    at org.springframework.jdbc.support.AbstractFallbackS QLExceptionTranslator.translate(AbstractFallbackSQ LExceptionTranslator.java:80)
    at org.springframework.jdbc.support.AbstractFallbackS QLExceptionTranslator.translate(AbstractFallbackSQ LExceptionTranslator.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute (JdbcTemplate.java:952)
    at org.springframework.jdbc.core.JdbcTemplate.call(Jd bcTemplate.java:985)
    at org.springframework.jdbc.object.StoredProcedure.ex ecute(StoredProcedure.java:117)
    at com.wipro.daoImpl.CallPocedure.execute(CallPocedur e.java:33)
    at com.wipro.daoImpl.CallProcedureDaoImpl.getStateNam e(CallProcedureDaoImpl.java:32)
    at com.wipro.serviceImpl.CallProcedureServiceImpl.get StateName(CallProcedureServiceImpl.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.invokeJoinpoint(ReflectiveMethodInvocat ion.java:182)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :149)
    at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:106)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :171)
    at org.springframework.aop.interceptor.ExposeInvocati onInterceptor.invoke(ExposeInvocationInterceptor.j ava:89)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :171)
    at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy1.getStateName(Unknown Source)
    at com.wipro.client.CallProcedureClient.getStateName( CallProcedureClient.java:25)
    at com.wipro.client.CallProcedureClient.main(CallProc edureClient.java:36)
    Caused by: java.sql.SQLException: Callable statments not supported.
    at com.mysql.jdbc.Connection.prepareCall(Connection.j ava:1287)
    at org.springframework.jdbc.core.CallableStatementCre atorFactory$CallableStatementCreatorImpl.createCal lableStatement(CallableStatementCreatorFactory.jav a:167)
    at org.springframework.jdbc.core.JdbcTemplate.execute (JdbcTemplate.java:930)
    Last edited by MPatric; Jul 1st, 2011 at 12:23 AM.

  9. #9
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    1. Such questions should be posted Data forum (http://forum.springsource.org/forumdisplay.php?27-Data).
    2. It looks as if a JDBC driver you are using does not support a stored procedures (nested exception is java.sql.SQLException: Callable statments not supported.). So you either have to use a different JDBC driver for your DB (if exists) or use another DB.

  10. #10
    Join Date
    May 2008
    Posts
    1

    Default Need help in handling cursor being returned

    I have a oracle stored procedure that I am calling which returns a cursor
    and I am doubtful about what to use when i use SqlOutParameter for declaring a Parameter in the constructor.

    Any suggestions?

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •