On 7/8/07, Bandula <[EMAIL PROTECTED]> wrote:
Hi Guys,
I am new to web application development with appfuse. This is my problem.
I have 2 model classes (Employee and Task) which has many to many
relationship.
My code segments as follows
*Task model class*
*private* List<Employee> assignedEmployeeList;
@ManyToMany( targetEntity = Employee.*class*)
*public* List<Employee> getAssignedEmployeeList() {
*return* assignedEmployeeList;
}
*public* *void* setAssignedEmployeeList(List<Employee>
assignedEmployeeList) {
*this*.assignedEmployeeList = assignedEmployeeList;
}
etc
*Employee model class*
* private* List<Task> asignedTaskList;
@ManyToMany(targetEntity = Task.*class*)
*public* List<Task> getAsignedTaskList() {
*return* asignedTaskList;
}
*public* *void* setAsignedTaskList(List<Task> asignedTaskList) {
*this*.asignedTaskList = asignedTaskList;
}
Etc
*TaskDaoHibernate class*
*public* List<Task> findTasksByEmployee(List<Employee> employeeList){
*return* getHibernateTemplate().find("from Task t where
t.assignedEmployeeList in ?", employeeList);
// return getHibernateTemplate().find("from Task");
}
Problem 1.
When I call findTasksByEmployee() method from action class it gives an
error related to HQL syntax used in TaskDaoHibernate. I wander where when
wrong. But I commented line in above class (return
getHibernateTemplate().find("from Task");) works fine and return tasks
list. Can someone pls help me to correct this. I appreciated if you can tell
me where I can find sample codes on these.
I'm not 100% sure on this, but I think that the default implementation of
the find() method on the Spring HibernateTemplate does not support setting
multiple values on a single query parameter. To get this to work you are
going to need to do something like:
public List<Task> findTasksByEmployee(List<Employee> employeeList){
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws
HibernateException, SQLException {
Query query = session.createQuery("from Task t where
t.assignedEmployeeList in (:employees)");
query.setParameterList("employees", employees);
return query.list();
}
};
);
}
Problem 2
I get 2 additional tables (task_t_employee_t and employee_t_task_t) even
if many to many relationship can maintained with one additional table
When mapping many-to-many, you need to declare one end of the relationship
as the owner. So your annotations should read something like the following:
@ManyToMany(
targetEntity=Task.class,
cascade={CascadeType.ALL}
)
public List<Task> getAsignedTaskList() {
return asignedTaskList;
}
And on the other side:
@ManyToMany(
cascade={CascadeType.ALL},
mappedBy="assignedTaskList"
targetEntity=Employee.class
)
public List<Employee> getAssignedEmployeeList() {
return assignedEmployeeList;
}
Thanks in advance
Bandula