public final class DefaultSqlUpdate extends Object implements Serializable, SpiSqlUpdate
There is also Update which is similar except should use logical bean and
property names rather than physical table and column names.
SqlUpdate is designed for general DML sql and CallableSql is designed for use with stored procedures.
// String sql = "update f_topic set post_count = :count where id = :topicId";
SqlUpdate update = new SqlUpdate(sql);
update.setParameter("count", 1);
update.setParameter("topicId", 50);
int modifiedCount = Ebean.execute(update);
Note that when the SqlUpdate is executed via Ebean.execute() the sql is parsed to determine if it is an update, delete or insert. In addition the table modified is deduced. If isAutoTableMod() is true, then this is then added to the TransactionEvent and cache invalidation etc is maintained. This means you don't need to use the Ebean.externalModification() method as this has already been done.
You can sql.setAutoTableMod(false); to stop the automatic table modification
EXAMPLE: Using JDBC batching with SqlUpdate
String data = "This is a simple test of the batch processing"
+ " mode and the transaction execute batch method";
String[] da = data.split(" ");
String sql = "insert into junk (word) values (?)";
SqlUpdate sqlUpdate = Ebean.createSqlUpdate(sql);
Transaction t = Ebean.beginTransaction();
t.setBatchMode(true);
t.setBatchSize(3);
try {
for (int i = 0; i < da.length; i++) {
sqlUpdate.setParameter(1, da[i]);
sqlUpdate.execute();
}
// NB: commit implicitly flushes the batch
Ebean.commitTransaction();
} finally {
Ebean.endTransaction();
}
CallableSql,
Ebean.execute(SqlUpdate),
Serialized Form| Constructor and Description |
|---|
DefaultSqlUpdate(EbeanServer server,
String sql)
Create with a specific server.
|
DefaultSqlUpdate(EbeanServer server,
String sql,
BindParams bindParams)
Create with server sql and bindParams object.
|
DefaultSqlUpdate(String sql)
Create with some sql.
|
| Modifier and Type | Method and Description |
|---|---|
SqlUpdate |
addParameter(Object value) |
int |
execute()
Execute the update returning the number of rows modified.
|
BindParams |
getBindParams()
Return the bind parameters.
|
String |
getLabel()
Return the label that can be seen in the transaction logs.
|
String |
getSql()
Return the sql statement.
|
int |
getTimeout()
Return the timeout used to execute this statement.
|
boolean |
isAutoTableMod()
Return true if eBean should automatically deduce the table modification
information and process it.
|
SqlUpdate |
setAutoTableMod(boolean isAutoTableMod)
Set this to false if you don't want eBean to automatically deduce the table
modification information and process it.
|
SqlUpdate |
setLabel(String label)
Set a descriptive text that can be put into the transaction log.
|
SqlUpdate |
setNull(int position,
int jdbcType)
Set a null parameter via its index position.
|
SqlUpdate |
setNull(String name,
int jdbcType)
Set a named parameter that has a null value.
|
SqlUpdate |
setNullParameter(int position,
int jdbcType)
Set a null valued parameter using its index position.
|
SqlUpdate |
setNullParameter(String name,
int jdbcType)
Set a named parameter that has a null value.
|
SqlUpdate |
setParameter(int position,
Object value)
Set a parameter via its index position.
|
SqlUpdate |
setParameter(String name,
Object param)
Set a named parameter value.
|
SqlUpdate |
setTimeout(int secs)
Set the timeout in seconds.
|
public DefaultSqlUpdate(EbeanServer server, String sql, BindParams bindParams)
Useful if you are building the sql and binding parameters at the same time.
public DefaultSqlUpdate(EbeanServer server, String sql)
public DefaultSqlUpdate(String sql)
public int execute()
SqlUpdate
After you have executed the SqlUpdate you can bind new variables using
SqlUpdate.setParameter(String, Object) etc and then execute the SqlUpdate
again.
For JDBC batch processing refer to
Transaction.setBatchMode(boolean) and
Transaction.setBatchSize(int).
execute in interface SqlUpdateEbean.execute(SqlUpdate)public boolean isAutoTableMod()
SqlUpdateIf this is true then cache invalidation and text index management are aware of the modification.
isAutoTableMod in interface SqlUpdatepublic SqlUpdate setAutoTableMod(boolean isAutoTableMod)
SqlUpdateSet this to false if you don't want any cache invalidation or text index management to occur. You may do this when say you update only one column and you know that it is not important for cached objects or text indexes.
setAutoTableMod in interface SqlUpdatepublic String getLabel()
SqlUpdatepublic SqlUpdate setLabel(String label)
SqlUpdateUseful when identifying the statement in the transaction log.
public String getSql()
SqlUpdatepublic int getTimeout()
SqlUpdategetTimeout in interface SqlUpdatepublic SqlUpdate setTimeout(int secs)
SqlUpdateThis will set the query timeout on the underlying PreparedStatement. If the timeout expires a SQLException will be throw and wrapped in a PersistenceException.
setTimeout in interface SqlUpdatepublic SqlUpdate setParameter(int position, Object value)
SqlUpdatesetParameter in interface SqlUpdatepublic SqlUpdate setNull(int position, int jdbcType)
SqlUpdateSqlUpdate.setNull(int, int).public SqlUpdate setNullParameter(int position, int jdbcType)
SqlUpdatesetNullParameter in interface SqlUpdatepublic SqlUpdate setParameter(String name, Object param)
SqlUpdatesetParameter in interface SqlUpdatepublic SqlUpdate setNull(String name, int jdbcType)
SqlUpdateSqlUpdate.setNullParameter(String, int).public SqlUpdate setNullParameter(String name, int jdbcType)
SqlUpdatesetNullParameter in interface SqlUpdatepublic BindParams getBindParams()
getBindParams in interface SpiSqlUpdateCopyright © 2014. All Rights Reserved.