Hibernate memory Sort

Source: Internet
Author: User
Tags uuid
Memory sort
The only difference between memory sorting and database sorting is the use of the Sort property in the configuration file instead of the Order-by property
He has three attribute values (unsorted,natural), where natural refers to the sort in ascending order of nature, and the third
The property value is our own defined collation class, which implements the comparator interface and implements the
The Compare method in the interface, in which the collation is defined. The custom collation is then
The class masterpiece is the property value of sort.

Specific reference:

Team.java

Package com.fgh.hibernate;

Import Java.util.Set;

public class Team {

	private String ID;

	Private String teamname;

	Private set<student> students;

	Public String GetId () {return
		ID;
	}

	public void SetId (String id) {
		this.id = ID;
	}

	Public String Getteamname () {return
		teamname;
	}

	public void Setteamname (String teamname) {
		this.teamname = teamname;
	}

	Public set<student> getstudents () {return
		students;
	}

	public void Setstudents (set<student> students) {
		this.students = students;
	}
}

Student.java

Package com.fgh.hibernate;

/**
 * One-to-one Two-way Association
 * 
 * @author FGH
 * 
 *
/public class Student {

	private String ID;

	private String name;

	Private String Idcard;

	private int age;

	Private Team team;

	Public Team Getteam () {return team
		;
	}

	public void Setteam (Team team) {
		this.team = team;
	}

	Public String GetId () {return
		ID;
	}

	public void SetId (String id) {
		this.id = ID;
	}

	Public String GetName () {return
		name;
	}

	public void SetName (String name) {
		this.name = name;
	}

	Public String Getidcard () {return
		idcard;
	}

	public void Setidcard (String idcard) {
		this.idcard = Idcard;
	}

	public int getage () {return age
		;
	}

	public void Setage (int age) {
		this.age = age;
	}

}

Team.hbm.xml

<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping public
        "-//hibernate/hibernate mapping DTD 3.0//en"
        "http:// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

Student.hbm.xml

<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping public
        "-//hibernate/hibernate mapping DTD 3.0//en"
        "http:// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

Custom Collation class:

Mycomparator.java

Package com.fgh.hibernate;

Import Java.util.Comparator;

/**
 * Custom Collation Class * * 
 @author FGH * */Public
class Mycomparator implements comparator< student> {public

	int compare (Student s1, Student S2) {
		//If two objects are the same, do not sort if
		(S1 = s2) {return
			0;
  
   
		int result = S1.getidcard (). Comparetoignorecase (S2.getidcard ());
		If Cardid is different, sort by cardid if
		(Result!= 0) {
			//if descending return-result;
			return result;
		}
		If Cardid is the same, sort by name
		//If descending returns a negative return
		s1.getname (). Comparetoignorecase (S2.getname ());
	}



  

Test class:

Hibernatetest.java

Package com.fgh.hibernate;
Import Java.util.HashSet;
Import Java.util.Iterator;

Import Java.util.Set;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;

Import org.hibernate.cfg.Configuration;

	public class Hibernatetest {private static sessionfactory sessionfactory;

		static {try {sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();
		catch (Exception e) {e.printstacktrace (); } public static void Main (string[] args) {//insert operation first have data to query OH//------------begin--------------Team Team
		= new Team ();
		Team.setteamname ("team1");

		Team.setstudents (New hashset<student> ());
		Student Student = new Student ();
		Student.setname ("Zhangsan");
		Student.setidcard ("AA");

		Student.setage (20);
		Student student1 = new Student ();
		Student1.setname ("Lisi");
		Student1.setidcard ("AA");

		Student1.setage (21);
		Student Student2 = new Student ();
		Student2.setname ("Wangwu"); Student2. Setidcard ("ba");

		Student2.setage (22);
		Student Student3 = new Student ();
		Student3.setname ("Xiaoqiang");
		Student3.setidcard ("BB");

		Student3.setage (23);
		Student student4 = new Student ();
		Student4.setname ("Xiaohong");
		Student4.setidcard ("DC");

		Student4.setage (24);
		Team.getstudents (). Add (student);
		Team.getstudents (). Add (Student1);
		Team.getstudents (). Add (Student2);
		Team.getstudents (). Add (STUDENT3);

		Team.getstudents (). Add (STUDENT4);
		Student.setteam (team);
		Student1.setteam (team);
		Student2.setteam (team);
		Student3.setteam (team);
		Student4.setteam (team);
		-------------End------------Session session = Sessionfactory.opensession ();

		Transaction tx = NULL;
			try {tx = Session.begintransaction ();

			Query----Bein--------Team TEAM2 = (team) session.get (Team.class, "8a8ae4d934b2bfa10134b2bfa4a30001");

			set<student> set = Team2.getstudents (); for (iterator<student> iterator = Set.iterator (); iterator. Hasnext ();)
	{			System.out.println (Iterator.next (). GetName ());

			//--------------End----------//Save//Session.save (team);

		Tx.commit ();
			catch (Exception e) {e.printstacktrace ();
			if (null!= tx) {tx.rollback ();
		finally {session.close ();
 }
	}
}
Print sql:

Hibernate:select team0_.id as id1_0_, Team0_.teamname as teamname1_0_ from Team team0_ where team0_.id=?
Hibernate:select students0_.team_id as team5_1_, students0_.id as id1_, students0_.id as id0_0_, students0_.name as NAME0 _0_, Students0_.idcard as idcard0_0_, students0_.age as age0_0_, students0_.team_id as team5_0_0_ from student students0_ where students0_.team_id=?
Lisi
Zhangsan
Wangwu
xiaoqiang
Xiaohong

Oh also, finish


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.