hibernate中查询排序

查询排序:(内存排序与数据库排序)

推荐使用数据库排序

package model;

public class Student {
	private String id;
	
	private String card_id;
	
	private String name;
	
	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 getCard_id() {
		return card_id;
	}

	public void setCard_id(String cardId) {
		card_id = cardId;
	}

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

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


package model;



import java.util.HashMap;
import java.util.Map;





public class Team {
	private String id;
	
	private String teamName;
	
	private Map students = new HashMap ();
	

	public Map getStudents() {
		return students;
	}

	public void setStudents(Map students) {
		this.students = 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;
	}	
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name = "model.Team" table = "team" lazy="true">
		<id name="id" type = "string">
			<generator class="uuid"></generator>
		</id>
		<property name="teamName">
			<column name="teamName" length="20"></column>
			<type name="string"></type>
		</property>
		<map name="students" table = "student" order-by="name desc">
			<key column = "team_id"></key>
			<index column="name" type = "string"></index>
			<element column = "description" type = "string"></element>
		</map>
	</class>
</hibernate-mapping>
package model;


import java.util.Collection;
import java.util.Iterator;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class TEST {
	public static void main(String[] args) {
		//save();
		fetch();
	}
	static void save () {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			
			
			Team team1 = new Team ();
			team1.setTeamName("team1");
			
			team1.getStudents().put("zhangsan", "worker");
			team1.getStudents().put("lisi", "cook");
			team1.getStudents().put("wangwu", "teacher");
			session.save(team1);
			tx.commit();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 
	 * result:Hibernate: select team0_.id as id0_, team0_.teamName as teamName0_ from team team0_ where team0_.teamName='team1'
Hibernate: select students0_.team_id as team1_0_, students0_.description as descript2_0_, students0_.name as name0_ from student students0_ where students0_.team_id=? order by students0_.name desc
worker
teacher
cook

	 */
	static void fetch () {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Team team = (Team) session.createQuery("from Team t where t.teamName = 'team1'").uniqueResult();
			Collection c = team.getStudents().values();
			Iterator iter = c.iterator();
			while (iter.hasNext()) {
				System.out.println(iter.next());
			}
			tx.commit();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}


内存排序使用sort属性,它有三个属性值(unsorted,natural),其中的natural值的是按照自然的升序排序。第三个属性值是我们自己
定义的排序规则类,方法时定义一个类,让其实现Comparator接口,并且实现该接口中的compare方法,在该方法中实现排序规则即
可。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值