首页 > 代码库 > Hibernate4实例

Hibernate4实例

Hibernate是流行的ORM开源框架,它向开发者提供了面向对象编程接口,隐藏了底层的数据库基础操作。

hibernate版本4.3.11

该简单实例只是实现增加一条记录到数据库中,数据库使用mysql。

大概步骤:

1.hibernate配置

2.创建实体类

3.关系映射配置

4.加载配置创建session。

 

 技术分享

1.hibernate配置

hibernate默认的配置文件以hibernate.properties(key value格式)或hibernate.cfg.xml(XML文件格式)命名,实现项目中通常使用xml文件格式。

下载hibernate包后解压,可以在hibernate-release-4.3.11.Final\documentation\manual\en-US\html_single\index.html文件中找到配置模板。

技术分享

根据帮助文档的配置实例修改:

技术分享
 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:3306/test</property>13         <property name="connection.username">root</property>14         <property name="connection.password">root</property>15 16         <!-- JDBC connection pool (use the built-in) -->17         <property name="connection.pool_size">1</property>18 19         <!-- SQL dialect -->20         <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect </property>21 22         <!-- Enable Hibernate‘s automatic session context management -->23         <property name="current_session_context_class">thread</property>24 25         <!-- Disable the second-level cache  -->26         <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>27 28         <!-- Echo all executed SQL to stdout -->29         <property name="show_sql">true</property>30 31         <!-- Drop and re-create the database schema on startup -->32         <property name="hbm2ddl.auto">update</property>33 34         <mapping resource="config/hibernate/News.hbm.xml"/>35 36     </session-factory>37 38 </hibernate-configuration>
hibernate.cfg.xml

 

2.创建实体类

技术分享
 1 package com.wei.pojo; 2  3 public class News { 4      5     private Integer id; 6     private String title; 7     private String content; 8      9     public News(){10         11     }12 13     public Integer getId() {14         return id;15     }16 17     public void setId(Integer id) {18         this.id = id;19     }20 21     public String getTitle() {22         return title;23     }24 25     public void setTitle(String title) {26         this.title = title;27     }28 29     public String getContent() {30         return content;31     }32 33     public void setContent(String content) {34         this.content = content;35     }36 37 }
News.java

 

3.关系映射配置

News类对应数据库中News表

技术分享
1 create table News2 (3     id int primary key auto_increment,4     title varchar(100) not null,5     content varchar(5000)6 )
News.sql

映射配置文件头的声明可以根据hibernate.cfg.xml的文件头声明修改得来,将hibernate-configuration替换成hibernate-mapping。

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5          6 <hibernate-mapping package="com.wei.pojo"> 7  8     <class name="News" table="News"> 9         <id name="id" column="id">10             <generator class="identity"/>11         </id>12         <property name="title"/>13         <property name="content"/>14     </class>15 16 </hibernate-mapping>
News.hbm.xml

4.加载配置创建session

技术分享
 1 package com.wei.proc; 2  3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.Transaction; 6 import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 7 import org.hibernate.cfg.Configuration; 8 import org.hibernate.service.ServiceRegistry; 9 10 import com.wei.pojo.News;11 12 public class DataProc {13 14     public static void main(String[] args) {15         // TODO Auto-generated method stub16         17         Configuration config=new Configuration().configure();18         19         ServiceRegistry service=new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();20         //ServiceRegistry service=new StandardServiceRegistryBuilder().build();21         SessionFactory sessionFactory=config.buildSessionFactory(service);22         Session session=sessionFactory.openSession();23         Transaction tran=session.beginTransaction();24         25         News news=new News();26         news.setTitle("title内容");27         news.setContent("content内容");28         29         session.save(news);30         tran.commit();31         session.close();32         sessionFactory.close();33 34     }35 36 }
DataProc.java

注:

1.使用configuration.buildSessionFactory()方法会提示该方法已过时,实例中使用了带参数的buildSessionFactory方法。

2.创建ServiceRegistry对应时,若使用:

ServiceRegistry service=new StandardServiceRegistryBuilder().build();

则会产生以下错误:

Exception in thread "main" org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect‘ not set    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104)    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71)    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209)    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)    at com.wei.proc.DataProc.main(DataProc.java:21)

应使用以下代码代替:

ServiceRegistry service=new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();

 

代码下载:http://pan.baidu.com/s/1miGuCsw

Hibernate4实例