Hi guys,
I am having problems while trying to do a bulk delete.
I notice that the syntax has changed for hibernate 3.1 (table aliases cannot be specified) and that it has not yet been updated in the documentation - it would be great if this was updated as it took me a while to figure out :) (I'm happy to do so if required!)
Anyway, I have found that when trying to specify a composite key value it's not doing the funky "translate property name to SQL column name" that I have come to love so much. Eg;
(Note that AdPointContent has a composite key, with iblId being one of these)
Code:
Query query = session.createQuery( "delete from AdPointContent where id.iblId = :iblid and statusId = :statusid" );
query.setInteger("iblid", 1 );
query.setInteger("statusid", 2 );
query.executeUpdate();
and it's generating the following SQL:
Code:
delete from YPOL_AD_POINT_CONTENT_BSTORE where adpointcon0_.IBL_ID=? and STATUS=?
which is obviously a bit wrong (it's generating a table alias when it doesn't need to - looks like someone only ripped out most of that code but missed the composite key stuff).
To workaround it I have found that you can just specify the table's column name in your HQL:
Code:
Query query = session.createQuery( "delete from AdPointContent where ibl_Id = :iblid and statusId = :statusid" );
query.setInteger("iblid", 1 );
query.setInteger("statusid", 2 );
query.executeUpdate();
which of course defeats the whole point of using HQL (might as well just write out the SQL), but at least it generates the right SQL:
Code:
delete from YPOL_AD_POINT_CONTENT_BSTORE where ibl_Id=? and STATUS=?
Waahey. Notice that the second parameter _is_ being translated correctly. (Yes, I did put it in there for a reason :) My only gripe with this solution is that it is very confusing for the average user who might stumble across my code (why is one column name specified and the other is the class property?). Of course it's a great way to confuse people so I guess that's a bonus.
Am I doing something stupid? Is there a better way to do this?