【问题标题】:Why this HQL is not valid?为什么这个 HQL 无效?
【发布时间】:2011-06-02 21:27:17
【问题描述】:

错误是: 错误:org.hibernate.QueryException:只能使用序列或插入后样式生成器生成 id 作为批量插入的一部分

HQL:

insert into CategoryProduct (category, product) 
select c, p from Category c, Product p 
where c.id = 252 and p.id = 554

categoryProduct 是一个嵌入了 Id 的实体:

@EmbeddedId
protected CategoryProductPK categoryProductPK;

提前致谢!

【问题讨论】:

  • 看起来您可能没有在数据库中将 PK 列定义为自动增量。
  • 是的,我的问题是:插入一行嵌入 id。我正在为此进行研究。可能不支持 HQL。

标签: java sql hibernate hql


【解决方案1】:

异常似乎是从org.hibernate.hql.ast.HqlSqlWalker 处抛出的:

IdentifierGenerator generator = persister.getIdentifierGenerator();
if ( !supportsIdGenWithBulkInsertion( generator ) ) {
    throw new QueryException( "can only generate ids as part of bulk insert with either sequence or post-insert style generators" );
}

决定是在

public static boolean supportsIdGenWithBulkInsertion(IdentifierGenerator generator) {
    return SequenceGenerator.class.isAssignableFrom( generator.getClass() )
        || PostInsertIdentifierGenerator.class.isAssignableFrom( generator.getClass() );
}

因此,Hibernate 似乎希望您使用的生成器是SequenceGeneratorPostInsertIdentifierGenerator 的子类型。你用的是什么生成器?

【讨论】:

  • 谢谢,我知道。 CategoryProducts 有一个复合主键。 2个字段(CategoryId、ProductId为主键)
猜你喜欢
  • 1970-01-01
  • 2016-07-27
  • 2012-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-26
  • 2019-03-26
相关资源
最近更新 更多