天天看点

hibernate最初印象---helloworld的实现

最近,由于种种原因,开始接触hibernate,今天实现了第一个hibernate的入门程序,也就是经典的helloworld,个人喜欢在框架学习的时候,把最基本的helloworld程序的开发过程详细的写下来,以备日后搭建环境时作为参考,步入正题,说说hibernate的helloworld的开发。

以贴图为主,先来看看hibernate工程的目录结构

hibernate最初印象---helloworld的实现

目录的结构很清晰,额外说一句,我是采用的hibernate4的jar包,具体下载,详见hibernate官网的下载目录,还有我采用的是mysql的数据库,所以,mysql的连接jar文件必不可少

Studnet是一个model,具体实现,参见下面代码

Java代码

hibernate最初印象---helloworld的实现
hibernate最初印象---helloworld的实现
hibernate最初印象---helloworld的实现
  1. package com.bjsxt.hibernate.model; 
  2. public class Student { 
  3.     private int id; 
  4.     private String name; 
  5.     private int age; 
  6.     public int getId() { 
  7.         return id; 
  8.     } 
  9.     public void setId(int id) { 
  10.         this.id = id; 
  11.     } 
  12.     public String getName() { 
  13.         return name; 
  14.     } 
  15.     public void setName(String name) { 
  16.         this.name = name; 
  17.     } 
  18.     public int getAge() { 
  19.         return age; 
  20.     }  
  21.     public void setAge(int age) { 
  22.         this.age = age; 
  23.     } 
package com.bjsxt.hibernate.model;

public class Student {
	private int id;
	private String name;
	private int age;
	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 int getAge() {
		return age;
	} 
	public void setAge(int age) {
		this.age = age;
	}
	
}
           

hibernate的配置文件

Xml代码

hibernate最初印象---helloworld的实现
hibernate最初印象---helloworld的实现
hibernate最初印象---helloworld的实现
  1. <?xml version='1.0'encoding='utf-8'?> 
  2. <!DOCTYPE hibernate-configuration PUBLIC 
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
  4.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
  5. <hibernate-configuration> 
  6.     <session-factory> 
  7.         <!-- Database connection settings --> 
  8.         <propertyname="connection.driver_class">com.mysql.jdbc.Driver</property> 
  9.         <propertyname="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 
  10.         <propertyname="connection.username">root</property> 
  11.         <propertyname="connection.password">mlc</property> 
  12.         <!-- JDBC connection pool (use the built-in) --> 
  13.        <!--   <property name="connection.pool_size">1</property>  --> 
  14.         <!-- SQL dialect --> 
  15.         <propertyname="dialect">org.hibernate.dialect.MySQLDialect</property> 
  16.         <!-- Enable Hibernate's automatic session context management --> 
  17.         <!--  <property name="current_session_context_class">thread</property> --> 
  18.         <!-- Disable the second-level cache  --> 
  19.         <propertyname="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 
  20.         <!-- Echo all executed SQL to stdout --> 
  21.         <propertyname="show_sql">true</property> 
  22.         <!-- Drop and re-create the database schema on startup --> 
  23.         <!--  <property name="hbm2ddl.auto">update</property> --> 
  24.         <mappingresource="com/bjsxt/hibernate/model/Student.hbm.xml"/> 
  25.     </session-factory> 
  26. </hibernate-configuration> 
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">mlc</property>

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

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

        <!-- Enable Hibernate's automatic session context management -->
        <!--  <property name="current_session_context_class">thread</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">update</property> -->

        <mapping resource="com/bjsxt/hibernate/model/Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>      

hibernate的映射关系配置文件

   <?xml version="1.0"?>

Java代码

hibernate最初印象---helloworld的实现
hibernate最初印象---helloworld的实现
hibernate最初印象---helloworld的实现
  1. <!DOCTYPE hibernate-mapping PUBLIC 
  2.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
  3.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
  4. <hibernate-mapping package="com.bjsxt.hibernate.model"> 
  5.     <class name="Student" table="student"> 
  6.         <id name="id"> 
  7.         </id> 
  8.         <property name="name"/> 
  9.         <property name="age"/> 
  10.     </class> 
  11. </hibernate-mapping> 
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


<hibernate-mapping package="com.bjsxt.hibernate.model">

    <class name="Student" table="student">
        <id name="id">
        </id>
        <property name="name"/>
        <property name="age"/>
    </class>

</hibernate-mapping>
           

用于测试hibernate的java类

Java代码

hibernate最初印象---helloworld的实现
hibernate最初印象---helloworld的实现
hibernate最初印象---helloworld的实现
  1. package com.bjsxt.hibernate.model; 
  2. import org.hibernate.Session; 
  3. import org.hibernate.SessionFactory; 
  4. import org.hibernate.cfg.Configuration; 
  5. public class StudentTest { 
  6.     public staticvoid main(String[] args) { 
  7.         // TODO Auto-generated method stub 
  8.         Student stu = new Student(); 
  9.         stu.setAge(2); 
  10.         stu.setId(2); 
  11.         stu.setName("mlc"); 
  12.         Configuration cf = new  Configuration(); 
  13.         SessionFactory sf = cf.configure().buildSessionFactory(); 
  14.         Session session =sf.openSession(); 
  15.         session.beginTransaction(); 
  16.         session.save(stu); 
  17.         session.getTransaction().commit(); 
  18.         session.close(); 
  19.         sf.close(); 
  20.     } 
package com.bjsxt.hibernate.model;

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

public class StudentTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student stu = new Student();
		stu.setAge(2);
		stu.setId(2);
		stu.setName("mlc");
		
		Configuration cf = new  Configuration();
		SessionFactory sf = cf.configure().buildSessionFactory();
		Session session =sf.openSession();
		session.beginTransaction();
		session.save(stu);
		session.getTransaction().commit();
		session.close();
		sf.close();
	}
}
           

运行结果

  2011-12-22 10:34:13 org.hibernate.annotations.common.Version <clinit>

Xml代码

hibernate最初印象---helloworld的实现
hibernate最初印象---helloworld的实现
hibernate最初印象---helloworld的实现
  1. INFO: HCANN000001: Hibernate Commons Annotations {4.0.0.CR2} 
  2. 2011-12-22 10:34:13 org.hibernate.Version logVersion 
  3. INFO: HHH000412: Hibernate Core {4.0.0.CR5} 
  4. 2011-12-22 10:34:13 org.hibernate.cfg.Environment <clinit> 
  5. INFO: HHH000206: hibernate.properties not found 
  6. 2011-12-22 10:34:13 org.hibernate.cfg.Environment buildBytecodeProvider 
  7. INFO: HHH000021: Bytecode provider name : javassist 
  8. 2011-12-22 10:34:13 org.hibernate.cfg.Configuration configure 
  9. INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 
  10. 2011-12-22 10:34:13 org.hibernate.cfg.Configuration getConfigurationInputStream 
  11. INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 
  12. 2011-12-22 10:34:13 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity 
  13. WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! 
  14. 2011-12-22 10:34:13 org.hibernate.cfg.Configuration addResource 
  15. INFO: HHH000221: Reading mappings from resource: com/bjsxt/hibernate/model/Student.hbm.xml 
  16. 2011-12-22 10:34:13 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity 
  17. WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! 
  18. 2011-12-22 10:34:13 org.hibernate.cfg.Configuration doConfigure 
  19. INFO: HHH000041: Configured SessionFactory: null 
  20. 2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
  21. INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) 
  22. 2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
  23. INFO: HHH000115: Hibernate connection pool size: 20 
  24. 2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
  25. INFO: HHH000006: Autocommit mode: false 
  26. 2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
  27. INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate] 
  28. 2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
  29. INFO: HHH000046: Connection properties: {user=root,password=****} 
  30. 2011-12-22 10:34:14 org.hibernate.dialect.Dialect <init> 
  31. INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 
  32. 2011-12-22 10:34:14 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService 
  33. INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) 
  34. 2011-12-22 10:34:14 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory<init> 
  35. INFO: HHH000397: Using ASTQueryTranslatorFactory 
  36. Hibernate: insert into student (name, age, id) values (?, ?, ?) 
  37. 2011-12-22 10:34:14 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop 
  38. INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate] 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.0.CR2}
2011-12-22 10:34:13 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.0.0.CR5}
2011-12-22 10:34:13 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
2011-12-22 10:34:13 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
2011-12-22 10:34:13 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2011-12-22 10:34:13 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2011-12-22 10:34:13 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
2011-12-22 10:34:13 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/bjsxt/hibernate/model/Student.hbm.xml
2011-12-22 10:34:13 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
2011-12-22 10:34:13 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate]
2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
2011-12-22 10:34:14 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2011-12-22 10:34:14 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
2011-12-22 10:34:14 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: insert into student (name, age, id) values (?, ?, ?)
2011-12-22 10:34:14 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate]
      

基本就这些了,做点备注吧

Java代码

hibernate最初印象---helloworld的实现
hibernate最初印象---helloworld的实现
hibernate最初印象---helloworld的实现
  1. 首先,本身的目录的结构是约定俗称的一种配置,包括文件的命名 
  2. 其次,hibernate的映射文件,必须保证要数据库中要有相应的表与之对应,或者名称是对应的,或者,自行指定也可以 
  3. 最后,hibernate的映射文件中,id是代表了表的主键值,property是其它的属性值 
  4. 特别提醒: 
  5.      对于配置文件,最好还是从原始的参考文档中直接复制粘贴,而不要自己敲或者其它什么的,没啥技术含量,而且容易出错 
首先,本身的目录的结构是约定俗称的一种配置,包括文件的命名
其次,hibernate的映射文件,必须保证要数据库中要有相应的表与之对应,或者名称是对应的,或者,自行指定也可以
最后,hibernate的映射文件中,id是代表了表的主键值,property是其它的属性值

特别提醒:
     对于配置文件,最好还是从原始的参考文档中直接复制粘贴,而不要自己敲或者其它什么的,没啥技术含量,而且容易出错
           

Java代码

hibernate最初印象---helloworld的实现
hibernate最初印象---helloworld的实现
hibernate最初印象---helloworld的实现
  1. 如果在细节上,有什么问题,可以针对性的交流 
如果在细节上,有什么问题,可以针对性的交流
           

继续阅读