【问题标题】:HQL insert ... select with parametersHQL 插入 ... 带参数选择
【发布时间】:2013-08-12 20:39:09
【问题描述】:

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

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: number of select types does not match those for insert.

有什么办法吗?

【问题讨论】:

  • 不,它适用于原始类型。

标签: hibernate jpa hql insert-select


【解决方案1】:

当您在选择字段中放入非原始类型时,它会在所有属性中分解。所以你有这样的查询:

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

当您执行 INSERT 时,您只输入实体的主键,因此您只能使用原始类型。

我希望,我很清楚;)祝你有美好的一天

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多