ITeye博客

hibernate中的增删改查实现代码

jil5 · 2012-08-29 00:03 · hibernate · 非技术 · Markdown

@GeneratedValue自动主键

 

在hibernate中的增删改查的实现。hibernate是OR框架,也就是对象关系框架,有了 hibernate我们就不用再去写SQL语言,我们只需要操纵对象去进行增删改查。这里今天写的就是在如何应用hibernate实现增删改查。

第一个我们首先看看增,增在SQL里面就是insert,也就是插入,在hibernate中,我们只需要,操纵一个对象进行sava,然后再commit事务,就能实现插入功能,下面给大家具体看看代码,持久类我就不再写了,里面也就是与数据库中的字段要一一对应的东西,要有set,get方法,我直接就写的怎么调用save方法。 

//导入所需的包 

import org.hibernate.HibernateException;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import com.hibernate.model.Student;

public class StudentInsertTest {

public static void main(String args[]) {

Configuration cfg = new Configuration().configure(); // 获取hibernate的配置信息

SessionFactory sf = cfg.buildSessionFactory(); // 根据config建立sessionFactory

Session ses = sf.openSession(); // factory用于建立session,开启Session,相当于开启JDBC的Connection

Transaction ts = ses.beginTransaction(); // 创建事务的对象ts

Student student = new Student(); // 持久化对象

student.setName("kobe");

student.setAge(11);

try {

ses.save(student);

ts.commit();

} catch (HibernateException he) {

he.printStackTrace();

ts.rollback();

} finally {

ses.close();

sf.close();

System.out.println("插入成功");

}

}

}

第二个我们看看删,删在SQL里面是delete,也就是删除,同样在hibernate中,我们也是只需要调用一个对象,调用delete方法,就能进行删除。 

import org.hibernate.HibernateException;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import com.hibernate.model.Student;

public class StudentDeleteTest {

public static void main(String args[]) {

Configuration cfg = new Configuration().configure();

SessionFactory sf = cfg.buildSessionFactory();

Session ses = sf.openSession();

Transaction ts = ses.beginTransaction();

Student student = new Student();

student.setId(0);

try {

ses.delete(student);

ts.commit();

} catch (HibernateException he) {

he.printStackTrace();

ts.rollback();

} finally {

ses.close();

sf.close();

System.out.println("删除成功");

}

}

}

具体中间的含义参照sava方法,这里我们要注意一点,我们调用删除的时候,他删除的条件,也就是where后面的条件一定是我们xml中配置id,通过这个来进行查找删除,这里尤其值得注意,也就是,我这里调用的user.setId(" ");这句话,他是通过""中的内容进行删除的。 
第三个我们看看改,改在SQL中update,在hibernate中,我们同样只需要操作一个对象进行更改信息。 

import org.hibernate.HibernateException;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import com.hibernate.model.Student;

public class StudentUpdateTest {

public static void main(String args[]) {

Configuration cfg = new Configuration().configure();

SessionFactory sf = cfg.buildSessionFactory();

Session ses = sf.openSession();

Transaction ts = ses.beginTransaction();

Student student = new Student();

student.setId(1);

student.setName("kobe855");

student.setAge(50);

try {

ses.update(student);

ts.commit();

} catch (HibernateException he) {

he.printStackTrace();

ts.rollback();

} finally {

ses.close();

sf.close();

System.out.println("更改成功");

}

}

}

但是这里我们有需要注意的地方了,如果有的朋友用过这个update就会发现,调用这个方法的时候他更新的不只是你想更新的数据,你不想更新的数据,他也会随着改变,如果你没有给他set值,他就会出现null,或者表格中什么都没有,这里我们就需要用另一种方法了,去更新你想更新的数据,而你不想改变的数据还会保持原来的状态,这里我们就需要调用一个方法。 

Session ses = sf.openSession();

Transaction ts = ses.beginTransaction();

Student student = (Student)ses.get(Student.class,1); //1是主键ID 第二个参数

student.setName("kobe299");

// student.setAge(50);

try {

ses.update(student);

ts.commit();

这样我们就会发现,我们只更新了我们想要更新的数据。ses不光光有这一个get方法,相同功能他还有一个load方法,两个方法功能是相同的但是有什么区别呢,区别就是用load方法时候他是从缓存中查找,而我们调用get方法的时候是从数据库中查找,不过get方法他也是先从缓存中查找,如果没有在去数据库中查找。 
第三个我们看看查,查在SQL中是select,在hibernate中我们查询的时候有多种方法,这里我就写一种hibernate比较提倡的方法,就是HQL。用这个方法时候我们尤其需要注意的是他其中的from跟的不是表名,而是类名。 

import java.util.Iterator;

import java.util.List;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import com.hibernate.model.Student;

public class StudentSelectTest {

public static void main(String args[]) {

Configuration cfg = new Configuration().configure();

SessionFactory sf = cfg.buildSessionFactory();

Session ses = sf.openSession();

Transaction tx = ses.beginTransaction();

Student student = new Student();

Query query = ses.createQuery("from Student");

List students = query.list(); // 序列化

Iterator it = students.iterator(); // 迭代

while (it.hasNext()) {

student = (Student) it.next();

System.out.println(student.getName() + " " + student.getAge() + " ");

}

ses.close();

sf.close();

}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。