-->
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.  [ 1 post ] 
Author Message
 Post subject: insert select new query in hql
PostPosted: Tue Aug 19, 2008 11:33 am 
Newbie

Joined: Tue Aug 19, 2008 10:44 am
Posts: 1
Hoi guys,

I try to make the description of my problem as short as possible:

In sql I have:

insert into table_A
(
select property_1, property_2, property_3 from table_B where condition
)

This runs perfectly.
property_n are fields of table_A.

Let's say in hql table_A is represented by object_A and table_B by object_B.
object_A has a composite key made out of property_1 and property_2.

When I want to insert my select result into objec_A, I would like to modify property_2.

My first attempt just for the id of object_A by the select query is:

select new object_id_of_A(object_B.property_1, 'myConstantString') from object_B.

This is ok. When I want to complete the list of properties, like:

select new object_id_of_A(object_B.property_1, 'myConstantString'), object_B.property_3 as myProperty3 from object_B

I get an error (I use Hibernate Perspective):
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: , ...

Digging a little bit further, I found in sql-gen.g the following:

selectClause!
: #(SELECT { handleClauseStart( SELECT ); beforeSelectClause(); } (d:DISTINCT)? x:selectExprList ) {
#selectClause = #([SELECT_CLAUSE,"{select clause}"], #d, #x);
}
;

selectExprList {
boolean oldInSelect = inSelect;
inSelect = true;
}
: ( selectExpr | aliasedSelectExpr )+ {
inSelect = oldInSelect;
}
;

aliasedSelectExpr!
: #(AS se:selectExpr i:identifier) {
setAlias(#se,#i);
#aliasedSelectExpr = #se;
}
;

selectExpr
: p:propertyRef { resolveSelectExpression(#p); }
| #(ALL ar2:aliasRef) { resolveSelectExpression(#ar2); #selectExpr = #ar2; }
| #(OBJECT ar3:aliasRef) { resolveSelectExpression(#ar3); #selectExpr = #ar3; }
| con:constructor { processConstructor(#con); }
| functionCall
| count
| collectionFunction // elements() or indices()
| literal
| arithmeticExpr
| query
;



What I retain from here is that I can either use a con:constructor (selectExpr) OR an aliasedSelectExpr, what means that the combination of both, i.e.

select new obj(), property_3 from table

is IMPOSSIBLE. Is it right? If the answer is yes, is there a workaround (with imbricated inserts or flying keywords in a 4-D screen with adjustable time parameter...)?

Thanks in advance.

à+

Fredy


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

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.