HIbernate学习



















Hibernate配置
1、复制hibernate3.jar和lib下*.jar文件到WEB-INF/lib中 commons-logging.jar只留一个
2、复制etc下log4j.properties到WEB-INF/classes 即开发的src目录
3、src目录下新建一个hibernate.cfg.xml主配置文件
4、model.hbm.xml XML映射文件

Hibernate使用说明
[url]http://www.hibernate.org.cn/[/url]
hibernate.cfg.xml配置文件
设置连接池
设置xml映射文件
另一种配置文件hibernate.properties

采用properties文件–初始化
Configuration config = new Configuration();
config.addClass(myclass.class);
配置文件为XML–初始化
Configuration config = new Configuration().config(配置文件名);

映射文件*.hbm.xml

定义实体类和数据表关系,name是实体类,table对应的数据表,可以省略掉,默认表名和类名相同
name定义主键id字段,type设置类型,column设置主键生成方法,MSSQL,MYSQL用identity,oracle用sequence 定义实体类和表字段的关联,name是使用的名称,column是字段名,not-null=”true”不允许空,length=”16″设置长度,unique
设置多对一

以实体类的字段为依据来配置xml映射文件,类的字段有则映射有,类的字段无则映射无

管理session的类

实例[url]http://www.onjava.com/lpt/a/4386[/url]
插入
// 1. Build a Product
Product p = new Product();
p.setName(args[0]);
p.setAmount(Integer.parseInt(args[1]));
p.setPrice(Double.parseDouble(args[2]));
// 2. Fire up Hibernate
Configuration cfg = new Configuration()
.addClass(Product.class);
SessionFactory sf = cfg.buildSessionFactory();
// 3. Open Session
Session sess = sf.openSession();
// 4. Save Product and close Session
Transaction t = sess.beginTransaction();
sess.save(p);
t.commit();
sess.close();
查询
public static void main(String[] args) throws Exception {
// query to issue
String query =
“select product from product ”
+ “in class test.hibernate.Product ”
+ “where product.name=:name”;
// search for what?
String name = args[0];
// init
Configuration cfg = new Configuration()
.addClass(Product.class);
SessionFactory sf = cfg.buildSessionFactory();
// open session
Session sess = sf.openSession();

// search and return
List list = sess.find(query, name,
Hibernate.STRING);
if (list.size() == 0) {
System.out.println(“No products named ”
+ name);
System.exit(0);
}
Product p = (Product) list.get(0);
sess.close();
System.out.println(“Found product: ” + p);
查询
Session session=SessionFactory.currentSession();
//hibernate面对对象的查找语句,不要误解成了sql语句哦!
Query query=session.createQuery(“from User as u where u.name=:username”);
query.setString(“username”,name);
//返回查到的集合
List list=query.list();
System.out.println(“查找结果 :”+list.size());

save=========================
this.getHibernateTemplate().saveOrUpdate(modle)

find=========================
(BoardAuthUser)this.getHibernateTemplate().get(BoardAuthUser.class, id);
——————–
this.getHibernateTemplate().find(LOADS_BY_BID, new Long(bid));
——————–
Object[] o = {postID, userID, new Long(bid)};
List l = this.getHibernateTemplate().find(LOAD_BY_UID_PID_BID, o);
if (l == null || l.isEmpty()) {
return null;
}
else {
return (AgreeAgainst) l.get(0);
}

remove=========================
this.getHibernateTemplate().delete(boardAuthUser);
————————–
Query query = s.createQuery(REMOVE_OUTTIEM);
query.setLong(0, time);
query.executeUpdate();
return null;

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部