HQL插入...使用参数选择

时间:2013-08-12 20:39:10

标签: hibernate jpa hql insert-select

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

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:选择类型的数量与插入的类型不匹配。

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

当你在select字段中放入一个非原始类型时,它会在所有属性中进行分解。所以你有这样的查询:

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

当您执行INSERT时,只放置实体的主键,因此您只能使用基本类型。

我希望,我很清楚;)祝你有愉快的一天

相关问题