13.3.2 SortedSet Implementation
We can use SortedSet interface with TreeSet implementation if we want to have the sert sorted. File class has to implement Comparable interface in order to get it added in TreeSet
In a <set> tag we can use sort attribute to
- specify the Comparator class which will be used to sort the set when the files will be retrieved.
- Instead of specifying comparator we can use “natural” in which compareTo method will be used for sorting.
Folder.java
package com.tutorial.hibernate; import java.util.SortedSet; import java.util.TreeSet; public class Folder { private int folderId; private String folderName; private SortedSet<File> files = new TreeSet<File>(); public int getFolderId() { return folderId; } public void setFolderId(intfolderId) { this.folderId = folderId; } public String getFolderName() { return folderName; } public void setFolderName(String folderName) { this.folderName = folderName; } public SortedSet<File>getFiles() { return files; } public void setFiles(SortedSet<File> files) { this.files = files; } }
Component-mapping.hbm.xml
We are using ReverseFileComparator which will sort the files in reverse order of names.
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.tutorial.hibernate.Folder" table="Folder"> <id name="folderId" type="int" column="folder_id"> <generator class="native"/> </id> <property name="folderName" column="name" type="string"/> <set name="files" table="Files" sort="com.tutorial.hibernate.ReverseFileComparator"> <key column="folderIdForeignKey"></key> <composite-element class="com.tutorial.hibernate.File"> <property name="name" type="string" column="file_name"/> <property name="size" type= "int" column="file_size"/> <property name="extension" type="string" column="file_ext"/> </composite-element> </set> </class> </hibernate-mapping>
package com.tutorial.hibernate; public class File implements Comparable<File>{ private String name; private int size; private String extension; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public String getExtension() { return extension; } public void setExtension(String extension) { this.extension = extension; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((extension == null) ? 0 : extension.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + size; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; File other = (File) obj; if (extension == null) { if (other.extension != null) return false; } else if (!extension.equals(other.extension)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (size != other.size) return false; return true; } @Override public String toString() { return "File [name=" + name + ", size=" + size + ", extension=" + extension + "]"; } @Override public int compareTo(File file) { return getName().compareTo(file.getName()); } }
ReverseFileComparator.java
package com.tutorial.hibernate; import java.util.Comparator; public class ReverseFileComparator implements Comparator<File>{ @Override public int compare(File f1, File f2) { return f1.getName().compareTo(f2.getName()) * -1; } }
Test Program
import com.tutorial.hibernate.File; import com.tutorial.hibernate.Folder; import java.util.SortedSet; import java.util.TreeSet; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class Test { public static void main(String args[]) { Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactory(); Session session= factory.openSession(); Transaction tx = session.beginTransaction(); Folder folder = new Folder(); folder.setFolderName("folder1"); SortedSet<File> files = new TreeSet<File>(); File f1= new File(); f1.setExtension(".txt"); f1.setName("sample"); f1.setSize(102); File f2= new File(); f2.setExtension(".xls"); f2.setName("data"); f2.setSize(1024); files.add( f1); files.add(f2); folder.setFiles(files); session.save(folder); tx.commit(); session.close(); session= factory.openSession(); Query q = session.createQuery("from Folder"); Folder f =(Folder) q.uniqueResult(); System.out.println(f.getFiles()); session.close(); factory.close(); } }
Run Program
If we change the sort to “natural”