当使用@TableGenerator时,Hibernate会尝试两次创建相同的实体/表 - 如何避免它?

时间:2011-02-22 23:41:28

标签: hibernate entity javabeans

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

1 个答案:

答案 0 :(得分:2)

如果尚未解决 - 则不应将EDIS_SEQUENCE表映射到实体类。它由hibernate内部使用。