Hibernate基础:快速入门(3):SessionFactory和Session

来源:互联网 发布:淘宝店铺规划方案 编辑:程序博客网 时间:2024/06/05 02:13

这里也像前面的JDBC的Sample那样,如何设定Hibernate的设定文件,如何从SessionFactory中取出一个session。

hibernate.cfg.xml

创建如下所示的hibernate的设定文件

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-configuration SYSTEM  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>  <session-factory>    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hbtestdb?useSSL=false</property>    <property name="hibernate.connection.username">hbtestuser01</property>    <property name="hibernate.connection.password">hbtestuser01</property>    <property name="show_sql">true</property>    <property name="current_session_context_class">thread</property>  </session-factory></hibernate-configuration>

Hibernate properties

项番 Properties Description 1 hibernate.dialect This property makes Hibernate generate the appropriate SQL for the chosen database. 2 hibernate.connection.driver_class The JDBC driver class. 3 hibernate.connection.url The JDBC URL to the database instance. 4 hibernate.connection.username The database username. 5 hibernate.connection.password The database password. 6 hibernate.connection.pool_size Limits the number of connections waiting in the Hibernate database connection pool. 7 hibernate.connection.autocommit Allows autocommit mode to be used for the JDBC connection.

Hibernate DB & JNDI properties

项番 Properties Description 1 hibernate.connection.datasource The JNDI name defined in the application server context you are using for the application. 2 hibernate.jndi.class The InitialContext class for JNDI. 3 hibernate.jndi. Passes any JNDI property you like to the JNDI InitialContext. 4 hibernate.jndi.url Provides the URL for JNDI. 5 hibernate.connection.username The database username. 6 hibernate.connection.password The database password.

Hibernate Dialect

Database Dialect Property DB2 org.hibernate.dialect.DB2Dialect HSQLDB org.hibernate.dialect.HSQLDialect HypersonicSQL org.hibernate.dialect.HSQLDialect Informix org.hibernate.dialect.InformixDialect Ingres org.hibernate.dialect.IngresDialect Interbase org.hibernate.dialect.InterbaseDialect Microsoft SQL Server 2000 org.hibernate.dialect.SQLServerDialect Microsoft SQL Server 2005 org.hibernate.dialect.SQLServer2005Dialect Microsoft SQL Server 2008 org.hibernate.dialect.SQLServer2008Dialect MySQL org.hibernate.dialect.MySQLDialect Oracle (any version) org.hibernate.dialect.OracleDialect Oracle 11g org.hibernate.dialect.Oracle10gDialect Oracle 10g org.hibernate.dialect.Oracle10gDialect Oracle 9i org.hibernate.dialect.Oracle9iDialect PostgreSQL org.hibernate.dialect.PostgreSQLDialect Progress org.hibernate.dialect.ProgressDialect SAP DB org.hibernate.dialect.SAPDBDialect Sybase org.hibernate.dialect.SybaseDialect Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect

Entity Class:User

我们将会创建一个Entity的User类。Entity类就是用来与数据库的Table进行Mapping的Class。

import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.Table;import javax.persistence.Column;@Entity@Table (name="hbtableuser")public class User {  @Id  @Column(name="userid")  private int userid;  @Column(name="username")  private String username;  @Column(name="country")  private String country;  public  User(){    System.out.println("Default User construction is called...");  }  public User(int userid, String username, String country) {    this.userid = userid;    this.username = username;    this.country = country;  }  public int getUserid() {    return userid;  }  public void setUserid(int userid) {    this.userid = userid;  }  public String getUsername() {    return username;  }  public void setUsername(String username) {    this.username = username;  }  public String getCountry() {    return country;  }  public void setCountry(String country) {    this.country = country;  }}
类型 注解 import 作用 类注解 @Entity javax.persistence.Entity 类注解用 类注解 @Table javax.persistence.Table 类注解用:@Table (name=”Table名”) 属性注解 @Id javax.persistence.Id 主码注解 属性注解 @Column javax.persistence.Column 所有字段都需要

使用xml文件自然也可以,由于注解可能是今后的标准方向,相关的例子都回使用注解方式。各个字段的get/set方法以及default无参的构造函数也都做成。

创建Demo演示代码

import org.hibernate.Session;import org.hibernate.SessionFactory;public class HbDemo01 {  public static void main(String[] args){    try{      SessionFactory sessionFactory = new org.hibernate.cfg.Configuration()                                    .configure("hibernate.cfg.xml")                                    .addAnnotatedClass(User.class)                                    .buildSessionFactory();      Session session = sessionFactory.getCurrentSession();      System.out.println("Successfully got connection from session factory.");      sessionFactory.close();    }catch (Exception e){      System.out.println("Exception happened...");      e.printStackTrace();    }  }}
项目 详细说明 SessionFactory 读取Hibernate设定文件,创建Session对象,因为SessionFactory很重,一般只是创建一次,反复使用。 Session SessionFactory所创建,封装了JDBC Connection,用来和数据库存取进行交互操作。

执行结果

十二月 12, 2016 9:12:14 下午 org.hibernate.Version logVersionINFO: HHH000412: Hibernate Core {5.2.5.Final}十二月 12, 2016 9:12:14 下午 org.hibernate.cfg.Environment <clinit>INFO: HHH000206: hibernate.properties not found十二月 12, 2016 9:12:14 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}十二月 12, 2016 9:12:14 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureWARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)十二月 12, 2016 9:12:14 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreatorINFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hbtestdb?useSSL=false]十二月 12, 2016 9:12:14 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreatorINFO: HHH10001001: Connection properties: {user=hbtestuser01, password=****}十二月 12, 2016 9:12:14 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreatorINFO: HHH10001003: Autocommit mode: false十二月 12, 2016 9:12:14 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>INFO: HHH000115: Hibernate connection pool size: 20 (min=1)十二月 12, 2016 9:12:15 下午 org.hibernate.dialect.Dialect <init>INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect十二月 12, 2016 9:12:15 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stopINFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/hbtestdb?useSSL=false]Successfully got connection from session factory.
0 0
原创粉丝点击