简体   繁体   中英

Unsuccessful: alter table add constraint: Table doesn't exist on create

I have strange behavior with OneToMany:

Entity1:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "entity1")
private Set<Deal> deals;

Deal:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "entity1_id")
private Entity1 entity1;

Also I have:

properties.setProperty("hibernate.hbm2ddl.auto", "create");

It gives following error upon start:

ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table deal add constraint FK_d17qqf8ra0ibdsnngbp465qvs foreign key (entity1_id) references entity1 (entity1_id)
ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - Table 'test.deal' doesn't exist

I tried drop all tables from db, all tables are created successfully (I have this type of relationship in other entities, those tables are also created) except of this one. What am I doing wrong? Thanks.

Finally I found what the problem was. To name my fields I used MySQL key words: to, from, show. So the SQL create request generated by Hibernate was complete gibberish for MySQL, and the only sign was the message from the server that the syntax was wrong. I found it by deleting fields each after other and restarting, step by step.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM