[英]How to retrieve the latest record that has been added to a set?
I have a class called MyClass which keeps a set of student objects as member. 我有一个名为MyClass的类,该类保留一组学生对象作为成员。
Currently I can pass the id of required student and retrieve the object but not sure how to retrieve the latest one who has been added. 目前,我可以传递所需学生的ID并检索对象,但不确定如何检索已添加的最新对象。
I am wondering how I can change the code to retrieve the object of last student that was added to the class? 我想知道如何更改代码以检索添加到班级的最后一个学生的对象?
"I know set does not maintain order, is there any method to retrieve the latest one using hibernate or by comparing the enrolledDate members of student class?" “我知道set不能保持顺序,是否有任何方法可以使用hibernate或通过比较学生班级的enrolledDate成员来检索最新的方法?”
MyClass 我的课
import java.util.Set
@Entity
public class MyClass {
@Id
@GeneratedValue
private long id;
@OneToMany
@LazyCollection(LazyCollectionOption.FALSE)
private Set<Student> students;
....
}
Student class 学生班
@Entity
public class Student {
@Id
@GeneratedValue
private long id;
private String name;
@Temporal(javax.persistence.TemporalType.DATE)
private Date enrolledDate; //I may need to use this date as it is enrollment date but not sure how to retrieve the latest one.
}
Hibernate Code 休眠代码
MyClass myClass = (MyClass) session.get(MyClass.class, id);
Iterator<MyClass> it = myClass.getStudents().iterator();
Student stu = new Student();
while (it.hasNext()) {
stu = it.next();
if (stu.getId() == stuId) {
break;
}
}
System.err.println("Student name is:" + stu.getName());
assuming that the id
of student
is auto increment
you can use TreeSet
so that the recent Student
will be right most element in the TreeSet
(write your compareTo()
on top of id
).You can use last()
of TreeSet
which will give you the latest. 假设
student
的id
是auto increment
您可以使用TreeSet
以便最近的Student
将是TreeSet
最右边的元素compareTo()
在id
上写您的compareTo()
)。您可以使用TreeSet
last()
最新的。
Even instead of id
you can use anything that is ascending order (write compareTo() accordingly ),and put student
into TreeSet
and fetch last()
ie recent student
object. 即使使用
id
也可以使用任何升序(相应地编写compareTo()),然后将student
放入TreeSet
并获取last()
即最近的student
对象。
Hope it helps. 希望能帮助到你。
Okay a Set (or some other collection) in Hibernate can be ordered in one of two ways: using @Sort (in-memory sort) or using @OrderBy (database sort). 好的,可以通过以下两种方式之一对Hibernate中的Set(或其他集合)进行排序:使用@Sort(内存中排序)或使用@OrderBy(数据库排序)。
As @OrderBy cannot be used on nested properties (eg myclass.student.dateEnrolled) you will need to use @Sort. 由于不能在嵌套属性(例如myclass.student.dateEnrolled)上使用@OrderBy,因此需要使用@Sort。
http://docs.jboss.org/hibernate/core/3.5/javadocs/org/hibernate/annotations/Sort.html http://docs.jboss.org/hibernate/core/3.5/javadocs/org/hibernate/annotations/Sort.html
http://eubauer.de/kingsware/2011/03/15/ordering-collections-with-jpa/ http://eubauer.de/kingsware/2011/03/15/ordering-collections-with-jpa/
All you need to do then is: 然后,您需要做的就是:
@OneToMany
@Sort(...)
private Set<Student> students; //note leave as Set
If the types in the Set implement Comparable and this is the Order you want you can specify SortType.NATURAL. 如果Set中的类型实现Comparable,并且这是您想要的Order,则可以指定SortType.NATURAL。 Otherwise you can specify a Comparator to handle the sorting.
否则,您可以指定一个Comparator来处理排序。
Hibernate will ensure the Set passed in is sorted. Hibernate将确保对传入的Set进行排序。 If we assume this Set implements the NavigableSet Interface (and you would need to check this) then you can use the methods of that Interface to find the latest:
如果我们假设此Set实现了NavigableSet接口(并且您需要进行检查),则可以使用该接口的方法来查找最新的方法:
http://docs.oracle.com/javase/6/docs/api/java/util/NavigableSet.html http://docs.oracle.com/javase/6/docs/api/java/util/NavigableSet.html
Note that if there were a large number of students and you only ever wanted the latest then this is a sub-optimal solution as all Students will be loaded. 请注意,如果有大量学生,而您只想要最新的,那么这是次佳的解决方案,因为将加载所有学生。
Set不能维持顺序,您可以尝试其他Collections,例如LinkedHashSet,它以插入顺序存储元素,或者可以通过PriorityQueue从两端进行访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.