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.
|