当使用@TableGenerator时,Hibernate尝试两次创建相同的Entity / Table - 如何避免它? [英] Hibernate tries to create same Entity/Table twice when @TableGenerator is used - how to avoid it?
本文介绍了当使用@TableGenerator时,Hibernate尝试两次创建相同的Entity / Table - 如何避免它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi All,
I am using hibernate with my J2EE app deployed on JBoss 6.0.
My database is Oracle 11i and Derby.
For generating primary-key (running serial Id) I use @TableGenerator annotation
in one of my Entity (PersonDTO) - like:
@Entity
@Table(name = "EDIS_PERSON")
public class PersonDTO {
@Id
@TableGenerator(name="TABLE_GEN",
table="EDIS_SEQUENCE", pkColumnName="SEQ_NAME",
valueColumnName="SEQ_COUNT",
pkColumnValue="PERSON_ID", allocationSize = 5)
@GeneratedValue(strategy=GenerationType.TABLE, generator="TABLE_GEN")
private long id;
.
.
.
And I have defined the required Entity for sequence - like:
@Entity
@Table(name = "EDIS_SEQUENCE")
public class EdmSequenceDTO {
@Id
private String SEQ_NAME;
private long SEQ_COUNT;
.
.
.
Everything works fine as far as sequence-generation is concerned.
But during the creation of entitites (when very first time I deploy my J2EE app),
I see following error in JBoss server.log file, though all my entities/tables
get created successfully eventually.
14:04:16,817 ERROR [org.hibernate.tool.hbm2ddl.SchemaUpdate] Unsuccessful: create table EDIS_SEQUENCE ( SEQ_NAME varchar(255), SEQ_COUNT integer )
14:04:16,817 ERROR [org.hibernate.tool.hbm2ddl.SchemaUpdate] Table/View 'EDIS_SEQUENCE' already exists in Schema 'EDM'.
14:04:16,818 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] schema update complete**
I believe this is "not" actually an ERROR. It looks like since I have reference
of EDIS_SEQUENCE as part of 2 Entity beans, hibernate tries to create it twice.
Firstly, when the EdmSequenceDTO is deployed and secondly, when PersonDTO refers
the "EDIS_SEQUENCE" as part of @TableGenerator annotation.
In my persistence.xml file I have "hibernate.hbm2ddl.auto" property with
value="update".
I need to have this value instead of "create-drop".
I wonder is there any way I can avoid the error reported by hibernate?
Thanks & Regards,
- kuntal
推荐答案
如果尚未解决 - 您不应该将EDIS_SEQUENCE表映射到实体类。它由hibernate在内部使用。
If it is not yet solved - you should not map the EDIS_SEQUENCE table to an entity class. It is used internally by hibernate.
这篇关于当使用@TableGenerator时,Hibernate尝试两次创建相同的Entity / Table - 如何避免它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文