Nic*_*Div 21 hibernate hibernate-mapping
我有一个基本的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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
的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>
Run Code Online (Sandbox Code Playgroud)
这是我试图运行的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 {
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表' **.responsibilities'不存在
Sac*_*ngh 19
我有同样的问题,这对我有用 -
<property name="hibernate.hbm2ddl.auto">create</property>
Run Code Online (Sandbox Code Playgroud)
Kih*_*ats 16
以下情况可能是Hibernate无法自动创建表的另一个原因:
@Entity
public class Employee {
@Id
@GeneratedValue
private String empID;
private String name;
}
Run Code Online (Sandbox Code Playgroud)
该表Employee不会由Hibernate自动创建,因为它empID是a String并且它具有@GeneratedValue注释.empID应该是一个int或long.我有时遇到这个问题而且我改变了我的id字段,其中@GeneratedValue注释是类型的int,Hibernate自动创建了表.
| 归档时间: |
|
| 查看次数: |
43958 次 |
| 最近记录: |