3

我需要执行批量插入选择语句:

Query query = em
        .createQuery(
        "insert into EntityA (a,b,entity_field) select t.a, t.b, 
         :entity_field from EntityA t where ...");
query.setParameter("entity_field ", entity);

字段 entity_field 不是 EntityA 中的原始类型 我收到 java.lang.IllegalArgumentException:org.hibernate.QueryException:选择类型的数量与插入的类型不匹配。

有没有办法做到这一点?

4

1 回答 1

1

when you put in select field a not primitive type it's decomposite in all properties. So you have a query like this:

INSERT INTO (a, b, entity_field)
SELECT t.a, t.b, entity_field.field1, entity_field.field2,...., entity_field.fieldN

When you do an INSERT you put only the primary key of your entity, so you can use only primitive type.

I hope, I was clear ;) Have a nice day

于 2013-08-23T10:25:11.523 回答