public class HibernateTest {
@Test
public void test(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
Customer customer = session.get(Customer.class, 1L);
Customer customer2 = session.get(Customer.class, 4L);
System.out.println(customer);
System.out.println(customer2);
transaction.commit();
HibernateUtils.close(session);
}
}
public class OneToManyTest {
@Test
//测试对象导航查询
public void test1(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
Linkman linkman = session.get(Linkman.class, 1L);
System.out.println(linkman.getLkm_name());
Customer customer = linkman.getCustomer();
System.out.println(customer.getCust_name());
transaction.commit();
session.close();
}
@Test
//测试对象导航查询
public void test2(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
Customer customer = session.get(Customer.class, 1L);
System.out.println(customer.getCust_name());
//查询与该customer相关的linkman
Set linkmans = customer.getLinkmans();
for(Linkman man : linkmans){
System.out.println(man.getLkm_name());
}
transaction.commit();
session.close();
}
}
public class HQLHibernate {
//排序查询
@Test
public void test07(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
//Query代表面向对象的hibernate操作,createQuery创建查询对象
Query query = session.createQuery("FROM Customer ORDER BY cust_name DESC");
List
QBC方式也叫标准查询:
public class QBCHibernate {
//离线查询
@Test
public void test06(){
//在service/web层凭空创建Criteria对象
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
//拼装sql查询条件
detachedCriteria.setProjection(Projections.property("cust_name"));
detachedCriteria.add(Restrictions.gt("cust_id",3L));
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
//获得可被执行的criteria,把离线变成在线
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
List list = criteria.list();
for(Object object : list){
System.out.println(object);
}
transaction.commit();
HibernateUtils.close(session);
}
//聚合查询
@Test
public void test05(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
criteria.setProjection(Projections.avg("cust_id"));
Object o = criteria.uniqueResult();
System.out.println(o);
transaction.commit();
HibernateUtils.close(session);
}
//分页查询
@Test
public void test04(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
criteria.setFirstResult(0);
criteria.setMaxResults(3);
List list = criteria.list();
for(Object object : list){
Customer customer = (Customer) object;
System.out.println(customer.getCust_id()+" "+customer.getCust_name());
}
transaction.commit();
HibernateUtils.close(session);
}
//条件查询
@Test
public void test03(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
criteria.add(Restrictions.like("cust_name","%阿里%"));
List list = criteria.list();
for(Object object : list){
Customer customer = (Customer) object;
System.out.println(customer.getCust_id()+" "+customer.getCust_name());
}
transaction.commit();
HibernateUtils.close(session);
}
//查询部分字段(投影)
@Test
public void test02(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
criteria.setProjection(Projections.property("cust_name"));
List list = criteria.list();
for(Object object : list){
System.out.println(object);
}
transaction.commit();
HibernateUtils.close(session);
}
//查询全部字段
@Test
public void test01(){
Session session = HibernateUtils.openSession();
Transaction transaction = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
List list = criteria.list();
for(Customer customer : list){
System.out.println(customer.getCust_id()+" "+customer.getCust_name());
}
transaction.commit();
HibernateUtils.close(session);
}
}
一直没用过SQL方式,SQL方式参考这篇文章:
Hibernate SQLQuery原生sql查询https://blog.csdn.net/hjiacheng/article/details/53717727