我在项目中使用Hibernate作为ORM。我使用mysql数据库
我在DB“目录”中有一个“产品”表。
我在我的应用程序中为实体产品添加了@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)
关于我如何解决这个问题的任何想法?
答案 0 :(得分:3)
请通过添加此属性来更新您的hibernate.cfg.xml文件
<property name="hibernate.hbm2ddl.auto">create</property>
or
<property name="hibernate.hbm2ddl.auto">update</property>
答案 1 :(得分:0)
我认为你的映射是指User
表,它实际上不在数据库中。 。
所以检查你的hibernate的xml映射
答案 2 :(得分:0)
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 utilpublic class CustomHibernateUtil{
private static Configuration getConfiguration(){
configuration.addAnnotatedClass(Products.class);
return configuration;
}
}
答案 3 :(得分:0)
我从hibernate得到了同样的异常。在我的情况下,这是因为没有正确授权数据库用户查看表。这些表最终存在于数据库中。
我将角色 db_datareader 分配给该数据库的用户后,它就可以了。
然而,在另一种情况下,表实际上并不存在,我从hibernate获得了完全相同的异常。我遇到表格的情况,我认为由于安全原因,hibernate可能不会显示更多信息。