利用hibernate自动创建数据库

首先hibernate配置文件,hibernate.cfg.xml。

<!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="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	    <!-- 连接数据库的驱动名 -->
	    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
	    <!-- 连接数据库的URL -->
	    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/helloHibernate</property>
	    <!-- 连接数据库的用户名 -->
	    <property name="hibernate.connection.username">root</property>
	    <!-- 连接数据库的密码 -->
	    <property name="hibernate.connection.password">123456</property>
        <!-- 在控制台显示SQL语句 -->
		<property name="show_sql">true</property>
		<!-- 自动建表 -->
		<property name="hibernaete.hbm2ddl.auto">update</property>
		<mapping resource="com/domainModel/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

然后就建立一个实体类

package com.domainModel;

public class User {
	private int id;
	private String username;
	private String password;
	private int age;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

然后配置实体类的映射文件。

<?xml version="1.0"?>

<!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="com.domainModel.User" table="Test_USER">
		<id name="id" column="id">
			<generator class="identity" />
		</id>
             <!--设置主键-->
		<property name="username" column="name" />
		<property name="password" column="password"/>
		<property name="age" column="age"/>
	</class>

</hibernate-mapping>
然后写测试类
package com.DAO;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.domainModel.User;

public class testSaveUser {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Configuration cfg = null;
		SessionFactory sf = null;
		
		Session s = null;
		Transaction ts = null;
		User user = new User();
		user.setUsername("老范");
		user.setPassword("laofan");
		user.setAge(21);
		try {
			cfg = new Configuration().configure();
			sf = cfg.buildSessionFactory();
			
			s = sf.openSession();
			ts = s.beginTransaction();
			s.save(user);
			ts.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			if(ts != null)
			{
				ts.rollback();
			}
			e.printStackTrace();
		} finally{
			if(s != null){
				s.close();
			}
		}
	}

}

运行结果



我在运行当中遇到几个问题:

1.注意数据库方言,其中包括版本号问题(MySQL5.7以上需要在"MySQL"后面加个"5")。而且,报错说明你的数据库没有建立这个表导致你的程序并没有成功,可以把数据库方言换成"org.hibernate.dialect.MySQLialect"(也就是说用MySQLIonnoDB智能在数据库不能自动建表);

2.关于hibernate.hbm2ddl.auto参数的问题

create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。

create-drop :每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。

update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。

validate :每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值