为什么这个HQL无效?

时间:2011-06-02 21:27:18

标签: java sql hibernate hql

错误是: 错误: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;

提前致谢!

1 个答案:

答案 0 :(得分: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的子类型。你用的是什么发电机?