The mapping you have there is pretty exotic. I think I would use that
pattern when I needed to store additional information about each
association. It doesn't look that way in the code below, but perhaps
in the future you want to distinguish users that belong to a project
by a role. In that case you could add a role field to your
association class, but I am gettting off topic.....
Would this simplify things?
[some annoations omitted]
public class Project{
@ManyToMany
@JoinTable(
name="project_employee",
joinColumns = [EMAIL PROTECTED](name = "employee_id")},
inverseJoinColumns = [EMAIL PROTECTED](name = "project_id")}
}
private Set<Employee> employees;
}
public class Employee{
@ManyToMany(mappedBy = "employees")
private Set<Project> projects
}
Project project = [get project from db or whatever]
List employeesInProject = getHibernateTemplate.find("from Employee
where ? in elements(e.projects)", project)
Note: I did not look up the HQL above but I am pretty sure the in
elements() is the right syntax.... With this model, you would only
know what employees were in each project and vice versa. As I
mentioned before, if your intent is to store information about the
association then your mapping model is appropriate.
On Apr 7, 2008, at 11:03 PM, Trish wrote:
Hi
Im new to appfuse and trying to implement manay to many relation for
Employee Project tables, the created model classes as follows
@Entity
public class Employee extends BaseObject {
private Long employeeId;
private String employeeName;
private List<EmployeesProject> employeesProjects;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getEmployeeId() {
return employeeId;
}
@OneToMany(mappedBy = "project")
public List<EmployeesProject> getProjects() {
return employeesProjects;
}
......
}
@Entity
public class Project extends BaseObject {
private Long projectId;
private String projectName;
private List<EmployeesProject> employeesProjects;
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long getProjectId() {
return projectId;
}
@OneToMany(mappedBy="employee")
public List<EmployeesProject> getEmployees() {
return employeesProjects;
}
.........
}
@Entity
public class EmployeesProject extends BaseObject {
Long employeesProjectId;
Employee employee;
Project project;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getEmployeesProjectId() {
return employeesProjectId;
}
@ManyToOne
@JoinColumn(name = "employeeId")
public Employee getEmployee() {
return employee;
}
@ManyToOne
@JoinColumn(name = "employeeId")
public Employee getEmployee() {
return employee;
}
........
}
Test data
<table name="employeesproject">
<column>employeesProjectId</column>
<column>employeeId</column>
<column>projectId</column>
<row>
<value description="employeesProjectId">1</value>
<value description="employeeId">1</value>
<value description="projectId">1</value>
</row>
<row>
<value description="employeesProjectId">2</value>
<value description="employeeId">2</value>
<value description="projectId">1</value>
</row>
</table>
public List<Employee> getByProject(Long projectId, Long periodId) {
return getHibernateTemplate().find("from Employee where employeeId
in " +
"(select employeeId from EmployeesProject " +
"where projectId=?"+ projectId+")");
}
with above entity classes I have created three tables respectivly
and trying
to fetch the records with above code but fail to get them the hql i
have
writen is giving error where I m not sure about used fields. kindly
guide
me. I may be wrongly implement the relation but could not found
sufficient
help in the appfuse pdf about realtional maaping.
Thank you
Regards
Trish
--
View this message in context:
http://www.nabble.com/Help-needed-for-Many-to-Many-relation-tp16547740s2369p16547740.html
Sent from the AppFuse - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]