3:29 am - Tuesday November 13, 2018

Hibernate CreateQuery OrderbyAsc in Eclipse

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

•  This explains how to fetch data’s in ascending order.
•  createQuery(“from Student stu where stu.id > 2 ORDER BY stu.stuid ASC”);
•  ASC – implies ascending order.

Project Structure:

 

 

 

 

 

 

 

 

 

 

 

Jar Files:

 

 

 

 

 

 

 

 

 

 

 

 

ASCorderbyCreateQuery.java:

package com.candidjava;

import java.util.*;

public class ASCorderbyCreateQuery {
	public static void main(String args[]) {
		try {
			ASCorderbyCreateQueryControl ctrl = new ASCorderbyCreateQueryControl();
			List li = ctrl.param(args);
			for (Iterator iter = li.iterator(); iter.hasNext();) {
				Student stu = (Student) iter.next();

				System.out.println("ID: " + stu.getId());
				System.out.println("StuId: " + stu.getStuid());
				System.out.println("StuRegNo: " + stu.getSturegno());
				System.out.println("StuName: " + stu.getStuname());
				System.out.println("StuMark1: " + stu.getStumark1());
				System.out.println("StuMark2: " + stu.getStumark2());

				System.out.println("Degree: " + stu.getDegree());
				System.out.println("Phone: " + stu.getMobileno());
				System.out.println("Mailid: " + stu.getMailid());
				System.out.println("");

				System.out.println("");
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}

ASCorderbyCreateQueryControl.java:

package com.candidjava;

import java.util.*;
import java.io.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.Transaction;

public class ASCorderbyCreateQueryControl {
	private static SessionFactory sessionFactory;

	private Session getSession() {
		Session s = null;
		try {
			sessionFactory = new Configuration().configure(
					"com\\xml\\hibernate.cfg.xml").buildSessionFactory();
			s = sessionFactory.openSession();
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
		}
		return s;
	}

	public List param(String[] args) throws Exception {
		List list = null;
		try {
			Session s = getSession();

			Query query = s
					.createQuery("from Student stu where stu.id > 2 ORDER BY stu.stuid ASC");

			list = query.list();
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
		}
		return list;
	}
}

Student.java:

package com.candidjava;

import java.io.*;

public class Student implements Serializable {
	private long id;
	private String stuid;
	private String sturegno;
	private String stuname;
	private String stumark1;
	private String stumark2;
	private String degree;
	private String mobileno;
	private String mailid;

	public long getId() {
		return id;
	}

	public String getStuid() {
		return stuid;
	}

	public String getSturegno() {
		return sturegno;
	}

	public String getStuname() {
		return stuname;
	}

	public String getStumark1() {
		return stumark1;
	}

	public String getStumark2() {
		return stumark2;
	}

	public String getDegree() {
		return degree;
	}

	public String getMobileno() {
		return mobileno;
	}

	public String getMailid() {
		return mailid;
	}

	public void setId(long string) {
		id = string;
	}

	public void setStuid(String string) {
		stuid = string;
	}

	public void setSturegno(String string) {
		sturegno = string;
	}

	public void setStuname(String string) {
		stuname = string;
	}

	public void setStumark1(String string) {
		stumark1 = string;
	}

	public void setStumark2(String string) {
		stumark2 = string;
	}

	public void setDegree(String string) {
		degree = string;
	}

	public void setMobileno(String string) {
		mobileno = string;
	}

	public void setMailid(String string) {
		mailid = string;
	}

}

Web.xml:

<!--?xml version="1.0" encoding="UTF-8"?-->

	CreateQueryOrderbyASC

		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp

hibernate.cfg.xml:


			oracle.jdbc.OracleDriver

			jdbc:oracle:thin:@localhost:1521:XE

			system

			system

		5
		<!-- SQL dialect -->

			org.hibernate.dialect.OracleDialect

		<!-- Echo all executed SQL to stdout -->
		false
		update

Student.hbm.xml:

<!--?xml version="1.0"?-->

OUTPUT:

    

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 DOWNLOAD+SOURCECODE


BACK

Filed in: Z-A Hibernate codes

No comments yet.

Leave a Reply