I am trying to use HIbernate with my inheritence hierarchy of Person, Parent Student. I have one table per class (I realized that this was not advised but this is the database structure I have to deal with!!). I have tried a lot of ideas but the following is a snapshot of where I got to last and I would appreciate some more informatoin on using joined-subclass as the documentation using it from SQL to mapping files to code is not clear. I am not sure what I am missing? Following is everything I have. Any help suggestions or a detainled explanation of how to use joined-sublcass would be really appreciated. Thanks. BTW, my hibernate.properties file is correct as I used it with a simpler example and it was working fine for me.
The following is my sql:
/* PERSON TABLE DDL */
CREATE TABLE PERSON(
PERSON_ID NVARCHAR(50) PRIMARY KEY,
NAME VARCHAR(100) NOT NULL,
NATIONALITY VARCHAR(50),
DATE_OF_BIRTH DATETIME,
GENDER CHAR(1),
CITIZEN_ID VARCHAR(10),
ADDRESS VARCHAR(200),
HOME_PHONE VARCHAR(20),
BUS_PHONE VARCHAR(20)
)
GO
/* PARENT TABLE DDL */
CREATE TABLE PARENT(
PARENT_ID NVARCHAR(50) PRIMARY KEY,
PARENT_NAME VARCHAR(100),
CELL_PHONE VARCHAR(100),
)
GO
/* STUDENT TABLE DDL */
CREATE TABLE STUDENT(
STUDENT_ID NVARCHAR(50) PRIMARY KEY,
GRADE VARCHAR(10),
TRACK VARCHAR(10),
FAMILY_COMMENT VARCHAR(200),
STUDENT_SECTION VARCHAR(10),
SCHEDULE_COMMENT VARCHAR(200),
ALL_OTHER_COMMENT VARCHAR(200),
)
GO
ALTER TABLE STUDENT
ADD FOREIGN KEY (STUDENT_ID)
REFERENCES PERSON(PERSON_ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
GO
ALTER TABLE PARENT
ADD FOREIGN KEY (PARENT_ID)
REFERENCES PERSON(PERSON_ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
GO
I have 3 tables where a Parent and student are of type person. Based on what I saw in the docuementation, I create the relationship between person and the subclasses using a foreign key from each subclasses primary key to the superclasses primary key. (is this correct?).
Following is my mapping file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class
name="Person"
table="PERSON"
>
<id
name="personId"
type="java.lang.Object"
column="PERSON_ID"
>
<generator class="assigned" />
</id>
<property
name="name"
type="java.lang.String"
column="NAME"
not-null="true"
length="100"
/>
<property
name="nationality"
type="java.lang.String"
column="NATIONALITY"
length="50"
/>
<property
name="dateOfBirth"
type="java.sql.Timestamp"
column="DATE_OF_BIRTH"
length="23"
/>
<property
name="gender"
type="java.lang.String"
column="GENDER"
length="1"
/>
<property
name="citizenId"
type="java.lang.String"
column="CITIZEN_ID"
length="10"
/>
<property
name="address"
type="java.lang.String"
column="ADDRESS"
length="200"
/>
<property
name="homePhone"
type="java.lang.String"
column="HOME_PHONE"
length="20"
/>
<property
name="busPhone"
type="java.lang.String"
column="BUS_PHONE"
length="20"
/>
<!-- associations -->
<joined-subclass
name="Parent"
table="PARENT"
>
<meta attribute="implements">net.sf.hibernate.Lifecycle</meta>
<meta attribute="implements">net.sf.hibernate.Validatable</meta>
<key column="PARENT_ID" />
<property
name="parentName"
type="java.lang.String"
column="PARENT_NAME"
length="100"
/>
<property
name="cellPhone"
type="java.lang.String"
column="CELL_PHONE"
length="100"
/>
</joined-subclass>
<joined-subclass
name="Student"
table="STUDENT"
>
<meta attribute="implements">net.sf.hibernate.Lifecycle</meta>
<meta attribute="implements">net.sf.hibernate.Validatable</meta>
<key column="STUDENT_ID" />
<property
name="grade"
type="java.lang.String"
column="GRADE"
length="10"
/>
<property
name="track"
type="java.lang.String"
column="TRACK"
length="10"
/>
<property
name="familyComment"
type="java.lang.String"
column="FAMILY_COMMENT"
length="200"
/>
<property
name="studentSection"
type="java.lang.String"
column="STUDENT_SECTION"
length="10"
/>
<property
name="scheduleComment"
type="java.lang.String"
column="SCHEDULE_COMMENT"
length="200"
/>
<property
name="allOtherComment"
type="java.lang.String"
column="ALL_OTHER_COMMENT"
length="200"
/>
<!-- associations -->
<!-- bi-directional one-to-one association to Person -->
<one-to-one
name="parent"
class="Person"
outer-join="auto"
/>
</joined-subclass>
</class>
</hibernate-mapping>
This is what I am trying to do, have both a student and parent inherit from Person. The Student has a reference to Parent (which is a person, or should it be specified as Parent?).
My code for all three classes is as follows:
import java.io.Serializable;
import java.util.Date;
import net.sf.hibernate.CallbackException;
import net.sf.hibernate.Session;
import net.sf.hibernate.ValidationFailure;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Parent extends Person implements Serializable, net.sf.hibernate.Lifecycle, net.sf.hibernate.Validatable {
/** nullable persistent field */
private String parentName;
/** nullable persistent field */
private String cellPhone;
/** full constructor */
public Parent(java.lang.Object personId, java.lang.String name, java.lang.String nationality, Date dateOfBirth, java.lang.String gender, java.lang.String citizenId, java.lang.String address, java.lang.String homePhone, java.lang.String busPhone, java.lang.String parentName, java.lang.String cellPhone) {
super(personId, name, nationality, dateOfBirth, gender, citizenId, address, homePhone, busPhone);
this.parentName = parentName;
this.cellPhone = cellPhone;
}
/** default constructor */
public Parent() {
}
public java.lang.String getParentName() {
return this.parentName;
}
public void setParentName(java.lang.String parentName) {
this.parentName = parentName;
}
public java.lang.String getCellPhone() {
return this.cellPhone;
}
public void setCellPhone(java.lang.String cellPhone) {
this.cellPhone = cellPhone;
}
public String toString() {
return new ToStringBuilder(this)
.append("personId", getPersonId())
.toString();
}
/* (non-Javadoc)
* @see net.sf.hibernate.Lifecycle#onSave(net.sf.hibernate.Session)
*/
public boolean onSave(Session s) throws CallbackException {
// TODO Auto-generated method stub
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Lifecycle#onUpdate(net.sf.hibernate.Session)
*/
public boolean onUpdate(Session s) throws CallbackException {
// TODO Auto-generated method stub
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Lifecycle#onDelete(net.sf.hibernate.Session)
*/
public boolean onDelete(Session s) throws CallbackException {
// TODO Auto-generated method stub
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Lifecycle#onLoad(net.sf.hibernate.Session, java.io.Serializable)
*/
public void onLoad(Session s, Serializable id) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see net.sf.hibernate.Validatable#validate()
*/
public void validate() throws ValidationFailure {
// TODO Auto-generated method stub
}
}
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Person implements Serializable {
/** identifier field */
private Object personId;
/** persistent field */
private String name;
/** nullable persistent field */
private String nationality;
/** nullable persistent field */
private java.util.Date dateOfBirth;
/** nullable persistent field */
private String gender;
/** nullable persistent field */
private String citizenId;
/** nullable persistent field */
private String address;
/** nullable persistent field */
private String homePhone;
/** nullable persistent field */
private String busPhone;
/** full constructor */
public Person(java.lang.Object personId, java.lang.String name, java.lang.String nationality, java.util.Date dateOfBirth, java.lang.String gender, java.lang.String citizenId, java.lang.String address, java.lang.String homePhone, java.lang.String busPhone) {
this.personId = personId;
this.name = name;
this.nationality = nationality;
this.dateOfBirth = dateOfBirth;
this.gender = gender;
this.citizenId = citizenId;
this.address = address;
this.homePhone = homePhone;
this.busPhone = busPhone;
}
/** default constructor */
public Person() {
}
/** minimal constructor */
public Person(java.lang.Object personId, java.lang.String name) {
this.personId = personId;
this.name = name;
}
public java.lang.Object getPersonId() {
return this.personId;
}
public void setPersonId(java.lang.Object personId) {
this.personId = personId;
}
public java.lang.String getName() {
return this.name;
}
public void setName(java.lang.String name) {
this.name = name;
}
public java.lang.String getNationality() {
return this.nationality;
}
public void setNationality(java.lang.String nationality) {
this.nationality = nationality;
}
public java.util.Date getDateOfBirth() {
return this.dateOfBirth;
}
public void setDateOfBirth(java.util.Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public java.lang.String getGender() {
return this.gender;
}
public void setGender(java.lang.String gender) {
this.gender = gender;
}
public java.lang.String getCitizenId() {
return this.citizenId;
}
public void setCitizenId(java.lang.String citizenId) {
this.citizenId = citizenId;
}
public java.lang.String getAddress() {
return this.address;
}
public void setAddress(java.lang.String address) {
this.address = address;
}
public java.lang.String getHomePhone() {
return this.homePhone;
}
public void setHomePhone(java.lang.String homePhone) {
this.homePhone = homePhone;
}
public java.lang.String getBusPhone() {
return this.busPhone;
}
public void setBusPhone(java.lang.String busPhone) {
this.busPhone = busPhone;
}
public String toString() {
return new ToStringBuilder(this)
.append("personId", getPersonId())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof Person) ) return false;
Person castOther = (Person) other;
return new EqualsBuilder()
.append(this.getPersonId(), castOther.getPersonId())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getPersonId())
.toHashCode();
}
}
import java.io.Serializable;
import java.util.Date;
import net.sf.hibernate.CallbackException;
import net.sf.hibernate.Session;
import net.sf.hibernate.ValidationFailure;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Student extends Person implements Serializable, net.sf.hibernate.Lifecycle, net.sf.hibernate.Validatable {
/** nullable persistent field */
private String grade;
/** nullable persistent field */
private String track;
/** nullable persistent field */
private String familyComment;
/** nullable persistent field */
private String studentSection;
/** nullable persistent field */
private String scheduleComment;
/** nullable persistent field */
private String allOtherComment;
/** nullable persistent field */
private Person parent;
/** full constructor */
public Student(java.lang.Object personId, java.lang.String name, java.lang.String nationality, Date dateOfBirth, java.lang.String gender, java.lang.String citizenId, java.lang.String address, java.lang.String homePhone, java.lang.String busPhone, java.lang.String grade, java.lang.String track, java.lang.String familyComment, java.lang.String studentSection, java.lang.String scheduleComment, java.lang.String allOtherComment, Person parent) {
super(personId, name, nationality, dateOfBirth, gender, citizenId, address, homePhone, busPhone);
this.grade = grade;
this.track = track;
this.familyComment = familyComment;
this.studentSection = studentSection;
this.scheduleComment = scheduleComment;
this.allOtherComment = allOtherComment;
this.parent = parent;
}
/** default constructor */
public Student() {
}
public java.lang.String getGrade() {
return this.grade;
}
public void setGrade(java.lang.String grade) {
this.grade = grade;
}
public java.lang.String getTrack() {
return this.track;
}
public void setTrack(java.lang.String track) {
this.track = track;
}
public java.lang.String getFamilyComment() {
return this.familyComment;
}
public void setFamilyComment(java.lang.String familyComment) {
this.familyComment = familyComment;
}
public java.lang.String getStudentSection() {
return this.studentSection;
}
public void setStudentSection(java.lang.String studentSection) {
this.studentSection = studentSection;
}
public java.lang.String getScheduleComment() {
return this.scheduleComment;
}
public void setScheduleComment(java.lang.String scheduleComment) {
this.scheduleComment = scheduleComment;
}
public java.lang.String getAllOtherComment() {
return this.allOtherComment;
}
public void setAllOtherComment(java.lang.String allOtherComment) {
this.allOtherComment = allOtherComment;
}
public Person getParent() {
return this.parent;
}
public void setParent(Person parent) {
this.parent = parent;
}
public String toString() {
return new ToStringBuilder(this)
.append("personId", getPersonId())
.toString();
}
/* (non-Javadoc)
* @see net.sf.hibernate.Lifecycle#onSave(net.sf.hibernate.Session)
*/
public boolean onSave(Session s) throws CallbackException {
// TODO Auto-generated method stub
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Lifecycle#onUpdate(net.sf.hibernate.Session)
*/
public boolean onUpdate(Session s) throws CallbackException {
// TODO Auto-generated method stub
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Lifecycle#onDelete(net.sf.hibernate.Session)
*/
public boolean onDelete(Session s) throws CallbackException {
// TODO Auto-generated method stub
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Lifecycle#onLoad(net.sf.hibernate.Session, java.io.Serializable)
*/
public void onLoad(Session s, Serializable id) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see net.sf.hibernate.Validatable#validate()
*/
public void validate() throws ValidationFailure {
// TODO Auto-generated method stub
}
}
|