I'm a little confused about the composite-id syntax. I read the documentation, and looked through *many* of the forum posts on this topic, but I haven't found a complete example. Perhaps this can be one.
I have 4 legacy tables:
Division -> Group -> Department -> Employee
Division has a 1 to many relationship with Group,
Group has a 1 to many relationship with Department and,
Department has a 1 to many relationship with Employee.
Divisions table looks like:
divCode char(2)
name varchar(30)
primary key(divCode)
Grouptable looks like:
divCode char(2)
grpCode char(2)
name varchar(30)
primary key(divCode, grpCode)
Department table looks like:
divCode char(2)
grpCode char(2)
dptCode char(3)
name varchar(30)
primary key(divCode, grpCode, dptCode)
Employee table looks like:
divCode char(2)
grpCode char(2)
dptCode char(3)
empCode char(6)
firstName varchar(50)
lastName varchar(50)
primary key(divCode, grpCode, dptCode, empCode)
I created classes similar to:
Code:
public class DivisionId implements Serializable {
private String myDivisionCode;
public DivisionId() {
super();
}
public DivisionId(String divCode){
this();
myDivCode = divCode;
}
public String getDivCode() {
return myDivCode;
}
public void setDivCode(String string) {
myDivCode = string;
}
Code:
public class GroupId implements Serializable {
private DivisionId myDivisionId;
private String myGroupCode;
public GroupId() {
super();
}
public GroupId(DivisionId divisionId, String grpCode) {
this();
myDivisionId = divisionId;
myGroupCode = grpCode;
}
public String getGroupCode() {
return myGroupCode;
}
public DivisionId getDivisionId() {
return myDivisionId;
}
public void setGroupCode(String string) {
myGroupCode = string;
}
public void setDivisionId(DivisionId id) {
myDivisionId = id;
}
public boolean equals(Object other) {
if (!(other instanceof GroupId))
return false;
GroupId castOther = (GroupId) other;
return new EqualsBuilder()
.append(this.getDivisionId(), castOther.getDivisionId())
.append(this.getGroupCode(), castOther.getGroupCode())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder().append(getDivisionId()).append(getGroupCode()).toHashCode();
}
}
I thought that it made sense to use the parent ID in the child, however, I can't find any examples that described except:
this or
this
Is it possible to map the ids in this manner? Or do each of the columns have to be mapped individually in the <key> tag of the hbm.xml file?
P.S. The documentation is good, but it would be great if it included table layouts and relationships (via an ERD) in the examples for completeness. Sometimes the syntax and it's effect on the model is not obvious by the hbm.xml file.