1

I have a generic criteria query and it returns same records. I think there is something wrong with my student save method. Here is my save method;

    Student student = new Student();
    student.setId(Utility.generateUUID());
    student.setClassroom(selectedClassroom);
    student.setUrl(urlAddress);
    genericService.save(student);

When I try to get all Classrooms from datatable it returns 3 Classroom object which are same but there is only one record in Classroom table. The Problem is there are 3 student records which Classrooms are referencing to this classroom record.

My criteria query;

    @Transactional(readOnly = true)
public <T> List<T> getByTemplate(T templateEntity) {
    Criteria criteria = getCurrentSession().createCriteria(templateEntity.getClass());
    criteria.add(Example.create(templateEntity));
    return criteria.list();
}

Entities;

public class Classroom{

    ....
    @OneToMany(mappedBy = "classroom", fetch = FetchType.EAGER)
    private List<Student> studentList;
}


public class Student{

    @JoinColumn(name = "classroom", referencedColumnName = "id")
    @ManyToOne(fetch = FetchType.LAZY)
    private Classroom classroom;
}
4

1 回答 1

1

尝试将以下内容添加到您的标准中:

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

这将为 Classroom 检索不同的实体,即使内部连接选择将检索三行(每个用户一行)。

于 2013-09-25T14:43:53.990 回答