java - 如何使用 Hibernate 检索一组成员对象?

标签 java hibernate hibernate-criteria

这个问题跟进我的previous question .我需要检索复杂类的列表。每个都有几组,应该只检索特定数量的组。我已经阅读了这些问题的答案 1 , 2但他们都没有解决我的问题。

我需要找到特定组中和位于特定位置的学生列表,以及他们地址中的电话号码。我还需要显示每个学生到特定坐标的距离。

以下代码工作正常,唯一的问题是我无法检索对象列表,例如每个学生的电子邮件列表、组列表和电话列表。

@Entity
public class Student implements java.io.Serializable {

    private static final long serialVersionUID = -23949494858373847L;
    @Id
    @GeneratedValue
    String id;
    String name;
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "student_groups", joinColumns = { @JoinColumn(name = "id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "groupId", nullable = false, updatable = false) })
    Set<Group> groups = new HashSet<Group>(0);
    ..
}


@Entity
public class Address implements java.io.Serializable {

    private static final long serialVersionUID = -274634747474623637L;
    @Id
    @GeneratedValue
    String addId;
    @Id
    @ManyToOne
    @JoinColumn(name = "id", nullable = false)
    Student student;
    @ManyToOne
    @JoinColumn(name = "locId", nullable = false)
    Location location;
    double latitude;
    double longitude;
    String address;
    @OneToMany(mappedBy = "phoneOwner", fetch = FetchType.EAGER)
    Set<Phone> phones = new HashSet<Phone>();


        String formula = "( 6371 * acos ( cos ( radians("
                + lat
                + ") ) * cos( radians( this_.latitude ) ) * cos( radians( this_.longitude ) - radians("
                + lan + ") ) +" + "sin ( radians(" + lat
                + ") ) * sin( radians( this_.latitude ) ) ) ) as distance";
        Session session = sessionFactory.getCurrentSession();
        ProjectionList pl = Projections
                .projectionList()
                .add(Projections.property("std.id").as("id"))
                .add(Projections.property("std.name").as("name"))
                .add(Projections.property("addr.address").as(
                        "address"))
                .add(Projections.property("location.name").as("location"))
                .add(Projections.property("location.city").as("city"))
                .add(Projections.property("location.latitude").as("latitude"))
                .add(Projections.property("location.longitude").as("longitude"))
                .add(Projections.sqlProjection(formula,
                        new String[] { "distance" },
                        new Type[] { new DoubleType() }));

        List<Students> students = (List<Students) session
                .createCriteria(Address.class, "addr")
                .createAlias("addr.student", "std")
                .createAlias("std.groups", "group")
                .createAlias("addr.location", "location")
                .setProjection(pl)
                .setFetchMode("group", FetchMode.JOIN)
                .add(Restrictions.ilike("group.name", groupName))
                .add(Restrictions.eq("location.id", locId))
                .setResultTransformer(
                        new AliasToBeanResultTransformer(Students.class))
                .list();

最佳答案

这是个好问题。我遇到过类似的问题。所以 AliasToBeanResultTransformer 只能将主对象转换为实体,但它没有选择嵌套对象作为嵌套对象的能力。

要获取嵌套对象,我们应该使用 Custom Transformer。这是一个例子:

https://github.com/madhupathy/Hibernate-Custom-Transformer

在这种情况下,如果没有巨大的性能影响并且我需要几乎所有值,我会避免投影并获取所有对象以保持简单。

关于java - 如何使用 Hibernate 检索一组成员对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29980421/

相关文章:

java - core-ktx 1.7.0 和 Hilt

java - Java Persistence API 中 FetchType LAZY 和 EAGER 的区别?

java - 如何在 Spring boot 1.5.2 中终止 Hibernates 数据库连接?

java - Hibernate 标准 API。加入

java - Lucene Search 不返回任何结果,即使它应该返回

java - 运行多线程应用程序

java - 使用 JAVA 进行图像细化

java - Hibernate - 从单向 OneToMany 关系中获取集合的 HQL

grails - Grails:如何最好地构造一个休眠条件构建器来搜索与域实例的 'hasMany'关系

使用枚举类型的 grails createcriteria 过滤