Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hello all,
I'm working on a very basic situation but I am having quite a bit of trouble with it. In my project I have a date dimension table that is prepopulated with the dates from now until 20 years later. This is to be considered the "parent" object I guess. The child object is a list of appointments that are mapped as a bag from the date dimension table. You can obviously have many appointments per day. Herein lies the problem... I've got a POJO that is supposed to be gathering a List of appointments for any given day. I can either grab one day, or a weekly view of days. In my JSP I iterate over the results and it is always telling me that I don't have this "appointments" bean anywhere. I need to know if my code is correct, and I also need to know if my logic and thinking are correct on how to populate the "date" bean. When I select a date range shouldn't it automatically select any appointments scheduled for that day because of the hibernate mapping? Thanks for your help, here is my code.
Hibernate version:
2.x
Mapping documents:
for the Day:
<hibernate-mapping
>
<class
name="com.nyegroup.insurance.model.Day"
table="Day"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="id"
column="id"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Day.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="date"
type="java.util.Date"
update="true"
insert="true"
access="property"
column="date"
/>
<bag
name="appointments"
lazy="false"
inverse="true"
cascade="none"
>
<key
column="date"
>
</key>
<one-to-many
class="com.nyegroup.insurance.model.Appointment"
/>
</bag>
<property
name="dayofweek"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="dayofweek"
/>
<property
name="dayofyear"
type="java.lang.Long"
update="true"
insert="true"
access="property"
column="dayofyear"
/>
<SNIP>
</class>
</hibernate-mapping>
This is the Appointment Mapping:
<hibernate-mapping
>
<class
name="com.nyegroup.insurance.model.Appointment"
table="CalendarEntry"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="id"
column="id"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Appointment.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="account"
type="java.lang.Long"
update="true"
insert="true"
access="property"
column="account"
unique="false"
/>
<property
name="date"
type="java.util.Date"
update="true"
insert="true"
access="property"
column="date"
/>
<property
name="description"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="description"
/>
<property
name="location"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="location"
/>
<property
name="time"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="time"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Appointment.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
This is my DAO class that I use to retrieve all of the rows for a week and for a day.
import java.util.List;
import com.nyegroup.insurance.model.Day;
import com.nyegroup.insurance.model.Appointment;
import com.nyegroup.insurance.dao.DayDAO;
import com.nyegroup.insurance.dao.AppointmentDAO;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
public class DayDAOHibernate extends HibernateDaoSupport implements DayDAO {
/**
* @see com.nyegroup.insurance.dao.DayDAO#getDay(final Long id)
*/
public Day getDay(Long id) {
Day day = (Day) getHibernateTemplate().get(Day.class, id);
return day;
}
/**
* @see com.nyegroup.insurance.dao.DayDAO#getDay(final Long id)
*/
public Day getDay(Long id, String date) {
Day day = (Day) getHibernateTemplate().find("from Day" +
" where id='" + id + "' and date='" + date + "'");
return day;
}
/**
* @see com.nyegroup.insurance.dao.DayDAO#getDays(com.nyegroup.insurance.model.Day)
*/
public List getDays(Long id) {
return getHibernateTemplate().find("from Day where " +
//(account=" + id + " and
"( date>=DATE_SUB(CURRENT_DATE, " +
"INTERVAL (DAYOFWEEK(CURRENT_DATE) -1) DAY)) " +
"and ( date <= DATE_ADD(CURRENT_DATE, INTERVAL " +
"(7 - DAYOFWEEK(CURRENT_DATE)) DAY))" );
}
public List getDays(Long id, String date) {
return getHibernateTemplate().find("from Day where " +
//(account=" + id + " and
"( date>=DATE_SUB('" + date + "', " +
"INTERVAL (DAYOFWEEK('" + date + "') -1) DAY)) " +
"and ( date <= DATE_ADD('" + date +"', INTERVAL " +
"(7 - DAYOFWEEK('" + date +"')) DAY))" );
/* "(date>=(DATE_SUB('" + date +
"', INTERVAL (7 - DAYOFWEEK('" +
date + "')) DAY)) and date<=(DATE_ADD('" +
date + "', INTERVAL (7 - DAYOFWEEK('" +
date + "')) DAY)))" );
*/
}
/**
* @see com.nyegroup.insurance.dao.DayDAO#saveDay(Day day)
*/
public void saveDay(Day day) {
getHibernateTemplate().saveOrUpdate(day);
}
/**
* @see com.nyegroup.insurance.dao.DayDAO#removeDay(final Long id)
*/
public void removeDay(Long id) {
getHibernateTemplate().delete(getDay(id));
}
}
Name and version of the database you are using:
Mysql 4.1.x
Thanks for the help!