【问题标题】:Hibernate : Help in resolving exception org.hibernate.HibernateException: Missing tableHibernate : 帮助解决异常 org.hibernate.HibernateException: Missing table
【发布时间】:2013-09-14 00:04:08
【问题描述】:

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

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

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)

有什么想法可以解决这个问题吗?

【问题讨论】:

  • 识别的缺失表不是产品而是用户。看看那个映射和表格。
  • 异常是在谈论不同的缺失表 - 用户
  • 检查您作为用户映射的映射类,该类在数据库中不存在
  • 我更正了描述。实际上我正在寻找“产品”本身。
  • 删除架构试试

标签: java hibernate hibernate-mapping


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 我的映射正在寻找“产品”本身。
      • 我已经修正了问题的描述。
      【解决方案4】:
      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
          }
      

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

      public class CustomHibernateUtil{
      
      
      private static Configuration getConfiguration(){
      
      configuration.addAnnotatedClass(Products.class);
      return configuration;
      
      }
      }
      

      【讨论】:

      • 我已经在“hibernate.cfg.xml”中指定了映射。下面是“hibernate.cfg.xml”文件中的映射标签
      猜你喜欢
      • 2016-03-31
      • 2011-07-21
      • 2018-10-01
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      相关资源
      最近更新 更多