Write the following queries-

1. Use CreateNativeSQL Method for calling a procedure

  • Query query = session.createSQLQuery (“CALL GetAllFoos ()”).addEntity;
  • List allFoos= query.list();

2. Use @NamedNativeQueries to call a stored procedure

3. Use @NamedStoreProcedureQuery to call a stored procedure

BY Best Interview Question ON 05 Apr 2019

Example

Use @NamedNativeQueries to call a stored procedure

@NamedNativeQueries({

          @NamedNativeQuery(

   name = "callGetAllFoos",

   query = "CALL GetAllFoos()",

      resultClass = Foo.class)

                         })

@Entity

public class Foo implements Serializable {

         // Model definition

}

Use @NamedStoreProcedureQuery to call a stored procedure

@NamedStoredProcedureQuery(

     name="GetAllFoos",

     procedureName="GetAllFoos",

     resultClasses = { Foo.class }

  )

@Entity

public class Foo implements Serializable {

 // Model Definition

}