Create and use a stored procedure with Play Framework and JPA

我的未来我决定 提交于 2019-12-07 10:29:40

问题


I'm using Play framework 1.2.5 and I would like to optimize my SQL queries by creating stored procedures and using them but I don't know how to do.

To create the stored procedure via the Java code how should I do ? Also, should I do it in an @OnApplicationStart job so that I'm sure the procedures are created and stored when the application starts ?

After, how can I use my stored procedures ? Using which function ? How can I pass the parameters to my procedure ? How can I retrieve the result of my procedure ? (generally the result will be a SELECT query) And finally, is it possible to bind the result of my procedure to a model in the play framework ?

I have a lot of questions but I'm new to stored procedures with play framework and JPA and I would like to be sure I'm using them correctly

Thank you for your help


回答1:


I don't know how you should create them. Perhaps the OnApplicationStart method is what you need. In my environment the procedures are already in place. We just use Play to invoke them. To invoke stored procedures, you should take a look at the Work interface. By implementing this you can execute statements in the database.

We've created a basic OracleProcedure class:

public class CallOracleProcedure implements Work {

    private String anonymousPLSQL;
    private String[] parameters;

    public CallOracleProcedure(String anonymousPLSQL, String[] parameters) {
        this.anonymousPLSQL = anonymousPLSQL;
        this.parameters = parameters.clone();
    }

    /**
     * Create a JDBC PreparedStatement and then execute the anonymous
     * PL/SQL procedure.
     */
    @Override
    public void execute(Connection connection) {
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement("begin " + anonymousPLSQL + "; end;");

            if (parameters != null) {
                int i = 1;
                for (String param : parameters) {
                    statement.setString(i++, param);
                }
            }
            statement.executeUpdate();
        } catch (SQLException e) {
            Logger.error("Error performing anonymous pl/sql statement: '%s', with parameters: '%s' - catched error '%s'", anonymousPLSQL, parameters, e);
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (Exception e) {
                    Logger.error("Error closing statement: %s", e);
                }
            }
        }
    }
}

For each specific stored procedure you can extend this class and pass the name and parameters to the constructor via super():

public class StoredProcedureCall extends CallOracleProcedure {
    public StoredProcedureCall(String param) {
        super("package.storedprocedure(?)", new String[] { orgname });
    }
}

In your code you can then call it like this:

StoredProcedureCall procedure = new StoredProcedureCall("your parameter");
session.doWork(procedure);

If you need to call a procedure and retrieve a return value you can use a CallableStatement in the execute() method:

public class ProcedureWithReturnValue implements Work {

    private final String parameter;
    private String returnValue = null;

    public ProcedureWithReturnValue (final String parameter) {
        this.parameter = parameter;
    }

    @Override
    public void execute(Connection connection) {
        CallableStatement statement = null;

        try {   
            statement = connection.prepareCall("begin ? := package.procedure(?); end;");
            statement.registerOutParameter(1, OracleTypes.VARCHAR);
            statement.setString(2, parameter);
            statement.execute();

            returnValue = statement.getString(1);
        } catch (SQLException e) {
            Logger.error("Error getting return value - catched error '%s'",  e);
        }
    }

    public String getReturnValue() {
        return returnValue;
    }
}



回答2:


Take a look at evolutions (http://www.playframework.com/documentation/1.2.7/evolutions) for creating your stored procedures.



来源:https://stackoverflow.com/questions/15246245/create-and-use-a-stored-procedure-with-play-framework-and-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!