为什么这个HQL语句无效?

4
错误信息为: Error: 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列定义为自动递增。 - davek
是的,我的问题是:插入带有嵌入式ID的行。我一直在研究这个问题。也许HQL不支持。 - anonymous
1个回答

2

异常似乎是从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)是主键。 - anonymous

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接