-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: a question about Hibernate 3
PostPosted: Mon Mar 21, 2005 1:53 pm 
Newbie

Joined: Mon Mar 21, 2005 1:46 pm
Posts: 2
in HIbernate 2.1, i use session.delete(String query) to do a mass deletion. When upgrade to 3.0, I found that function is gone. 3.0 only has session.delete(Object object). Does it mean I have to delete records one by one? No mass update on 3.0 or there is a better way to do mass deletion in 3.0?

Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 21, 2005 8:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
H2 that method was not a real builk update as it did it one by one based on the IDs collected.

H3 added Query.executeUpdate() for true bulk updates and delete.

Now you could use the classic API which the session is under org.hibernate.classic but the methods are deprecated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 12:03 pm 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
david wrote:
H2 that method was not a real builk update as it did it one by one based on the IDs collected.

H3 added Query.executeUpdate() for true bulk updates and delete.

Now you could use the classic API which the session is under org.hibernate.classic but the methods are deprecated.


That's all well and good, but what is the HQL syntax for a delete? It isn't documented anywhere.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 1:15 pm 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
Okay, after some trial and error I think I've figured it out. Pretty straight forward:

Code:
delete ClassName cn
where cn.something = ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 3:38 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Well after all that it just changed :)

You can actuall no longer use aliases with update or delete statements in HQL. The syntax is actually:
delete [from] ClassName where propName = ?
(notice no aliases).

This is now in CVS and will be in the 3.0 final release


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 11, 2005 9:22 am 
Newbie

Joined: Wed Mar 23, 2005 3:18 pm
Posts: 2
Location: Stockholm, SWEDEN
Steve, could you please explain the rationale behind the decision to remove aliases from HQL updates.

Not that I have a strong opinion against it, but, without having spent nights awake thinking about it, it seems that:

1) it would complicate/prevent joins in the update statement. If not, could you please provide an example to the contrary?

2) it deviates from both its native SQL counterpart and HQL selects, respectively.

Also, I use a composite PK class for an entity and couldn't use it in my statement as:

"update <EntityName> set <bla-bla-bla> where <compositePKClassInstanceName.attribute = :someValue".

Using the PK class attribute directly on the entity class however as follows:

"update <EntityName> set <bla-bla-bla> where attribute = :someValue".

, it worked.

This seems a bit unintuitive and non-consequential with respect to HQL selects. If working as designed, please explain the design decision.

Sincerely

/Par


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 11, 2005 3:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
1) it would complicate/prevent joins in the update statement. If not, could you please provide an example to the contrary?

Well, thats the whole point actually. Not all databases support joins (nor even aliased tables even) in UPDATE/DELETE statements.

Quote:
2) it deviates from both its native SQL counterpart

Huh? Very few databases actually allow joins in UPDATE or DELETE statements. And a good many complain if you even attempt to alias the table in a UPDATE or DELETE statement, as I mentioned above.

Quote:
Also, I use a composite PK class for an entity and couldn't use it in my statement as:

"update <EntityName> set <bla-bla-bla> where <compositePKClassInstanceName.attribute = :someValue".

Using the PK class attribute directly on the entity class however as follows:

"update <EntityName> set <bla-bla-bla> where attribute = :someValue".

, it worked.

Well, if true, that would be a bug.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 4:27 pm 
Newbie

Joined: Wed Mar 23, 2005 3:18 pm
Posts: 2
Location: Stockholm, SWEDEN
Quote:
Well, thats the whole point actually. Not all databases support joins (nor even aliased tables even) in UPDATE/DELETE statements.

Ok, I didn't know that. So if I really wanted to do a batch update with a join in Hibernate with a database that supports it, I guess I'm out of luck, since Hibernate does not support native SQL for update queries...? Perhaps not a big deal but good to know the options before resorting to JDBC or trying to code a workaround...

Also, I'll try to submit a formal bug report for the composite key example.

Thanks

/Par


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 14, 2005 5:44 pm 
Newbie

Joined: Thu Feb 17, 2005 2:04 pm
Posts: 4
Hi All,
I am not sure if iam helping in any way. but this is what I have noticed....

I could get a query string from the criterion.. This query string references the properties with its alias. Now if I use this to build the where condition for my query for deletion, I end up having the where clause having the alias where in the from clause is without the alias. I was wondering if there is a way of using the criteria for making dynamic queries.. I am trying this way of achieving this because there is a lot of idea/code reuse here and i would not want to reinvent what hibernate is already doing... (except for the little glitch ;) )

warm regards
Kiran


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.