I have some simple code that is supposed to select all teacher objects from a table of teachers in my mysql database. When I run similar queries for other domain objects, it is extremely fast, but for some reason this query takes forever. Also if I run a select all query from mysql workbench it executes instantly, so I think the problem is with my hibernate mapping. There are only 30 ish entries in the table so there is no reason this should be taking long.
Hibernate mapping xml file:
<?php
//*****************************************************START OF FUNCTION
function timeAgo($time_ago) {
$time_ago = strtotime($time_ago);
$cur_time = time();
$time_elapsed = $cur_time - $time_ago;
$seconds = $time_elapsed ;
$minutes = round($time_elapsed / 60 );
$hours = round($time_elapsed / 3600);
$days = round($time_elapsed / 86400 );
$weeks = round($time_elapsed / 604800);
$months = round($time_elapsed / 2600640 );
$years = round($time_elapsed / 31207680 );
echo $seconds."<br>";
if ($seconds <= 60) {
return "just now";
} //end of if ($seconds <= 60)
else if ($minutes <=60) {
if ($minutes == 1) {
return "one minute ago";
} //end of else if ($minutes <=60)
else {
return "$minutes minutes ago";
} //end of else not ($minutes == 1)
} //end of else if ($minutes <= 60)
else if ($hours <= 24) {
if ($hours == 1) {
return "an hour ago";
} //end of if ($hours == 1)
else {
return "$hours hours ago";
} //end of else not ($hours == 1)
} //end of else if ($hours <= 24)
else if ($days <= 7) {
if ($days == 1) {
return "yesterday";
} //end of else if ($days <= 7)
else {
return "$days days ago";
} //end of else not ($days == 1)
} //end of else if ($days <= 7)
else if ($weeks <= 4.3) {
if ($weeks == 1) {
return "a week ago";
} //end of if ($weeks == 1)
else {
return "$weeks weeks ago";
} //end of else not ($weeks == 1)
} //end of else if ($weeks <= 4.3)
else if ($months <= 12) {
if ($months == 1) {
return "a month ago";
} //end of if ($months == 1)
else {
return "$months months ago";
} //end of else not ($months == 1)
} //end of else if ($months <= 12)
else {
if ($years == 1) {
return "one year ago";
} //end of if ($years == 1)
else {
return "$years years ago";
} //end of else not ($years == 1)
} //end of last else
} //end of function timeAgo($time_ago)
//*****************************************************END OF FUNCTION
$date1 = "2016-02-10";
$time1 = "22:41:58";
$date2 = "2016-02-12";
$time2 = "15:25:57";
$timeAgo1 = timeAgo($date1.$time1);
$timeAgo2 = timeAgo($date2.$time2);
echo $timeAgo1."<br>".$timeAgo2;
?>
This is the method being called that takes so long :
<hibernate-mapping>
<class name="org.myschool.domain.Teacher" table="teacher">
<id name="teacher_id" type="java.lang.Integer">
<column name="teacher_id" />
<generator class="increment" />
</id>
<property name="firstName" type="java.lang.String">
<column name="first_name" length="20" not-null="true"/>
</property>
<property name="lastName" type="java.lang.String">
<column name="last_name" length="30" not-null="true" />
</property>
<property name="middleInitial" type="java.lang.String">
<column name="middle_initial" length="1" not-null = "false"/>
</property>
<set name="wantsCourses" table="teacher_want" inverse="false" lazy="true" fetch="select" cascade="all">
<key>
<column name="teacher_id" not-null="true" />
</key>
<many-to-many entity-name="org.myschool.domain.Course">
<column name="course_id" not-null="true" />
</many-to-many>
</set>
<set name="eligibleCourses" table="teacher_eligible" inverse="false" lazy="true" fetch="select" cascade="all">
<key>
<column name="teacher_id" not-null="true" />
</key>
<many-to-many entity-name="org.myschool.domain.Course">
<column name="course_id" not-null="true" />
</many-to-many>
</set>
<set name="teacherTranscript" table="assigned_course" inverse="true" lazy="true" fetch="select">
<key>
<column name="teacher_id" not-null="true" />
</key>
<one-to-many class="org.myschool.domain.AssignedCourse" />
</set>
</class>
</hibernate-mapping>
And this is the actual hibernate query being executed:
public List<Teacher> findAll() {
try {
@SuppressWarnings("unchecked")
List<Teacher> results = (List<Teacher>) sessionFactory.getCurrentSession()
.createCriteria("org.myschool.domain.Teacher").list();
log.debug("find all successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
Here's the sql command that makes the teacher table:
Hibernate: select this_.teacher_id as teacher_1_8_0_, this_.first_name as first_na2_8_0_, this_.last_name as last_nam3_8_0_, this_.middle_initial as middle_i4_8_0_ from teacher this_
Thanks for your help!