Resolving problems with using Hibernate to generate tables in an Oracle database does not create tables correctly

Source: Internet
Author: User

When you recently used Hibernate's dynamic build table in your project to configure Hbm2ddl.auto as update, you found that hibernate did not generate the corresponding data table information according to the default build rules. Strangely, however, only some of the tables were not generated, while the other tables were generated successfully. Reboot the project and find the problem remains. Oddly, although some tables are not generated, the associated tables that are associated with them are generated, and at build time, an error is reported that cannot find the associated reference table. The reported errors are as follows:

2014-09-28 01:25:56 [org.hibernate.tool.hbm2ddl.schemaupdate]-[error] unsuccessful:alter table R_role_x_menu add Constraint fk474dc862e1a553e2 foreign KEY (menu_id) references P_menu 2014-09-28 01:25:56 [ Org.hibernate.tool.hbm2ddl.schemaupdate]-[error] ORA-00942: Table or view does not exist

Looking for a long time, finally found a problem, that is, the table to be referenced here P_menu in another user space already exists, and hibernate in the creation of the table, in another user space to find the table, so no longer in the current user space to create this table. This error occurs when the associated table is created because it is associated with a table for this user space.
Hibernate uses the JDBC default Databasemeta to find the appropriate table data information, and when using the default configuration, for some reason (not every time it happens, depending on the database itself and the corresponding driver). When you use the current user to connect to a database, when you use Databasemeta to look for database table information, the data table information for other users is queried (even if the current user does not have the appropriate permissions).
The solution to this problem is simply to configure one sentence in Hibernate.cfg.xml:

<property name= "Default_schema" > Current connection User </property>

This way, when using Databasemeta, it is mandatory to look for database information in the current user space, so that the table structure can be created correctly.  

Related Article

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.