Raymond Holguin
Raymond Holguin

Reputation: 1122

Hibernate order-by removing rows

I have a one-to-Many Set defined in my mapping file, and when I use the order-by property the data in the Set will be missing rows depending on what property I use to order.

<hibernate-mapping>
    <class name="domain.StudentUser" table="student_user" >

....

    <set name="studentCourseses" inverse="true" order-by="import_date ASC">
        <key>
            <column name="student_userid" length="15" not-null="true" />
        </key>
        <one-to-many class="domain.StudentCourses" />
    </set>
</hibernate-mapping>

public StudentUser findById(java.lang.String id) {
    log.debug("getting StudentUser instance with id: " + id);
    try {
        StudentUser instance = (StudentUser) getSession().get(
                "domain.StudentUser", id);
        return instance;
    } catch (RuntimeException re) {
        log.error("get failed", re);
        throw re;
    }
}

Here i use import_date, but this table has about 15 properties and certain properties will cause rows to be missing from the Set while others don't. It doesn't seem to be data-type specific as I have the issue with a String property but then another String property will work fine and all data will be there. It is always the same rows that are missing no matter the sorting property used, so its not random rows.

The majority of the student_user have data missing, but not all. The rows that end up missing have nothing abnormal about them, meaning no NULL's or anything like that.

As of this point, i just removed the order-by attribute so I can get my app working property, but I still need to know why these issues are happening. So far I have not noticed this behavior in any of the other Set's that i use the order-by attribute with, just this one.

I am using Hibernate 3.5.3 and in case it's helpful, here is the mapping file for the StudentCourses table

<hibernate-mapping>
<class name="domain.StudentCourses" table="student_courses" >
    <id name="id" type="integer">
        <column name="id" />
        <generator class="native" />
    </id>
    <many-to-one name="classLevel" class="domain.ClassLevel" fetch="select">
        <column name="class_level_code" length="2" not-null="true" />
    </many-to-one>
    <many-to-one name="academicTerm" class="domain.AcademicTerm" fetch="select">
        <column name="academic_term_id" not-null="true" />
    </many-to-one>
    <many-to-one name="studentUser" class="domain.StudentUser" fetch="select">
        <column name="student_userid" length="15" not-null="true" />
    </many-to-one>
    <property name="academicYear" type="integer">
        <column name="academic_year" not-null="true" />
    </property>
    <property name="term" type="string">
        <column name="term" length="8" not-null="true" />
    </property>
    <property name="title" type="string">
        <column name="title" length="64" not-null="true" />
    </property>
    <property name="prefix" type="string">
        <column name="prefix" length="8" not-null="true" />
    </property>
    <property name="courseNum" type="string">
        <column name="course_num" length="8" not-null="true" />
    </property>
    <property name="suffix" type="string">
        <column name="suffix" length="8" />
    </property>
    <property name="sectionNum" type="string">
        <column name="section_num" length="8" />
    </property>
    <property name="units" type="double">
        <column name="units" precision="4" not-null="true" unique="true" />
    </property>
    <property name="letterGrade" type="string">
        <column name="letter_grade" length="6" not-null="true" />
    </property>
    <property name="qtrGpa" type="double">
        <column name="qtr_gpa" precision="4" scale="3" not-null="true" />
    </property>
    <property name="cumGpa" type="double">
        <column name="cum_gpa" precision="4" scale="3" not-null="true" />
    </property>
    <property name="importDate" type="timestamp">
        <column name="import_date" length="19" />
    </property>
    <property name="sourceFileName" type="string">
        <column name="source_file_name" length="128" />
    </property>
    <property name="timeStamp" type="timestamp">
        <column name="time_stamp" length="19" not-null="true">
            <comment>on update CURRENT_TIMESTAMP</comment>
        </column>
    </property>
</class>

Update (More Info):

If i directly query the StudentCourses table (CORRECT) vs getting the StudentCourses data from the StudentUser mapping set (INCORRECT) gives me different results, they should match

for(StudentCourses course : getStudentService().retrieveStudentByID("861043440").getStudentCourseses()) {  //MISSING DATA
        System.out.println(course +" - "+course.getTerm());
    }

    for(StudentCourses course : (List<StudentCourses>)getStudentService().getStudentCoursesDAO().findByProperty("studentUser.studentUserid", "861043440")) {  //ALL DATA CORRECT
        System.out.println(course +" - "+course.getTerm());
    }

Upvotes: 1

Views: 275

Answers (1)

Raymond Holguin
Raymond Holguin

Reputation: 1122

Turns out it had nothing to do with sorting, as adjusting the sorting only fixed the issue for some but not all. The problem was that I had overriden the hashCode method of the StudentCourses object and it was causing collisions when the Set was being populated from the StudentUser object. I fixed this and everything is good now.

Upvotes: 0

Related Questions