Showing posts with label spring. Show all posts
Showing posts with label spring. Show all posts

Friday, April 20, 2012

Running Native SQL deletes or updates via Hibernate

Keywords:
hibernate manual native SQL update delete bulkUpdate child table spring

Problem:
Probably not the best idea but for performance, limitations in hibernate mappings or errors in the objects you find it'd be much easier to just run some manual SQL. Can you do this via hibernate?

Solution:
Yes you can, hibernate call this "Native SQL". This is fairly well document, but I noticed its missing an example of doing a manual update/delete - where the result is not going to give you a list() of anything.

For Query you make via createSQLQuery(...), use executeUpdate() which returns the number of rows effected.

Here an example (using the CATS/DOGS table names from the hibernate document):
Query deleteQuery = session.createSQLQuery(
    "delete from CATS "
    + "where DOG_ID in ( "
        + "select d.ID from DOGS d "
        + "where d.STATUS = ?)");
deleteQuery.setString(0, "VICIOUS");
int updated = deleteQuery.executeUpdate();

If you're integrating with hibernate via spring, the same can be done via the template-callback mechanism (where your DAO extends org.springframework.orm.hibernate3.support.HibernateDaoSupport):
Integer deletedData = (Integer)getHibernateTemplate().execute(new HibernateCallback () {
    public Object doInHibernate(Session session) throws HibernateException, SQLException {
        // delete the data
        Query deleteQuery = session.createSQLQuery(
            "delete from CATS "
            + "where DOG_ID in ( "
                + "select d.ID from DOGS d "
                + "where d.STATUS = ?)");
        deleteQuery.setString(0, "VICIOUS");
        int updated = deleteQuery.executeUpdate();
    }
});
if (log.isDebugEnabled()) {
    log.debug("rows deleted from CATS: " + deletedData);
}

Notes:
If you're wondering if you can avoid hard coding the table references by asking hibernate for the table-name mapping, this does seem possible - Get the table name from the model in Hibernate - but this approach doesn't seem to be "public" so may disappear.


Wednesday, November 08, 2006

spring webflow 1.0 build fails

Keywords:
spring webflow 1.0 build sample

Problem:
Downloaded the latest spring webflow distribution (1.0) and trying to build (just a sample app. in this case) causes NoClassDefFoundError errors ... but surely this is shipped with the right libraries?

ivy.configure:
[echo] reading ivy config
[ivy:configure] :: Ivy 1.4 - 20061009124215 :: http://ivy.jayasoft.org/ ::

BUILD FAILED
java.lang.NoClassDefFoundError: org/apache/commons/httpclient/UsernamePasswordCredentials


Solution:
It's not enough to make sure you have Java 1.5+ and Ant 1.6+ ... these must also not have different versions of the dependent libraries.

In my case, there was an different version of apache commons-httpclient in the ant/lib folder ... removing this from the /lib folder or pointing at a 'clean' version of Ant resolves the issue.

Friday, November 03, 2006

java.io.FileNotFoundException spring-beans_2_0.dtd

Keywords:
java.io.FileNotFoundException spring-beans_2_0.dtd spring java 2.0 MVC

Problem:
Following the guide (such as the "Spring Framework MVC application step-by-step") and you get this error about the DTD reference in the -servlet.xml file.

Solution:
This is an easy one, the file seems to have been renamed ... the DTD is actually accessed from the web. Change the _2_0 to -2.0

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans_2_0.dtd">


<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">