Hi,
I've also an type issue in an insert statement, but mine seems to be harder to solve.
I simplified the example as much as possible. Image the following 2 classes:
Type(int id, ...)
Obj(int id, Type type, ...)
What I want to do is to insert a new Obj with a certain value for "type". In SQL I can write
Code:
insert into Obj(type, ...) select 1, ...
in case that I know the value of type has to be 1.
In HQL this seems to be much harder. I cannot simply write
Code:
insert into Obj(type, ...) select 1, ...
without getting a org.hibernate.QueryException: insertion type [org.hibernate.type.ManyToOneType(Type)] and selection type [org.hibernate.type.IntegerType@314af9f7] at position 0 are not compatible [...]
I know that it's possible to cast int to long via "cast(1 as long)". However casting 1 to Type does not work. In this case I get a null pointer exception when I during the creation of the query.
The only workaround I found is to include the Type in the select statement:
Code:
insert into Obj(type, ...) select t, ... from ..., Type t where t.id = 1
But then I assume an unnecessary join is going to be constructed. For a better performance I would like to get rid of this join. Thus, is it somehow possible to work directly with the IDs of persistent objects instead of having to resolve the objects which leads to an unnecessary join?