Customizing SQL Querying with this stored procedure in the application looks like any other named query execution: Query q = session.getNamedQuery(”loadUsersByRank”); q.setParameter(”rank”, 12); List result = q.list(); At the time of writing, mapped stored procedures can be enabled as named queries, as you did in this section, or as loaders for an entity, similar to the loadUser example you mapped earlier. Stored procedures can not only query and load data, but also manipulate data. The first use case for this is mass data operations, executed in the database tier. You shouldn t map this in Hibernate but should execute it with plain JDBC: session.connection().prepareCallableStatement(); and so on. The data-manipulation operations you can map in Hibernate are the creation, deletion, and update of an entity object. Mapping CUD to a procedure Earlier, you mapped , , and elements for a class to custom SQL statements. If you d like to use stored procedures for these operations, change the mapping to callable statements: … { call UPDATE_USER(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) } With the current version of Hibernate, you have the same problem as before: the binding of values to the positional parameters. First, the stored procedure must have the same number of input parameters as expected by Hibernate (enable the SQL log as shown earlier to get a generated statement you can copy and paste). The parameters again must be in the same order as expected by Hibernate. Consider the check=”none” attribute. For correct (and, if you enabled it) optimistic locking, Hibernate needs to know whether this custom update operation was successful. Usually, for dynamically generated SQL, Hibernate looks at the number of updated rows returned from an operation. If the operation didn t or couldn t update any rows, an optimistic locking failure occurs. If you write your own custom SQL operation, you can customize this behavior as well. With check=”none”, Hibernate expects your custom procedure to deal internally with failed updates (for example, by doing a version check of the row that
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.
This entry was posted
on Sunday, March 9th, 2008 at 9:26 am and is filed under PHP5.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.