如何使用一对多关联在hibernate中选择单个子项

时间:2016-04-24 03:50:25

标签: java hibernate hql one-to-many hibernate-criteria

假设我有一个与“学生”有一对多关系的“班级”对象。

public class Class implements java.io.Serializable {

private String classId;
private List<Student> student; 

public Class() {
}

public Class(String classId) {
    this.classId = classId;
}


@Id

@Column(name = "CLASS_ID", unique = true, nullable = false, length = 10)
public String getClassId() {
    return this.classId;
}

public void setClassId(String classId) {
    this.classId = classId;
}


@OneToMany(fetch = FetchType.EAGER, mappedBy="classId")
public List<Student> getStudent() {
    return student;
}

每班最多可达20万“学生”。我想只用它的第一个学生来检索一个“班级”。但是当我这样做时,我得到一个单独的“班级”,所有的“学生”而不是一个。

criteria = session.createCriteria(Class.class, "class").add(Restrictions.eq("class.classId", "0002")).setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

这导致数据库超时,因为查询超过200k的数据库需要太长时间“学生”有没有办法在Hibernate中查询数据库以检索只有一个“学生”的特定“类”检索所有200k所以数据库没有超时?我更愿意使用Criteria这样做,但如果没有,有没有办法用HQL做到这一点?

2 个答案:

答案 0 :(得分:0)

我一直在阅读这篇关于这个主题的非常有趣的文章

http://www.theotherian.com/2013/07/hibernate-joins-maxresults.html

将该文章中提出的解决方案应用于您的问题:

/**
 * This function will receive the variable as parameter who's current state need to be
 * incremented or decremented based on operation parameter.
 * value send as parameter will be used as amount by which we need to increment our
 * current state variable, it will be usefull incase like we have a limitation on size
 * of a repo.     
 */

public function updateCurrentState($variable,$operation,$value = null)
{
    $preferenceMode = Preference::model()->findByAttributes(array(
        'variable' => $variable,
        'type' => 'system_limit',
    ));
    if(!$preferenceMode){
        return 'not found';
    }
    $currentStateVariable = "current_state_".$variable;

    $currentStatePreferenceModel = Preference::model()->findByAttributes(array(
        'variable' => $currentStateVariable,  
        'type' => 'system_limit'
    ));

    if ($operation == 'save'){
        $currentStatePreferenceModel->value += ($value == null?1:$value);
        if($preferenceMode->value < $currentStatePreferenceModel->value){
            $error = $this->updateFlagState($variable,1);
            return $error;
        }

    }

    if(!$currentStatePreferenceModel->save()){
        return 'Licensing variable can not be updated';
    }
    return $error;
}

/**
 * This function updates the notification variable value.
 */
public function updateFlagState($variable,$value)
{
    $prefrenceNotificationModel = Preference::model()->findByAttributes(array(
        'variable' => 'notification_'.$variable,  
        'type' => 'system_limit'
    ));
    if(!$prefrenceNotificationModel){
        return 'Licensing variable can not be updated'; 
    }
    $prefrenceNotificationModel->value = $value;
    $prefrenceNotificationModel->updated = time();
    if(!$prefrenceNotificationModel->save()) {
        return 'Licensing variable can not be updated';
    }
    return 'done';
}

public function licensingObject($variable,$operation=null,$value=null)
{
    switch ($variable) {
        case "user_count":

                $error = $this->updateCurrentState($variable,$operation,$value);
                return $error;
                if($error == 'done'){
                    return "user count has exceded the licensing limit, user can not be created";       
                }
                break;
            default:
        }
}

我认为通过这种方式,您将获得一个只有一名学生使用您的标准来检索特定班级的列表

答案 1 :(得分:-1)

你试过HQL吗?可以在帖子下方帮助您理解HQL。

HQL join query to join to a single row of many to one relationship