Java Hibernate : 帮助解决异常 org.hibernate.HibernateException: Missing table

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18716204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:26:02  来源:igfitidea点击:

Hibernate : Help in resolving exception org.hibernate.HibernateException: Missing table

javahibernatehibernate-mapping

提问by Bourne

I am using Hibernate as ORM for my project. I use mysql Database
I have a table "Products" inside DB "catalog".
I have put the @Table(name="Products",schema="catalog") annotation for the entity Products in my application.

我在我的项目中使用 Hibernate 作为 ORM。我使用 mysql 数据库
我在数据库“目录”中有一个表“产品”。
我已经在我的应用程序中为实体产品添加了 @Table(name="Products",schema="catalog") 注释。

However when I try to run the application I get the below exception. Can you please help me resolve this issue?

但是,当我尝试运行该应用程序时,出现以下异常。你能帮我解决这个问题吗?

Exception:  
        Exception in thread "main" org.hibernate.HibernateException: Missing table:Products
        at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1281)
        at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:155)
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:508)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1769)
        at org.eros.purchase.db.utils.HibernateUtil.configure(HibernateUtil.java:17)
        at Test.main(Test.java:14)

Any thoughts on how I can fix this?

关于如何解决这个问题的任何想法?

回答by Nirbhay Mishra

I think your mapping is referring to Usertable which is actually not in database. . so check your xml mapping of hibernate

我认为您的映射是指User实际上不在数据库中的表。. 所以检查你的休眠的xml映射

回答by Deepak

package com.mypackage;
    import javax.persistence.Entity;//first check two annotations belong to right package
    import javax.persistence.Table;
    @Entity
    @Table(name = "Products",schema="catalog")
    public class Products{

    //entity attributes
    }

//second check mapping file (if use)i.e register Products class with right spelling // or third check Hibernate util if we are register mapping class like this

//第二次检查映射文件(如果使用)即注册具有正确拼写的产品类//或第三次检查Hibernate util,如果我们像这样注册映射类

public class CustomHibernateUtil{


private static Configuration getConfiguration(){

configuration.addAnnotatedClass(Products.class);
return configuration;

}
}

回答by Deepak

Please update your hibernate.cfg.xml file by adding this property

请通过添加此属性来更新您的 hibernate.cfg.xml 文件

<property name="hibernate.hbm2ddl.auto">create</property>
or 
<property name="hibernate.hbm2ddl.auto">update</property>

回答by K. Suter

I got the same exception from hibernate. In my case, it was because the database user was not properly authorized to see the tables. The tables were definitively there on the database.

我从休眠中得到了同样的异常。就我而言,这是因为数据库用户没有被正确授权查看表。这些表肯定存在于数据库中。

After I assigned the roles db_datareaderto the user for this database it worked.

在我将角色db_datareader分配给该数据库的用户后,它就可以工作了。

However, in another case, where the tables weren't actually there, I got exactly the same exception from hibernate. I cases where the tables are there, I think hibernate might show no more information because of security reasons.

然而,在另一种情况下,表实际上并不存在,我从休眠中得到了完全相同的异常。如果表在那里,我认为由于安全原因,休眠可能不会显示更多信息。