开发者

Hibernate tries to create same Entity/Table twice when @TableGenerator is used - how to avoid it?

开发者 https://www.devze.com 2023-02-13 00:05 出处:网络
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

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 entities (when the very first time I deploy my J2EE app), I see the 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 a reference to 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 to 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 c开发者_开发知识库an avoid the error reported by 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.

0

精彩评论

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