-->
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: Subquery insert for a composite key.
PostPosted: Thu Apr 05, 2007 3:28 pm 
Newbie

Joined: Tue Jun 20, 2006 10:20 pm
Posts: 6
I'm trying to do a subquery insert in HQL and haven't been able to figure out how to handle the following scenario.



I started with a many-to-many relationship between two objects with a mapping file in between, for examples sake lets say students and classes. Where each student will be a couple classes, but each class has a lot of students.

When a student submits his roster what i really want to be able to do is a query along the lines of

insert into student_class_map (student_id, class_id) select 1, c from where c.className in ('class x', 'class y')

I can't do this with native SQL since Hibernate doesn't support native inserts, and I was unable to find any way to do this type of query in HQL.

So since one seems to need the item i broke out and created a "roster" class changing my many-to-many relationship to a pair of many-to-one relationships between roster and student and roster and class.

My primary key is a RosterId item which contains the student and the class ids. Now I stuck since i've been unable to figure out how to get HQL to accept that composite ID as a type.

insert into roster (id)
select new com.mypkg.RosterId(1, c.id)
from Class c
where c.className in ('class x', 'class y')

fails because the sub-query select isn't returning a list of ids, it's returning an object array of 2 of the same item.

I've tried every possible permutation i could think of of this insert, but none of them have worked for me. Any help would be greatly appreciated.


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.