-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Having issues with joined-subclass (inheritence)
PostPosted: Sun Aug 31, 2003 1:44 am 
Newbie

Joined: Sun Aug 31, 2003 1:20 am
Posts: 17
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

}

}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 31, 2003 1:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Exactly what is wrong??


You have to let us know whats not working....


Top
 Profile  
 
 Post subject: One more note...
PostPosted: Sun Aug 31, 2003 1:57 am 
Newbie

Joined: Sun Aug 31, 2003 1:20 am
Posts: 17
Following is a snippet of code I am using to test this:


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

public class Main {

private static SessionFactory sessionFactory;

private static void configSessionFactory() {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("hibernate.properties");
Configuration cfg = new Configuration();
try {
cfg.addClass(Person.class);
} catch (MappingException e) {
e.printStackTrace();
}

Properties p = new Properties();
try {
p.load(in);
} catch (IOException e1) {
e1.printStackTrace();
}
cfg.setProperties(p);

try {
sessionFactory = cfg.buildSessionFactory();
} catch (HibernateException e2) {
e2.printStackTrace();
}


}

public static void main(String[] args) {
configSessionFactory();

try {
Session session = sessionFactory.openSession();

Student sr = new Student();
sr.setPersonId(new Integer(1));
sr.setCitizenId("111111");
sr.setName("Me");
sr.setGender("M");
sr.setGrade("1");
//sr.setDateOfBirth(new Date());
sr.setNationality("Lebanese");
sr.setAddress("4343");
sr.setFamilyComment("familyComment");
sr.setHomePhone("44757803");

Parent par = new Parent();
par.setPersonId(new Integer(2));
par.setParentName("Dad");
par.setCellPhone("55555");
sr.setParent(par);

session.save(sr);

} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}
}

Following is my the stack trace when I run this code:

java.lang.NullPointerException
at net.sf.hibernate.persister.NormalizedEntityPersister.getIdentifierColumnNames(NormalizedEntityPersister.java:1075)
at net.sf.hibernate.persister.AbstractEntityPersister.<init>(AbstractEntityPersister.java:699)
at net.sf.hibernate.persister.NormalizedEntityPersister.<init>(NormalizedEntityPersister.java:646)
at net.sf.hibernate.persister.PersisterFactory.create(PersisterFactory.java:32)
at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:137)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:647)
at norc.enum.datatypes.Main.configSessionFactory(Main.java:65)
at Main.main(Main.java:75)



Thanks for any help with this.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 31, 2003 2:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
ick! yucky npe....

Bad Hibernate!



The problem is probably here:

Code:
<id
name="personId"
type="java.lang.Object" >


(at least that is one problem)

Hibernate will want something a bit better than "java.lang.Object" for an identifier type.


Top
 Profile  
 
 Post subject: Did that change..
PostPosted: Sun Aug 31, 2003 2:45 am 
Newbie

Joined: Sun Aug 31, 2003 1:20 am
Posts: 17
I am actually working on it now and made that change to Integer. Still did not work though. While going through the debug, I realized that Hibernate is blowing up on the following method:

public String[] getIdentifierColumnNames() {
return tableKeyColumns[0];
}


in the following class:

NormalizedEntityPersister

Which is wrapping my Student class.

Something seems to be wrong with the Student class/mapping that is causing this to blow up here I guess.

Thanks.


Top
 Profile  
 
 Post subject: BTW
PostPosted: Sun Aug 31, 2003 2:48 am 
Newbie

Joined: Sun Aug 31, 2003 1:20 am
Posts: 17
I am using Hibernate 2.1.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 31, 2003 2:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Ah this is a bug in 2.1 beta 1.

please get the latest version (beta2).


Top
 Profile  
 
 Post subject: Yeah!!
PostPosted: Sun Aug 31, 2003 3:16 am 
Newbie

Joined: Sun Aug 31, 2003 1:20 am
Posts: 17
Thank you for your help. I also downloaded 2.0.3 and used that and it worked fine with it (as well as with the 2.1beta2. Is there a list of all known bugs that we can refer to? We are starting to use Hibernate as our standard persistence solution and it would help to know what bugs exist in each release.

Thanks again for the help.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 31, 2003 3:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Well, there is JIRA for known bugs in current releases.

But in this case, the bug was mentioned in the changelog.


Note that bugs like this do creep into beta releases - thats why its called a beta ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.