开发者

Why this HQL is not valid?

开发者 https://www.devze.com 2023-03-09 22:18 出处:网络
The error is: Error: org.hibernate.QueryException: can only generate ids as part of bulk insert with either sequence or post-insert style generators

The error is: Error: org.hibernate.QueryException: can only generate ids as part of bulk insert with either sequence or post-insert style generators

HQL:

insert into CategoryProduct (category, p开发者_开发技巧roduct) 
select c, p from Category c, Product p 
where c.id = 252 and p.id = 554

The categoryProduct is a entity with embedded Id:

@EmbeddedId
protected CategoryProductPK categoryProductPK;

Thanks in advance!


The exception seems to be thrown from org.hibernate.hql.ast.HqlSqlWalker at:

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" );
}

and the decision is made at

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

So it seems that Hibernate expects you to be using a generator with is a sub-type of SequenceGenerator or PostInsertIdentifierGenerator. What generator are you using?

0

精彩评论

暂无评论...
验证码 换一张
取 消