第一个Hibernate程序

来源:互联网 发布:java登录次数锁定功能 编辑:程序博客网 时间:2024/05/07 21:07

配置文件hibernate.cfg.xml :

<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
 <session-factory >
 
     <!-- Database connection settings -->  
     <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
     <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcllibb</property>
     <property name="connection.username">asd</property>
     <property name="connection.password">asd</property>
    
     <!--<property name="dialect">org.hibernate.dialect.Oracle10iDialect</property> -->

   <property name="hbm2ddl.auto">update</property>
   <property name="show_sql">true</property>
   <mapping resource="com/bjsxt/hibernate/model/Student.hbm.xml"/>
  
  <!--<class-cache
   class="org.hibernate.test.legacy.Simple"
   region="Simple"
   usage="read-write"/>
      -->
 </session-factory>
</hibernate-configuration>

实体类:

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;
 }
 
}

映射文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
       
<hibernate-mapping >
   <class name="com.bjsxt.hibernate.model.Student" table="STUDENT" schema="ASD">  <!-- 如果表名与类名相同可以省略table属性 -->
      <id name="id" >   <!-- id映射主键 -->
         <column name="ID" length="20"/>
         <generator class="increment"/> <!--class="assigned" 如果不需要自增 -->
      </id>
      <property name="name" type="java.lang.String">
         <column name="NAME" length="20" not-null="true"/>
      </property>
      <property name="age"></property>
   </class>
</hibernate-mapping>

测试类:

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

import com.bjsxt.hibernate.model.Student;


public class StudentTest {

 public static void main(String[] args) {
  Student s = new Student();
  s.setAge(12);
  s.setName("二");
  
  Configuration cfg = new Configuration(); //创建Configuration
  SessionFactory factory = cfg.configure().buildSessionFactory(); //创建工厂
  Session session = factory.openSession(); //打开session
     Transaction tran =  session.beginTransaction();  //开启事务
  session.save(s);  //保存数据到数据库表
  tran.commit();  //提交事务
  session.close();//关闭session
  factory.close();//关闭sessionFactory

 }

}

 

附上创建SessionFactory的公共类:

package com.bjsxt.hibernate.util;

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

public class HibernateUtil {

 private static final SessionFactory sessionFactory = buildSessionFactory();
 private static SessionFactory buildSessionFactory(){
  try{
   return new Configuration().buildSessionFactory();
  }catch(Throwable ex){
   System.err.println("Initial SessionFactory creation failed."+ex);
   throw new ExceptionInInitializerError(ex);
  }
 }
 
 public static SessionFactory getSessionFactory(){
  return sessionFactory;
 }
}

 

原创粉丝点击