I tested the Hibernate hbm2ddl.auto=update hibernate property. Actually, it creates the table if it's not finding the table in database but if i add one more field in my pojo class it doesn't alter the table. following are the config, mapping, pojo and test java file. My Pojo class,
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;
}
}
Config:
<?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>
mapping:
<?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>
Test class:
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();
}
}
If i delete the table and recreate then it creates the table with email id but if i drop email field only from table and then run again it doesn't alter the table with email field in Mysql database. please let me know what went wrong? and in eclipse console instead of alter it shows create table statement. ex: ERROR: HHH000388: Unsuccessful: create table hibernate.student (sid integer not null, name varchar(255), marks integer, email varchar(255), rank integer, primary key (sid))