免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 932 | 回复: 0
打印 上一主题 下一主题

Begin to learn hibernate [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-06-01 10:49 |只看该作者 |倒序浏览


用hibernate写了第一个ORM程序,控制台的,实现往数据库中写,必须得包括以下几个包:
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ehcache-1.1.jar
hibernate3.jar
jta.jar
log4j-1.2.11.jar
mysql-connector-java-3.1.12-bin.jar

要实现读出,必须加入以下包:
antlr-2.7.6rc1.jar

建立数据库与表:

`computer`.CREATE DATABASE `computer` /*!40100 DEFAULT CHARACTER SET latin1 */;

DROP TABLE IF EXISTS `computer`.`computer`;
CREATE TABLE  `computer`.`computer` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `cpu` varchar(45) default NULL,
  `display` varchar(45) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

配置了以下文件:
hibernate.cfg.xml(置于/src目录中)

   
        org.hibernate.dialect.MySQLDialect
        org.gjt.mm.mysql.Driver
        jdbc:mysql://localhost/computer
        root
        aoe
        
   

*.hbm.xml(mapping--与相关类放在同一目录)

   
        
            
        
        
        
   

log4j.properties(置于根下)

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.rootLogger=warn, stdout

编写对象类:

public class computer {
    private int id;
    private String cpu;
    private String display;
   
    public int getId(){
        return id;
    }
    public void setId(int id){
        this.id=id;
    }
    public String getCpu(){
        return cpu;
    }
    public void setCpu(String cpu){
        this.cpu=cpu;
    }
    public String getDisplay(){
        return display;
    }
    public void setDisplay(String display){
        this.display=display;
    }
   
   
    /** Creates a new instance of computer */
    public computer() {
    }
}
注意每一个变量都要有相对应的setXX与getXX方法,也包括为主键自动增长的id字段.

持久化编程

import java.sql.SQLException;
import org.apache.log4j.PropertyConfigurator;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

    public static void main(String[] args) throws HibernateException{
        PropertyConfigurator.configure("log4j.properties");
        Configuration cfg = new Configuration();
        cfg.configure();
        SessionFactory sf = cfg.buildSessionFactory();
        Session sess = sf.openSession();
        computer pc = new computer();
        pc.setCpu("Inter 奔腾 4 2.8B GHz");
        pc.setDisplay("LG 563LS");
        sess.save(pc);
        sess.flush();
        try{
            sess.connection().commit();
        }catch(HibernateException e){
            e.printStackTrace();
        }catch(SQLException e){
            e.printStackTrace();
        }
        sess.close();
    }

但是还有一个警告信息存在:
No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/ztkdir/java/hibernate/hibernate-3.1.3/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
缺少ehcache.xml配置文件,在src目录下新建ehcache.xml,并配置如下:
   
网上的资料说,还要在hibernate.cfg.xml与*.hbm.xml中加入相应的配置,这是对hibernate3.0以前的版本说的.对3.0版本只须建立ehcache.xml即可,不要再做其它操作就OK了!

另外:我的mysql的数据编程为utf8,从程序中写入的汉字为乱码,些问题待研究.
原因:Hibernate的基础还是JDBC,所以一样需要设置characterEncoding!
解决方法
在hibernate.cfg.xml中应该这样写
property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8property>如果用hibernate.properties
#hibernate.connection.url jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
ps:处理中文的话,characterEncoding用GBK一样可以。
但字节编码问题最好的解决方法还是统一使用UTF-8!!!



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/17347/showart_121416.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP