Solve the problem that tables cannot be created correctly when hibernate is used to generate tables in Oracle databases.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.