我有一个基本的Hibernate代码,我已将属性“hibernate.hbm2ddl.auto”设置为更新仍然不会自动创建数据库中的表。
这些是必需的文件:
employee.hbm.xml
<hibernate-mapping>
<class name="contacts.employee" table="contacts">
<meta attribute="class-description"></meta>
<id column="contactId" name="contactId" type="string">
<generator class="assigned"/>
</id>
<property column="contactName" length="100" name="contactName" not-null="true" type="string"/>
<property column="password" length="100" name="password" not-null="true" type="string"/>
<set cascade="all" name="groupOfResponsibilities" table="employee_responsibilty">
<key column="contactId"/>
<many-to-many class="contacts.responsibilities" column="responsibilityId"/>
</set>
</class>
</hibernate-mapping>
responsibility.hbm.xml
<hibernate-mapping>
<class name="contacts.responsibilities" table="responsibilities">
<meta attribute="class-description">
This class list of responsibilities if an employee
</meta>
<id column="responsibilityId" name="responsibilityId" type="long">
<generator class="increment"/>
</id>
<property column="responsibilityName" name="responsibilityName" type="string"/>
</class>
</hibernate-mapping>
的hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/****</property>
<property name="hibernate.connection.username">*****</property>
<property name="hibernate.connection.password">*****</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="contacts/employee.hbm.xml"/>
<mapping resource="contacts/responsibilitiy.hbm.xml"/>
</session-factory>
</hibernate-configuration>
这是我试图运行的Main.java:
public class Main {
public static void main(String[] args) {
SessionFactory sessionfactory = NewHibernateUtil.getSessionFactory();
Transaction transaction = null;
try {
Session session = sessionfactory.openSession();
transaction = session.beginTransaction();
Set<responsibilities> groups = new HashSet<responsibilities>();
responsibilities responsibilityOne=new responsibilities("Java");
responsibilities responsibilityTwo=new responsibilities("SQL");
responsibilities responsibilityThree=new responsibilities("Oracle");
groups.add(responsibilityOne);
groups.add(responsibilityTwo);
groups.add(responsibilityThree);
String uuid = UUID.randomUUID().toString();
String uuid2 = UUID.randomUUID().toString();
employee firstEmployee;
firstEmployee = new employee(uuid, "Mike", groups);
employee secondEmployee = new employee(uuid2, "Marc", groups);
session.save(responsibilityOne);
session.save(responsibilityTwo);
session.save(responsibilityThree);
session.save(firstEmployee);
session.save(secondEmployee);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
}
}
}
这是我得到的错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'**。的职责'不存在
我有同样的问题,这对我有用 -
<property name="hibernate.hbm2ddl.auto">create</property>
以下情况可能是Hibernate无法自动创建表的另一个原因:
@Entity
public class Employee {
@Id
@GeneratedValue
private String empID;
private String name;
}
表Employee
将不会由Hibernate自动创建,因为empID
是String
并且它具有@GeneratedValue
注释。 empID
应该是int
或long
。我有时遇到这个问题,我改变了我的id
字段,其中@GeneratedValue
注释为int
类型,Hibernate自动创建表。
我有同样的问题,我通过改变解决了它:
org.hibernate.dialect.MySQLInnoDBDialect
至
org.hibernate.dialect.MySQLDialect
我在this question找到了解决方案。
为什么不尝试创建或创建drop。似乎是为了你的目的。 http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-optional
$mysql --version $mysql Ver 14.14 Distrib 5.5.11
检查您的MySQL版本然后相应地改变方言
MySQL55Dialect/ org.hibernate.dialect.MySQL55Dialect
//////////////////////////////////////////////// - 你在哪里可以找到方言:
org.hibernate.dialect
try
import org.hibernate.dialect.ctrl+space
you find option
希望这个帮助
我有同样的问题,但对我来说解决方案是:
<property name="hibernate.hbm2ddl.auto">create-drop</property>
代替
<property name="hibernate.hbm2ddl">create-drop</property>
Hibernate可以代表您创建用于多对多映射的表,hibernate序列和表,但是您必须通过调用Configuration对象的setProperty(“hibernate.hbm2ddl.auto”,“create”)来显式配置它。默认情况下,它只是使用DB验证模式,如果通过提供错误“ORA-00942:表或视图不存在”而存在任何不存在的情况则会失败。
对于现有的工作环境或任何大的应用领域,不建议设置hibernate.hbm2ddl.autoon create或create-drop,如下所示
<property name="hibernate.hbm2ddl.auto">create-drop</property>
虽然它可以工作,但每次重新启动服务器时它都会丢弃数据库的所有现有表。
如果您使用的是hibernate jar版本5.2.0,请切换到较低版本或任何其他版本。此版本jar有一些关于更新的问题。
它对我有用,我希望它也适用于其他人。
只是做正确的配置
1)每次创建新表时创建。所有旧数据都将被删除。
<property name="hibernate.hbm2ddl.auto">create</property>
2)更新,如果进行了任何修改,则同样会反映到DB。
<property name="hibernate.hbm2ddl.auto">update</property>