Hibernate的简单例子

 

前言:

    我们知道java是面向对象的语言,而我们现在使用的数据库是关系型数据库,所以当用java这种面向对象的程序语言去操作关系型数据库的时候,就会出现不匹配的情况,也就是不能把一个对象很好的保存到关系型数据库中,这种问题叫做阻抗不匹配,关系模型和对象模型之间不能够对应起来,所以,我们要做是对这两种模型进行转换.

JDBC,查找方法,如下面这段代码:

读取数据库信息

    查找用户的方法,就是从数据库里读一条记录出来.我们从结果集ResultSet中把数据读出来然后赋给user对象,这个过程相当于把关系模型转化成了对象模型.

插入方法,如下面这段代码:

 

    插入用户的方法,就是将一条记录插入到数据库中.我们user对象保存到关系型数据库中,可以看出我们将对象模型转换成关系型模型.

jdbc向数据库插入信息

正文开始:

第一步:创建一个普通的java project   hibernate

第二步:创建一个实体对象(User),如下代码:

package cn.ljq.hibernate.domain;

 

import java.util.Date;

 

public class User {

    private int id;

    private String name;

    private Date birthday;

    public int getId() {

       return id;

    }

    public void setId(int id) {

       this.id = id;

    }

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public Date getBirthday() {

       return birthday;

    }

    public void setBirthday(Date birthday) {

       this.birthday = birthday;

    }

    @Override

    public String toString() {

       return "ID: "+ id + "  Name: "+ name +"  Birthd: "+ birthday;

    }

}

--------------------------------------------------

第三步:导入hibernate依赖的jar包和hibernate3.jar(真正来完成映射的包)

--------------------------------------------------

第四步:先做映射文件

    其作用是将对象模型跟关系模型衔接起来,也就是将user对象跟数据库表对应起来.

    步骤:创建一个User.hbm.xml文件,其代码如下:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.ljq.hibernate.domain">

   <class name="User">

     <id name="id">

       <generator class="native"/>

     </id>

    

     <property name="name"/>

     <property name="birthday"/>

   </class>

</hibernate-mapping>

--------------------------------------------------

第五步:创建hibern的配置文件hibernate.cfg.xml

代码如下:

<?xml version="1.0"?>

<!DOCTYPE hibernate-configuration PUBLIC

    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 

<hibernate-configuration>

    <session-factory>

       <property name="show_sql">true</property>

       <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

       <property name="connection.url">jdbc:mysql://127.0.0.1:3306/test_1</property>

       <property name="connection.username">root</property>

       <property name="connection.password">123456</property>

      

       <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

       <property name="hbm2ddl.auto">create</property>

       <mapping resource="cn/ljq/hibernate/domain/User.hbm.xml"/>

      

    </session-factory>

</hibernate-configuration>

---------------------------------------

第六步:创建一个Test测试类:

代码如下:

package cn.ljq.hibernate;

import java.util.Date;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import cn.ljq.hibernate.domain.User;

public class Test {

    public static void main(String[] args){

       //初始化

       Configuration cfg = new Configuration();

       cfg.configure();

       SessionFactory sf = cfg.buildSessionFactory();

      

       Session s = sf.openSession();

       User user = new User();

       user.setBirthday(new Date());

       user.setName("name");

      

       s.save(user);

       s.close();

       System.out.println(user);

       System.out.println("end"); 

    }

}

 

到此,一个简单的Hibernate连接数据的程序就完成了.

--------------------------------------------------

下面是经常出现的2个异常的解释:

异常一:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: cn.ljq.hibernate.domain.User

解释:该异常原因是没有找映射文件

解决:Hibernate配置文件中加入此代码: 注意,映射文件路径根据当前项目而定

<mapping resource="cn/ljq/hibernate/domain/User.hbm.xml"/>

 

异常二:

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [cn.ljq.hibernate.domain.User]

Caused by: java.sql.SQLException: Table 'test_1.user' doesn't exist

解释:该异常原因是没有找到数据库表

解决:两种方式:

第一种:到数据库创建User

第二种:Hibernate配置文件中加入此代码:这是自动建表的意思

    <property name="hbm2ddl.auto">create</property>

    其中有4种方式:均经过本人实际测试,事实说话(^_^)

    create-drop: 根据model类生成表,但是sessionFactory一关闭,表就自动删除,

            ,一定是关闭sessionFactory,只关闭session是不会删除表的

    create: 会根据你的model类来生成表,但是每次运行都会删除上一次的表,重新生成

            ,哪怕2次没有任何改变

    update: 最常用的属性,也根据model类生成表,即使表结构改变了,表中的行仍然存

            ,不会删除以前的行

    validate: 只会和数据库中的表进行比较,不会创建新表,但是会插入新值,正式运行

           时用这种方式比较安全

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值