我测试了Hibernate hbm2ddl.auto = update hibernate属性。实际上,如果它没有在数据库中找到表,它会创建表,但如果我在我的pojo类中添加一个字段,它不会改变表。以下是config,mapping,pojo和test java文件。 我的Pojo课程,
package beans;
public class Student {
private int sid;
private String name;
private int marks;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Student() {
// TODO Auto-generated constructor stub
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
}
配置:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/</property>
<property name="connection.username">xxx</property>
<property name="connection.password">xxxx</property>
<property name="connection.pool_size">10</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping resource="resources/student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
映射:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="beans.Student" table="student" schema="hibernate">
<id name="sid" ></id>
<property name="name" />
<property name="marks" />
<property name="email" />
</class>
</hibernate-mapping>
测试类:
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Client {
public static void main(String[] args) {
// Student object state is transiant
Configuration cfg=new Configuration();
cfg.configure("resources/hibernate.cfg.xml");
SessionFactory sf= cfg.buildSessionFactory();
}
}
如果我删除了表并重新创建然后它创建了具有电子邮件ID的表,但如果我只从表中删除电子邮件字段然后再次运行它不会改变Mysql数据库中的电子邮件字段表。请让我知道出了什么问题?并在eclipse控制台而不是改变它显示创建表语句。例如:ERROR: HHH000388: Unsuccessful: create table hibernate.student (sid integer not null, name varchar(255), marks integer, email varchar(255), rank integer, primary key (sid))