<session-factory>
<property name="hbm2ddl.auto">create-drop</property>
<!--
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
-->
<property name="connection.url">jdbc:mysql://localhost/bobo</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
package com.javatpoint.mypackage;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class StoreData {
public static void main(String[] args) {
//creating configuration object
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file
//new SchemaExport(cfg.configure("hibernate.cfg.xml")).create(true,true);
//creating seession factory object
SessionFactory factory=cfg.buildSessionFactory();
//creating session object
Session session=factory.openSession();
//creating transaction object
Transaction t=session.beginTransaction();
Employee e1=new Employee();
e1.setId(116);
e1.setFirstName("刘");
e1.setLastName("利新");
session.persist(e1);//persisting the object
t.commit();//transaction is commited
session.close();
System.out.println("successfully saved");
}
}
如果用配置文件中的第4行和主程序中的第15行,程序会出现错误:ERROR: Table 'bobo.emp1000' doesn't exist
Hibernate给出的见表语句是:
create table emp1000 (
id integer not null,
firstName varchar(255),
lastName varchar(255),
primary key (id)
) type=InnoDB
拿这个当成sql脚本,在mysql>下都建不成表。不知道犯什么忌了,把type=InnoDB去掉就行,因此,我想到了去掉配置中的方言。但是,这样做出来又是中文乱码,看来要加那种utf-8的方言。
源代码:http://pan.baidu.com/share/link?shareid=3183233073&uk=3878681452
另见:http://188029.net/java/j12-03-15.html