hibernate - hbm2ddl.auto is not working -
i have created 1 entity class emp, using hbm2ddl.auto create tables, below entity related table creating properly, before table in database more tables there, if use property hbm2ddl.auto create need drop total schema(total tables) , created entity related table freshly, old tables not deleted. tables there , new table created, not getting create functionality can please clarify doubt. used version 3.6.4.
emp.java
@entity @table(name="employe") public class emp { @id int empid; string ename; public int getempid() { return empid; } public void setempid(int empid) { this.empid = empid; } public string getename() { return ename; } public void setename(string ename) { this.ename = ename; } }
configuration.cfg.xml
<property name="connection.driver_class">com.mysql.jdbc.driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/sample</property> <property name="connection.username">root</property> <property name="connection.password">sun</property> <property name="connection.pool_size">1</property> <property name="dialect">org.hibernate.dialect.mysqldialect</property> <property name="cache.provider_class">org.hibernate.cache.nocacheprovider</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping class="in.hib.emp" />
my main program this
public class hibapplication { public static void main(string[] args) { emp e=new emp(); e.setempid(2); e.setename("pavai"); configuration cfg=new configuration().configure(); sessionfactory sf=cfg.buildsessionfactory(); session session=sf.opensession(); session.begintransaction(); session.save(e); session.gettransaction().commit(); } }
dear there 4 possible values can used hbm2ddl.auto 4 defined below
validate- checks table , columns existing in database or not if table not exist or column not exist exception thrown.
create - if value create hibernate drops table if exist , creates new table , executes operation, not used in real time old data lost database.
update - if values update hibernate uses existing table , if table not exist creates new table , executes operation. , used in real time , recommended.
create-drop - if value create-drop hibernate creates new table , after executing operation drops table, value used while testing hibernate code.
thanks njoy coding
Comments
Post a Comment