Hibernate入门

 

前言

 

使用面向对象的语言和关系型的数据库,开发起来很繁琐,费时。由于现在流行的数据库都不面向对象。Hibernate 是一个Java的ORM(Object/Relational Mapping)解决方案。

 

Hibernte不仅关心把Java对象对应到数据库的表中,而且提供了请求和检索的方法。简化了手工进行JDBC操作的流程。

 

如何使用Hibernate:

 

增加Hibernate的数据库配置文件:

 

首先,我们要增加:

 

hibernate.cfg.xml 位置在类包中。

 

hibernate.cfg.xml的内容:

 

 

<?xml version='1.0' encoding='utf-8'?>
<!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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.h2.Driver</property>
        <property name="connection.url">jdbc:h2:db/h2manager</property>
        <property name="connection.username">tntxia</property>
        <property name="connection.password">123456</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.H2Dialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>
        
        <mapping resource="com/tntxia/test/hibernate/Entity/Event.hbm.xml"/>

    </session-factory>

</hibernate-configuration>
 

 

然后,我们再在com/tntxia/test/hibernate/Entity里面增加一个Event.hbm.xml的配置文件。

 

如下:

 

<?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 package="com.tntxia.test.hibernate.Entity">

    <class name="Event" table="EVENTS">
        <id name="id" column="EVENT_ID">
            <generator class="increment"/>
        </id>
        <property name="date" type="timestamp" column="EVENT_DATE"/>
        <property name="title"/>
    </class>

</hibernate-mapping>

 

Java的测试代码如下:

 

package com.tntxia.test.hibernate;


import java.util.Date;
import java.util.List;

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

import com.tntxia.test.hibernate.Entity.Event;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// A SessionFactory is set up once for an application
		SessionFactory sessionFactory = new Configuration()
                .configure() // configures settings from hibernate.cfg.xml
                .buildSessionFactory();
     // create a couple of events...
     		Session session = sessionFactory.openSession();
     		session.beginTransaction();
     		session.save( new Event( "Our very first event!", new Date() ) );
     		session.save( new Event( "A follow up event", new Date() ) );
     		session.getTransaction().commit();
     		session.close();

     		// now lets pull events from the database and list them
     		session = sessionFactory.openSession();
             session.beginTransaction();
             List<Event> result = session.createQuery( "from Event" ).list();
     		for ( Event event : (List<Event>) result ) {
     			System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
     		}
             session.getTransaction().commit();
             session.close();
		
	}

}

 

 这个例子中,我们插入了两条记录。然后又把这两条记录查询出来。

 

当我们再写一个查询的语句时,

 

 

package com.tntxia.test.hibernate;


import java.util.List;

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

import com.tntxia.test.hibernate.Entity.Event;

public class Test2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// A SessionFactory is set up once for an application
		SessionFactory sessionFactory = new Configuration()
                .configure() // configures settings from hibernate.cfg.xml
                .buildSessionFactory();
     // create a couple of events...
     		Session session = sessionFactory.openSession();
     		
            session.beginTransaction();
            List<Event> result = session.createQuery( "from Event" ).list();
    		for ( Event event : (List<Event>) result ) {
    			System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
    		}
            session.getTransaction().commit();
            session.close();
		
	}

}
 

 

 发现刚刚插入的数据,无法查询出来。因为hbm2ddl.auto的属性为true,每次运行,数据库都会重建,所以应该把这个属性删掉。

 

 

 

你可能感兴趣的:(Hibernate)