��web�����£�hibernate�����ļ���Ҫ���ã���ݿ������
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
hibernate�IJ�ѯ��ʽ
1��hql(hibernate query language) query
2��Criteria query
3��Native query
�ܽ
1�����ҳ����Ҫ��ʾ����ݺ���ݿ��е���������Զ�����ô�select�Ĺ��������в�ѯ�DZȽϺõķ���
2�����ҳ����Ҫ��ʾ����ݺ���ݿ��е����������Զ�����ʱ������������
3���������������ӣ�from�������Ǹ�������ǽṹ����
4�������ű���в�ѯ���Һ��ı�
public class HQLDao extends HiberanteUtils{ public static HQLDao getInstanse() { return new HQLDao(); } /** * ���� * * һ�Զ�Ͷ�Զ� * * ���Ľ�� * @author Think * */ public List<Classess> queryAllClasses(){ Session session = sessionFactory.openSession(); List<Classess> cList = session.createQuery("from Classess").list(); session.close(); return cList; } public List queryClasses_Properties(){ Session session = sessionFactory.openSession(); List cList = session.createQuery("select cid,cname from Classess").list(); session.close(); return cList; } public List<Classess> queryClasses_Constructor(){ Session session = sessionFactory.openSession(); List<Classess> cList = session.createQuery("select new cn.itcast.domain.Classess(cname,description) from Classess").list(); session.close(); return cList; } public Classess queryClasses_Condition(){ Session session = sessionFactory.openSession(); Query query = session.createQuery("select new cn.itcast.domain.Classess(cname,description) from Classess where cid=:cid"); query.setLong("cid", 1L); Classess classes = (Classess)query.uniqueResult(); System.out.println(classes.getCname()); session.close(); return classes; } public Classess queryClasses_Condition_2(){ Session session = sessionFactory.openSession(); Query query = session.createQuery("select new cn.itcast.domain.Classess(cname,description) from Classess where cid=?"); query.setLong(0, 1L); Classess classes = (Classess)query.uniqueResult(); System.out.println(classes.getCname()); session.close(); return classes; } /** * order by,group by,sun,min,max,avg,having�ȶ����� * @return */ /** * �Ӳ�ѯ */ public void queryClasses_SubSelect(){ Session session = sessionFactory.openSession(); List<Classess> cList = session.createQuery("from Classess where cid in(select cid from Classess where cid in(1,2,3))").list(); session.close(); } public static HQLDao getInstance(){ return new HQLDao(); } /*********************************************************************************************/ /** * һ�Զ� * ��ֵ���� ��ѯ�����Ļ�ܲ� * ������ * �������� * ������������ */ public List<Classess> queryClasses_Student_EQ(){ Session session = sessionFactory.openSession(); List<Classess> cList = session.createQuery("from Classess c,Student s where c.cid=s.classess.cid").list(); session.close(); return cList; } /** * ������ * @return */ public List<Classess> queryClasses_Student_INNER(){ Session session = sessionFactory.openSession(); List<Classess> cList = session.createQuery("from Classess c inner join c.students").list(); session.close(); return cList; } /** * ���������� * @return */ public List<Classess> queryClasses_Student_INNER_FETCH(){ Session session = sessionFactory.openSession(); List<Classess> cList = session.createQuery("from Classess c inner join fetch c.students").list(); session.close(); return cList; } /** * �������� */ public List<Classess> queryClasses_Student_LeftJoin(){ Session session = sessionFactory.openSession(); List<Classess> cList = session.createQuery("from Classess c left outer join c.students").list(); session.close(); return cList; } /** * ������������ */ public List<Classess> queryClasses_Student_LeftJoin_fetch(){ Session session = sessionFactory.openSession(); String hql = "from Classess c left outer join fetch c.students"; hql = "from Student s left outer join fetch s.classess c"; List<Classess> cList = session.createQuery(hql).list(); session.close(); return cList; } /** * ��select�IJ�ѯ */ //public List<Classess> queryClasses_Student_Select(){ //Session session = sessionFactory.openSession(); //String hql = "select new cn.itcast.domain.ClassesView(c.cname,s.sname) " + // "from Student s left outer join s.classes c"; //List<Classess> cList = session.createQuery(hql).list(); //session.close(); //return cList; //} /** * ��Զ� */ public void testQueryCourse_Student(){ Session session = sessionFactory.openSession(); List<Student> studentList = session.createQuery("from Student s inner join fetch s.courses c").list(); session.close(); } /** * һ�Զ��϶�Զ� */ public List<Student> queryClasses_Student_Course(){ Session session = sessionFactory.openSession(); String hql = "from Classes cs inner join fetch cs.students s inner join fetch s.courses c"; hql = "from Student s inner join fetch s.classes cs inner join fetch s.courses c"; //hql = "from Classes cs left outer join fetch cs.students s left outer join fetch s.courses c"; List<Student> cList = session.createQuery(hql).list(); //Set<Classes> cset = new HashSet<Classes>(cList); //cList = new ArrayList<Classes>(cset); System.out.println(cList.size()); //for(Classes classes:cList){ //System.out.println(classes.getCid()); //Set<Student> students = classes.getStudents(); //for(Student student:students){ //System.out.println(student.getSname()); //Set<Course> courses = student.getCourses(); //for(Course course:courses){ //System.out.println(course.getCname()); //} //} //} session.close(); return cList; } }