anonymous
anonymous

Reputation: 81

Why this HQL is not valid?

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, product) 
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!

Upvotes: 4

Views: 2120

Answers (1)

Binil Thomas
Binil Thomas

Reputation: 13809

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?

Upvotes: 2

Related Questions