-->
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.  [ 11 posts ] 
Author Message
 Post subject: Is it possible to parametrize named sql queries?
PostPosted: Fri Jan 13, 2006 9:29 am 
Newbie

Joined: Tue Jul 26, 2005 6:26 am
Posts: 15
I have to make changes to a table which is not mapped. In order to centralize sql statements, I want to use named sql queries.

I know how to get values from them, but is there also a possiblity to include a parameter in the query and assign a value to it upon creation of the query?


Thanks in advance.
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 9:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes, just do it like any other query.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 9:52 am 
Newbie

Joined: Tue Jul 26, 2005 6:26 am
Posts: 15
Could you give me an example or a hint where to look in the documentation?
I didn't find something which helps me.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 10:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
The very first example includes a parameter and and example code.

http://www.hibernate.org/hib_docs/v3/re ... medqueries

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 14, 2006 6:12 am 
Newbie

Joined: Tue Jul 26, 2005 6:26 am
Posts: 15
Thanks Max, now I see the example ;)

Unfortunately, it seems I have to go another way.

I wanted to execute an insert or update statement on a table which isn't mapped on a class, so I use a native query. But it seems that I can only execute SELECTs because I can only use methods like uniqueResult() etc. which require a specified return parameter (which I of course don't have if I'm updating or inserting).


My situation is the following:

I have multiple objects of type Plan. Exactly one plan has to be active, so I made a table which contains the id of the active plan. Every Plan has a active-property which gets its value from a formula which compares the id of the plan with the active one.

But formulars are read-only, so I need a mechanism to set this value, too.

Or is there a possibility to do this built-in in hibernate?
Creating a class which only contains a reference on the active plan doesn't sound like a good idea to me.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 14, 2006 6:52 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you can map the same column multiple times, but only one of them can be updatable...dont know if that helps you.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 14, 2006 9:53 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
There is a method executeUpdate at least with Hibernate 3.
In the reference have a look at the native SQL.

Just an idea, but couldn't you use a trigger.
insert new plan => trigger sets other plan inactive


Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 14, 2006 1:09 pm 
Newbie

Joined: Tue Jul 26, 2005 6:26 am
Posts: 15
executeOrUpdate() only works with HQL queries -. I get an java.lang.UnsupportedOperationException.

A trigger is also not an option because the table doesn't have a column for pubslihed so the trigger wozld never knwo if the inserted or updated plan has to be activated or not.


I'll try to access the JDBC connection directly and send the statement there.
That would be quite a dirty solution, but it should work.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 14, 2006 1:16 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Which Hibernate version do you use?

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 14, 2006 1:25 pm 
Newbie

Joined: Tue Jul 26, 2005 6:26 am
Posts: 15
I use 3.0.5.

Consider the following example:

Code:
SQLQuery q = session.createSQLQuery("UPDATE active_plan SET active=:active").addScalar("active", Hibernate.INTEGER);
q.executeUpdate();


I get the following:

Code:
Exception in thread "main" java.lang.UnsupportedOperationException: Update queries only supported through HQL


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 14, 2006 1:34 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Sorry, I thought that would work. But I tested it with 3.1 and it is the same.

In this case you may consider to fetch the connection from the session and make a direct JDBC connection, as you propose
session.getConnection

or you may try a stored procedure with a parameter as explained in the reference.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.