hibernate中QBC查询

package dao;

import domain.AccountEntity;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import javax.persistence.criteria.CriteriaQuery;

import java.util.List;

import static org.junit.Assert.*;

/**
 * @Author: David.Xiao
 * @Date: 2018/11/7 21:15
 * @Description: QBC查询是一种更加面向对象的查询方式,把生成语句的过程全都融入方法之中
 */
public class QbcUtilTest {
    /**
     * 基本查询
     */
    @Test
    public void queryAllByQbc()
    {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();
        CriteriaQuery criteriaQuery = session.getCriteriaBuilder().createQuery(AccountEntity.class);
        criteriaQuery.from(AccountEntity.class);

        List list = session.createQuery(criteriaQuery).getResultList();
        for (Object object:list) {
            System.out.println(object.toString());
        }
        tx.commit();
    }
}

 

你可能感兴趣的:(JAVA,Hibernate)