Hibernate(3.6)之初识

来源:互联网 发布:javascript 引号 转义 编辑:程序博客网 时间:2024/06/05 14:31
一 环境:W7+Eclipse3.6 
二 所用Hibernate版本:hibernate-distribution-3.6.0.Final 
工程目录结构如下 
 
三 参考资料 
1 Hibernate3.6的Annotation问题 
http://mayuchuan99.blog.163.com/blog/static/320023442011029438354/ 

hibernate3.6之前的版本使用Annotation,还需要下载Annotation库,需要添加ejb3-persistence.jar hibernate-annotations.jar hibernate-commons-annotations.jar,而获得SessionFactory必须以下这样写 
   
Java代码  收藏代码
  1. Configuration cfg=new AnnotationConfiguration();  
  2.     SessionFactory s=cfg.configure().buildSessionFactory();  

在hibernate3.6的这个版本中,Annotation类库集成到了hibernate3.6,所以不在需要添加hibernate-annotations.jar hibernate-commons-annotations.jar等类库了。但是必须添加hibernate-jpa-2.0-api-1.0.0.Final.jar。 
     hibernate 3.6要获取一个SessionFactory,我可以直接像用xml来配置实体与数据库表的映射关系那样。代码如下: 
Java代码  收藏代码
  1. Configuration cfg=new Configuration();  
  2. SessionFactory s=cfg.configure().buildSessionFactory();  

四 具体代码 
1 hibernate.cfg.xml 
Xml代码  收藏代码
  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.   
  8.     <session-factory>  
  9.   
  10.         <!-- Database connection settings -->  
  11.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  12.         <property name="connection.url">jdbc:mysql://localhost/hibernate</property>         
  13.         <property name="connection.username">root</property>  
  14.         <property name="connection.password">root</property>  
  15.         <!-- JDBC connection pool (use the built-in)   
  16.         <property name="connection.pool_size">1</property>  
  17.         -->  
  18.         <!-- SQL dialect -->  
  19.         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  20.         <!-- Enable Hibernate's automatic session context management   
  21.         <property name="current_session_context_class">thread</property>  
  22.         -->  
  23.         <!-- Disable the second-level cache  -->  
  24.         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>  
  25.         <!-- Echo all executed SQL to stdout -->  
  26.         <property name="show_sql">true</property>  
  27.         <!-- Drop and re-create the database schema on startup  
  28.         <property name="hbm2ddl.auto">update</property>  
  29.          -->  
  30.           
  31.         <!--  
  32.         <mapping resource="org/hibernate/model/Student.hbm.xml"/> 
  33.         -->  
  34.         <mapping class="org.hibernate.model.Student"/>  
  35.   
  36.     </session-factory>  
  37. </hibernate-configuration>  


2 Student.hbm.xml 
Xml代码  收藏代码
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping >  
  7.      <class name="org.hibernate.model.Student">  
  8.         <id name="id" column="id">  
  9.             <generator class="native"/>  
  10.         </id>  
  11.         <property name="name"/>  
  12.         <property name="age"/>  
  13.     </class>  
  14. </hibernate-mapping>  


3 Student.java 
Java代码  收藏代码
  1. @Entity  
  2. public class Student {  
  3.       
  4.     @Id  
  5. @GeneratedValue  
  6.     private int id;  
  7.     private String name;  
  8.     private int age;   
  9.         //get set  
  10. }  


4 HibernateUtil.java 
Java代码  收藏代码
  1. package org.hibernate.model;  
  2.   
  3. import org.hibernate.SessionFactory;  
  4.   
  5. import org.hibernate.cfg.Configuration;  
  6.   
  7.   
  8. public class HibernateUtil {  
  9.   
  10.     private static final SessionFactory sessionFactory = buildSessionFactory();  
  11.   
  12.     private static SessionFactory buildSessionFactory() {  
  13.         try {  
  14.             // Create the SessionFactory from hibernate.cfg.xml  
  15.             return new Configuration().configure().buildSessionFactory();  
  16.         }catch (Throwable ex) {  
  17.             // Make sure you log the exception, as it might be swallowed  
  18.             System.err.println("Initial SessionFactory creation failed." + ex);  
  19.             throw new ExceptionInInitializerError(ex);  
  20.         }  
  21.   
  22.     }  
  23.   
  24.   
  25.     public static SessionFactory getSessionFactory() {  
  26.         return sessionFactory;  
  27.     }  
  28.   
  29. }  


5 Test.java 
Java代码  收藏代码
  1. import org.hibernate.Session;  
  2.   
  3. public class Test {  
  4.     public static void main(String[] args) {  
  5.   
  6.         Student s = new Student();  
  7.         // 设置了ID的自动递增,就不用在指定ID值  
  8.         // s.setId(1);  
  9.         s.setName("zhangsan");  
  10.         s.setAge(8);  
  11.           
  12.         Session session = HibernateUtil.getSessionFactory().getCurrentSession();  
  13.         session.beginTransaction();  
  14.         session.save(s);  
  15.         session.getTransaction().commit();  
  16.         HibernateUtil.getSessionFactory().close();  
  17.     }  
  18. }  


6 mysql.sql 
Sql代码  收藏代码
  1. DROP TABLE IF EXISTS `student`;  
  2. CREATE TABLE `student` (  
  3.   `id` int(20) NOT NULL AUTO_INCREMENT,  
  4.   `namevarchar(20) DEFAULT NULL,  
  5.   `age` int(3) DEFAULT NULL,  
  6.   PRIMARY KEY (`id`)  
  7. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;  
  8.   
  9. INSERT INTO `student` VALUES ('1''zhangsan''8');  
0 0
原创粉丝点击