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

How to use Hibernate: 如何使用Hibernate(用MyEclipse)

1.  Create hibernate.cfg.xml || hibernate.properties         创建Hibernate的配置文件 !!
2.  Create tables in Database                                创建表单(在数据库里)
3.  Create PO(Persistent Objects)                            创建持久化类
4.  Create XXX.hbm.xml                                       创建对象-关系映射文件  !!!
5.  Create DataAccess Objects                                通过Hibernate API编写访问数据库的代码

-------------------------------------------
step1.  Create hibernate.cfg.xml        创建Hibernate的配置文件

<hibernate-configuration>

  <session-factory>
	<!-- mapping files -->


	<property name="connection.url">
		jdbc:mysql://localhost:3306/hibernatebookstore?useUnicode=true&characterEncoding=gb2312
	</property>
	<property name="connection.username">root</property>
	<property name="connection.password">root</property>
	<property name="connection.driver_class">
		com.mysql.jdbc.Driver
	</property>

	<property name="dialect">
		net.sf.hibernate.dialect.MySQLDialect     <!--    2.0         -->  
	</property>

                                          <!--    3.0     org.hibernate.dialect.MySQLDialect      --> 

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

        <mapping resource="com/liu/hibernate/po/Book.hbm.xml" />

   </session-factory>
</hibernate-configuration>


或者Create  hiberate.properties文件

connection.url=jdbc:mysql://localhost:3306/bookstoresql
connection.username=root
connection.password=root
connection.driver_class=com.mysql.jdbc.Driver
dialect=org.hibernate.dialect.MySQLDialect

------------------------------------------------
2.  Create tables in Database      创建表单在数据库里

 create table book( id bigint not null , title varchar(50) not null, price  double,  description varchar(200), primary key(id));

----------------------------------------------------------
step3. Create PO(Persistent Objects)  创建持久化类

      public abstract class AbstractBook implements Serializable
      public class Book   extends AbstractBook

  用MyEclipse 自动从数据库表单生成的两个类(一个抽象, 一个具体)
  如果自己手工建, 可以只生成的一个具体类

---------------------------------------------------
step4. Create XXX.xml             创建对象-关系映射文件

Book.hbm.xml         !!!!!!!!!!!!!!!!!!!!!!!

<hibernate-mapping package="com.liu.hibernate.po">

    <class name="Book" table="book">
        <id name="id" column="id" type="long">   
            <generator class="increment"/>
        </id>

        <property name="isbnNumber" column="isbnNumber" type="string"  not-null="true" />
        <property name="title" column="title" type="string"  not-null="true" />
        <property name="price" column="price" type="double" />
        <property name="description" column="description" type="string" />
    </class>

</hibernate-mapping>

在一般JAVA项目里,  配置文件放的路径为:      你的项目名称/hibernate.cfg.xml 
                               			or 你的项目名称/hibernate.properties
                                            你的项目名称/你的包名/xxx.hbm.xml
              你的项目名称必须设置在classpath中


在Web 项目里,  配置文件放的路径为:        你的Web应用名/WEB-INF/classes/hibernate.cfg.xml
                                         or   你的Web应用名/WEB-INF/classes/hibernate.properties
                                          你的Web应用名/WEB-INF/classes/你的包名/xxx.hbm.xml

--------------------------------------------------------
step5.  Create DataAccess   通过Hibernate API编写访问数据库的代码

  * 你可以用MyEclipse工具生成的HibernateSessionFactory来设定配置, 并获得Session

      public void  addBook(Book b){
	     Session sess= HibernateSessionFactory.getSession();
	    Transaction  tx=sess.beginTransaction();

	          //do some work
	     sess.save(b);

             tx.commit();		!!!!!!save book in DB	           
      }

   * 你可以自己生成SessionFactory对象, 并获得Session

         1. 生成SessionFactory对象

             //!!!!! 第一种用法 装载 hibernate.properties文件
               SessionFactory sfactory=null;
               try{  
        	  Configuration config=new Configuration();
       		  config.addClass(packageName.Book.class);
       		  //  config.addClass("Book.hbm.xml");

      		  sfactory=config.buildSessionFactory();  
   		}catch(Exception e){  e.printStackTrace();}


           //!!!!! 第二种用法 装载 hibernate.cfg.xml文件
            SessionFactory sfactory=null;
            try{  
         	Configuration config=new Configuration();
        	config.configure("/hibernate.cfg.xml");      
         	sfactory= config.buildSessionFactory();

         	//sfactory=new Configurtion().buildSessionFactory();
   	    }catch(Exception e){ e.printStackTrace();}


            Hibernate 就会在classpath中寻找相应的配置文件(hibernate.properties 或hibernate.cfg.xml).


       2. 获得Session

 	     Session se=sfactory.openSession();
             Transaction tx=null;

     	     tx=se.beginTransaction();

      		//do some work
      		se.delete("from Book as b  ");// !!!delete all books

     	     tx.commit();
  	     se.close();