Hibernate不会自动创建数据库中不存在的表 [英] Hibernate is not auto-creating a table that does not exist in the DB
问题描述
我有一个基本的Hibernate代码,
我已经将属性设置为hibernate.hbm2ddl.auto作为更新,但它不会自动创建数据库中的表。
这些是必需的文件:
employee.hbm.xml
< hibernate-mapping>
< class name =contacts.employeetable =contacts>
< meta attribute =class-description>< / meta>
< id column =contactIdname =contactIdtype =string>
< generator class =assigned/>
< / id>
< property column =contactNamelength =100name =contactNamenot-null =truetype =string/>
< property column =passwordlength =100name =passwordnot-null =truetype =string/>
< key column =contactId/>
< / set>
< / class>
< / hibernate-mapping> b
$ b p> <休眠映射>
< class name =contacts.responsibilitiestable =responsibilities>
< meta attribute =class-description>
如果雇员为
< / meta>
< generator class =increment/>
< / id>
< / class>
< / hibernate-mapping>
hibernate.cfg.xml
<冬眠-结构>
< 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>更新< / 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();
交易交易=空;
尝试{
Session session = sessionfactory.openSession();
transaction = session.beginTransaction();
设定<责任> groups = new HashSet< responsibilities>();
责任responsibilityOne =新职责(Java);
责任responsibilityTwo =新职责(SQL);
责任responsibilityThree =新职责(Oracle);
groups.add(responsibility);
groups.add(responsibility);
groups.add(responsibility);
String uuid = UUID.randomUUID()。toString();
字符串uuid2 = UUID.randomUUID()。toString();
雇员firstEmployee;
firstEmployee =新员工(uuid,Mike,groups);
员工secondEmployee =新员工(uuid2,Marc,groups);
session.save(responsibility);
session.save(responsibility);
session.save(responsibility);
session.save(firstEmployee);
session.save(secondEmployee);
transaction.commit();
} catch(HibernateException e){
transaction.rollback();
e.printStackTrace();
}终于{b
$ b}
}
}
这是我得到的错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:Table' * *。职责'不存在
我有同样的问题, p>
< property name =hibernate.hbm2ddl.auto> create< / property>
I have a basic Hibernate code, I have set the property "hibernate.hbm2ddl.auto" as update still it is not auto-creating the table in the Database.
These are the required files:
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>
This is the Main.java that I am trying to run:
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 {
}
}
}
This is the error that I get:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table '**.responsibilities' doesn't exist
I had the same issue, this worked for me -
<property name="hibernate.hbm2ddl.auto">create</property>
这篇关于Hibernate不会自动创建数据库中不存在的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!