Recently, we used hibernate to dynamically generate tables in the project. When we set hbm2ddl. auto to update, we found that hibernate did not generate the corresponding data table information according to the default generation rules. However, it is strange that some tables are not generated, and other tables are successfully generated. Restart the project and the problem persists. It is strange that although some tables are not generated, the associated joined tables are generated. In addition, an error indicating that the referenced table cannot be found will be reported during production. The following error is reported:
| 12 |
=2011-05-06 09:45:56 [org.hibernate.tool.hbm2ddl.SchemaUpdate]-[ERROR] Unsuccessful: alter table r_role_x_menu add constraint FK474DC862E1A553E2 foreign key (menu_id) references p_menu = 09:45:56 [org. hibernate. tool. hbm2ddl. SchemaUpdate]-[ERROR] ORA-00942: The table or view does not exist |
After searching for half a day, I finally found a problem, that is, the referenced table p_menu already exists in another user space. While hibernate was creating a table, this table is found in another user space, so it is no longer created in the current user space. This error occurs when you create an associated table because it is associated with a table in the current user space.
Hibernate uses the jdbc default databasemeta to find the corresponding table data information. When the default configuration is used, for some reason (not every time, it depends on the database itself and the corresponding driver ). When the current user connects to the database and uses databasemeta to find the database table information, the data table information of other users is queried (even if the current user does not have the corresponding permissions ).
To solve this problem, you only need to configure the following statement in hibernate. cfg. xml:
| 1 |
<property name="default_schema"> Current connected user </property> |
In this way, when databasemeta is used, it is mandatory to search for database information in the current user space, so that the table structure can be correctly created.