HibernateUtil:
package hibernate.util;
import java.io.Serializable;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import com.zxf.model.Student;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
public static SessionFactory buildSessionFactory() {
if(sessionFactory != null) {
return sessionFactory;
} else {
try {
return new AnnotationConfiguration().configure().buildSessionFactory();
}
catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void add(Object o) {
Session session = null;
try {
session = sessionFactory.openSession();
session.beginTransaction();
session.save(o);
session.getTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
session.close();
}
}
public static void update(Object o) {
Session session = null;
try {
session = sessionFactory.openSession();
session.beginTransaction();
session.update(o);
session.getTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
session.close();
}
}
public static void delete(Object o) {
Session session = null;
try {
session = sessionFactory.openSession();
session.beginTransaction();
session.delete(o);
session.getTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
session.close();
}
}
public static Object get(Class className,Serializable id) {
Session session = null;
Object o = null;
try {
session = sessionFactory.openSession();
o = session.get(className, id);
} catch (HibernateException e) {
e.printStackTrace();
return null;
} finally {
if(session != null) {
session.close();
}
}
return o;
}
}
HQL:
import hibernate.util.HibernateUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;
import com.zxf.model.Student;
public class HQLTest {
@Test
public void query01() {
Query query = null;
Session session = null;
List<Student> students = null;
String hql = "from Student as user where user.name=?";
session = HibernateUtil.buildSessionFactory().openSession();
query = session.createQuery(hql);
query.setString(0, "teeny");
students = query.list();//query.uniqueResult();唯一记录
for(Student s:students) {
System.out.println(s.getName() + "----" + s.getAge());
}
}
}
Critera:
//Critera查询
@Test
public void query04() {
Criteria cri = null;
Session session = null;
List<Student> students = null;
try {
session = HibernateUtil.buildSessionFactory().openSession();
cri = session.createCriteria(Student.class);
cri.add(Restrictions.eq("name", "jason"));//添加对于Student对象的约束条件
cri.add(Restrictions.eq("age",20));
students = cri.list();//cri.uniqueResult();唯一记录
for(Student s:students) {
System.out.println(s.getName() + "----" + s.getAge());
}
} catch (HibernateException e) {
e.printStackTrace();
} finally {
if(session != null) {
session.close();
}
}
}
对象名与关键字冲突:
<hibernate-mapping package="com.zxf.model">
<class name="Student" table="Student">
<id name="id"></id>
<property name="name"/>
<property name="age"/>
</class>
</hibernate-mapping>
table名冲突,比如User在sqlserver和oracle里面都是关键字,可以指定配置中table属性重新指定一个table,或者是用``将表名括起来,这样数据库就不会把它当关键字对待,而是看做普通字符串,如下:
<hibernate-mapping package="com.zxf.model">
<class name="Student" table="`Student`">
<id name="id"></id>
<property name="name" column="`name`"/>
<property name="age"/>
</class>
</hibernate-mapping>