设置方言会影响表的自动生成

<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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值