简体   繁体   English

如何检索已添加到集合中的最新记录?

[英]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. 假设studentidauto 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.

相关问题 如何获取Firebase中推送给孩子的最新数据? - How to retrieve the latest data that has been pushed to a child in Firebase? 如何将 setOnClickListener 设置为以编程方式添加的 TextView? - How to set setOnClickListener to TextView that has been added programmatically? 有没有办法为添加了鼠标适配器的东西设置动作命令? - Is there a way to set an action command for something that a mouse adapter has been added to? 这是一个图书馆还是已添加手动? - Is this a library or has been added manullay? 如何在JLabel的JPanel中动态添加getLocation()? - How to getLocation() in JPanel of JLabel that has been dynamically added? 如何使用 springboot 检查用户是否已添加到 LDAP 组 - How to check if the user has been added to LDAP group using springboot 如何对本地添加的flutter插件进行更改? - How to make changes to a flutter plugin that has been added locally? 如何检查是否没有将任何内容添加到对象数组中 - How to check if nothing has been added to an array of objects 如何检测文件中的字符串是否已被编辑或添加? - How to detect if a string in a file has been edited or added? 在运行时添加组件后如何使 JScrollPane 可滚动 - How to make a JScrollPane scrollable after a component has been added at runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM