当前位置:编程学习 > JAVA >>

初见Hibernate(二)

基于annotations配置文件实现的第一个hibernate小程序。
1.首先新建一个普通的java工程。
2.将oracle数据库驱动包和实现hibernate的最小包导入到该工程中。
Oracle数据库驱动包为:
Oracle 10g 10.2.0.4 JDBC_ojdbc14.jar
Hibernate的最小包为:
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate3.jar
javassist-3.4.GA.jar
jta-1.1.jar
slf4j-api-1.5.2.jar
slf4j-nop-1.5.2.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
ejb3-persistence.jar(这个包是hibernate基于jpa规范的实现)
 
3.在src文件夹下建一个包名为com.neusoft.bean包,在该包下建立一个名为Person.java的文件,内容如下:
 
package com.neusoft.bean;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity
@Table(name="person_anno")
public class Person {
       private int id;
       private String name;
 
       @Id
       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;
       }
 
}
 
4.在src包下建立名为com.neusoft.test的包,然后在该包下建立名为Test.java的测试类。内容为:
 
package com.neusoft.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
 
import com.neusoft.bean.Person;
 
public class Test {
 
       public static void main(String[] args) {
              Person p = new Person();
              p.setId(1);
              p.setName("wjl");
 
              Configuration cfg = new AnnotationConfiguration();
              SessionFactory sf = cfg.configure().buildSessionFactory();
              Session s = sf.openSession();
              s.beginTransaction().begin();
              s.save(p);
              s.getTransaction().commit();
              s.close();
              sf.close();
 
       }
 
}
 
5.在src文件夹下建立名为hibernate.cfg.xml的hibernate的配置文件。内容如下:
 
<?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">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@192.168.1.102:1521:oracle</property>
        <property name="connection.username">hr</property>
        <property name="connection.password">hr</property>
 
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
 
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.OracleDialect</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">create</property>
 
        <mapping class="com.neusoft.bean.Person" />
 
    </session-factory>
 
</hibernate-configuration>
 
6.执行Test.java在控制台中打印出如下信息:
Hibernate: insert into person_anno (name, id) values (?, ?)
7. 查看数据库,将看到生成了PERSON_ANNO表,并生成了一条数据。
 
作者“极致书生”

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,