2011-06-02 76 views
3

错误是: 错误:org.hibernate.QueryException:仅可作为散装的一部分与任一序列或嵌交式发电机插入产生IDS为什么这个HQL无效?

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; 

在此先感谢!

+0

看起来您可能没有在数据库中定义为自动增量的PK列。 – davek 2011-06-03 07:09:24

+0

是的,我的问题是:插入一个嵌入式ID行。我正在为此进行研究。也许HQL不支持。 – anonymous 2011-06-03 15:16:30

回答

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。你使用什么发电机?

+0

谢谢,我知道。 CategoryProducts具有复合主键。 2个字段(CategoryId,ProductId是主键) – anonymous 2011-06-09 15:52:58