hibernate入门

来源:互联网 发布:sql2000数据同步 编辑:程序博客网 时间:2024/05/02 01:27

今天做了hibernate的一个测试程序,总结

1,hibernate配置文件 hibernate.properties  内容为定义数据源,存放在class的根目录,如下:

hibernate.dialect=org.hibernate.dialect.DB2Dialect
hibernate.connection.driver_class=com.ibm.db2.jcc.DB2Driver
hibernate.connection.url=jdbc:db2://10.7.3.250:50001/testdb
hibernate.connection.username=db2inst1
hibernate.connection.password=jkl,,123
hibernate.show_sql=true

2,持久化类,对应数据库表的字段

public class Customer implements Serializable {

    private String snum;
    private String id;
    private String postflag;
    private Double type;
    private Double cancleflag;  

    public String getSnum() {
        return snum;
    }

    public void setSnum(String snum) {
        this.snum = snum;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPostflag() {
        return postflag;
    }

    public void setPostflag(String postflag) {
        this.postflag = postflag;
    }

    public Double getType() {
        return type;
    }

    public void setType(Double type) {
        this.type = type;
    }

    public Double getCancleflag() {
        return cancleflag;
    }

    public void setCancleflag(Double cancleflag) {
        this.cancleflag = cancleflag;
    }
}
3,关系映射文件,与类同名,扩展名为hbm.xml 并与类放到同一目录下

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
  <class name="apphibernate.Customer" table="SCOTT.CRM_APPLY200609"> 
    <id name="snum" column="DEV_NO" type="string">
      <generator class="increment"/>
    </id>
    <property name="id"     column="CUST_ID"     type="string" not-null="true" />
    <property name="postflag"  column="POST_FLAG"  type="string" not-null="true"/>
    <property name="type"     column="POSTCUST_TYPE"     type="java.lang.Double" />
    <property name="cancleflag"   column="POSTCANCEL_FLAG"   type="java.lang.Double" />
  </class>
</hibernate-mapping>

4,加载hibernate库文件

5,通过hibernate API操作数据库

public class BusinessService{
    public static SessionFactory sessionFactory;
   
    static{
        try{
            // Create a configuration based on the properties file we've put
            // in the standard place.
            Configuration config = new Configuration();
            config.addClass(Customer.class);
            // Get the session factory we can use for persistence
            sessionFactory = config.buildSessionFactory();
        }catch(Exception e){
            e.printStackTrace();}
    }
        
    public static void main(String args[]) throws Exception {
        Session session = sessionFactory.openSession();
        List lList=session.createQuery("from Customer").list();
        Iterator it=lList.iterator();
        while(it.hasNext()){
            Customer Cu=(Customer)it.next();
            System.out.println(Cu.getSnum()+"  "+Cu.getId());
        }
        sessionFactory.close();
    }
}

测试中遇到的问题:

1,库没有加载全

2,关系映射文件中定义的类路径没有写全

3,持久化类字段定义与数据库字段定义不兼容

 

 

 

 

 

原创粉丝点击