Another way you can think of designing your tables is as follows:-
Code:
create table Person (
pid varchar2(10),
name varchar2(50),
primary key (pid)
)
create table Project (
pro_id varchar2(10),
name varchar2(50),
primary key (pro_id)
)
create table Project_Member (
pm_id varchar2(10),
pid varchar2(10),
pro_id varchar2(10),
role varchar2(50),
primary key (pm_id, pro_id),
foreign key (pid) references Person,
foreign key (pro_id) references Project
)
Notice that the primary key in the Project_Member is a composite key comprising of pm_id and pro_id, this will ensure that one member cannot have multiple roles on one project. The primary key will not let this happen as there will always be one and only one entry for a given pm_id and pro_id.
The Hibernate mappings and domain objects for the above design are as follows:-
Hibernate Mappings:-Person.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="projectmember.Person" table="PERSON">
<id name="pid" type="string">
<column name="PID" length="10" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="NAME" length="50" />
</property>
<set name="projectMembers" inverse="true" lazy="true" cascade="all,delete-orphan">
<key>
<column name="PID" length="10" />
</key>
<one-to-many class="projectmember.ProjectMember" />
</set>
</class>
</hibernate-mapping>
Project.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="projectmember.Project" table="PROJECT">
<id name="proId" type="string">
<column name="PRO_ID" length="10" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="NAME" length="50" />
</property>
<set name="projectMembers" inverse="true" lazy="true">
<key>
<column name="PRO_ID" length="10" not-null="true" />
</key>
<one-to-many class="projectmember.ProjectMember" />
</set>
</class>
</hibernate-mapping>
ProjectMember.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="projectmember.ProjectMember" table="PROJECT_MEMBER">
<composite-id name="id" class="projectmember.ProjectMemberId">
<key-property name="pmId" type="string">
<column name="PM_ID" length="10" />
</key-property>
<key-property name="proId" type="string">
<column name="PRO_ID" length="10" />
</key-property>
</composite-id>
<many-to-one name="person" class="projectmember.Person" update="true" insert="true" fetch="select">
<column name="PID" length="10" />
</many-to-one>
<many-to-one name="project" class="projectmember.Project" update="false" insert="false" fetch="select">
<column name="PRO_ID" length="10" not-null="true" />
</many-to-one>
<property name="role" type="string">
<column name="ROLE" length="50" />
</property>
</class>
</hibernate-mapping>
Domain Objects:-Person.java
Code:
package projectmember;
import java.util.HashSet;
import java.util.Set;
public class Person implements java.io.Serializable {
private static final long serialVersionUID = -6513446322027254027L;
private String pid;
private String name;
private Set projectMembers = new HashSet(0);
public Person() {
}
public Person(String pid) {
this.pid = pid;
}
public Person(String pid, String name, Set projectMembers) {
this.pid = pid;
this.name = name;
this.projectMembers = projectMembers;
}
public String getPid() {
return this.pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set getProjectMembers() {
return this.projectMembers;
}
public void setProjectMembers(Set projectMembers) {
this.projectMembers = projectMembers;
}
}
Project.java
Code:
package projectmember;
import java.util.HashSet;
import java.util.Set;
public class Project implements java.io.Serializable {
private static final long serialVersionUID = -5979917496563169126L;
private String proId;
private String name;
private Set projectMembers = new HashSet(0);
public Project() {
}
public Project(String proId) {
this.proId = proId;
}
public Project(String proId, String name, Set projectMembers) {
this.proId = proId;
this.name = name;
this.projectMembers = projectMembers;
}
public String getProId() {
return this.proId;
}
public void setProId(String proId) {
this.proId = proId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set getProjectMembers() {
return this.projectMembers;
}
public void setProjectMembers(Set projectMembers) {
this.projectMembers = projectMembers;
}
}
ProjectMember.java
Code:
package projectmember;
public class ProjectMember implements java.io.Serializable {
private static final long serialVersionUID = -4509653258724432914L;
private ProjectMemberId id;
private Person person;
private Project project;
private String role;
public ProjectMember() {
}
public ProjectMember(ProjectMemberId id, Project project) {
this.id = id;
this.project = project;
}
public ProjectMember(ProjectMemberId id, Person person, Project project, String role) {
this.id = id;
this.person = person;
this.project = project;
this.role = role;
}
public ProjectMemberId getId() {
return this.id;
}
public void setId(ProjectMemberId id) {
this.id = id;
}
public Person getPerson() {
return this.person;
}
public void setPerson(Person person) {
this.person = person;
}
public Project getProject() {
return this.project;
}
public void setProject(Project project) {
this.project = project;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
And finally the ID class:-
Code:
package projectmember;
public class ProjectMemberId implements java.io.Serializable {
private static final long serialVersionUID = -2957054540624377023L;
private String pmId;
private String proId;
public ProjectMemberId() {
}
public ProjectMemberId(String pmId, String proId) {
this.pmId = pmId;
this.proId = proId;
}
public String getPmId() {
return this.pmId;
}
public void setPmId(String pmId) {
this.pmId = pmId;
}
public String getProId() {
return this.proId;
}
public void setProId(String proId) {
this.proId = proId;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof ProjectMemberId))
return false;
ProjectMemberId castOther = (ProjectMemberId) other;
return ((this.getPmId() == castOther.getPmId()) || (this.getPmId() != null
&& castOther.getPmId() != null && this.getPmId().equals(
castOther.getPmId())))
&& ((this.getProId() == castOther.getProId()) || (this
.getProId() != null
&& castOther.getProId() != null && this.getProId()
.equals(castOther.getProId())));
}
public int hashCode() {
int result = 17;
result = 37 * result
+ (getPmId() == null ? 0 : this.getPmId().hashCode());
result = 37 * result
+ (getProId() == null ? 0 : this.getProId().hashCode());
return result;
}
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append(getPmId() + "." + getProId());
return buffer.toString();
}
}
Hope this help, if it does don't forget to rate :-)
Thanks,
Nikhil