dharmendra.pandey wrote:
Hi sb5100,
Post your POJO ,HBM and table Desc .And environment detail (jars)
.
The following POJO can be saved without any Exceptions which is User
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="protocol.hbm.User" table="user">
<id name="id" type="string">
<generator class="server.DateIdGenerator"/>
</id>
<property name="uname" column="uname"/>
</class>
</hibernate-mapping>
Here is the POJO
Code:
package protocol.hbm;
import java.io.Serializable;
public class User implements Serializable {
public static final long serialVersionUID = 6632533009l;
private String id;
private String uname;
/**
* Default constructor required by hibernate
*/
public User(){
}
public User(String uname){
setUname(uname);
}
public String getId(){
return id;
}
void setId(String id){
this.id = id;
}
public String getUname(){
return uname;
}
public void setUname(String uname){
this.uname = uname;
}
public int getColor(){
return 0;
}
public boolean equals(Object obj){
if(!(obj instanceof User)){
return false;
}
User rhs = (User)obj;
return getUname().equals(rhs.getUname());
}
}
But when saving PIForm, which has a one-to-many relationship whit an instanceof, a saved user I get this exception. The POJO class and mapping is as follows
Mapping File
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<joined-subclass name=" protocol.hbm.PIForm"
extends="protocol.hbm.access.RestrictedAccess"
table="pi_form">
<key column="ra_id"/>
<many-to-one
name="user"
class="protocol.hbm.User"
column="userId"
not-null="true"
unique="true"/>
<set name="telephones" cascade="all,delete-orphan">
<key column="pi_form_id"/>
<one-to-many class=" protocol.hbm.Telephone"/>
</set>
<property name="firstname" type="string" length="32"/>
<property name="lastname" type="string" length="32"/>
<property name="forenames" type="string" length="128"/>
<property name="title" type="string" length="16"/>
<property name="email" type="string" length="32"/>
<property name="ismale" type="string" length="32"/>
<property name="dob" type="timestamp"/>
<set name="addresses" cascade="all,delete-orphan">
<key column="pi_form_id"/>
<one-to-many class=" protocol.hbm.Address"/>
</set>
</joined-subclass>
</hibernate-mapping>
POJO code
Code:
package protocol.hbm;
import java.util.Set;
import java.util.HashSet;
import java.sql.Date;
import protocol.IllegalFieldValueException;
/**
* This stands for Personal Information form. this class defines aabstract information
* about a person involved with the organaization such as a student or staff member
*
* @author Wiraj Rohitha Bibile
*
*/
//mapping document created
public class PIForm extends uk.org.wrtrust.protocol.hbm.access.RestrictedAccess{
public static final long serialVersionUID=100987;
private Set<Telephone> telephones = new HashSet<Telephone>();
private Set<Address> addresses = new HashSet<Address>();
private String firstname;//
private String lastname;//
private String forenames;//
private String title;//
private String email;
private boolean ismale;
private Date dob;//
private User user;
/*
* Nationality of the person
*/
private String nationality = "British";
public PIForm(){
}
public Set<Telephone> getTelephones(){
return telephones;
}
public void setTelephones(Set<Telephone> telephones){
this.telephones = telephones;
}
public Set<Address> getAddresses(){
return addresses;
}
public void setAddresses(Set<Address> addresses){
this.addresses = addresses;
}
public String getFirstname(){
return firstname;
}
public void setFirstname(String firstname){
this.firstname = firstname;
}
public String getLastname(){
return lastname;
}
public void setLastname(String lastname){
this.lastname = lastname;
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
public String getForenames(){
return forenames;
}
public void setForenames(String forenames){
this.forenames = forenames;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public User getUser(){
return user;
}
public void setUser(User user){
this.user = user;
}
public Date getDob(){
return dob;
}
public void setDob(Date dob){
this.dob= dob;
}
public String getNationality(){
return nationality;
}
public void setNationality(String nationality){
this.nationality = nationality;
}
//public PIForm(String name)
public static class Titles{
private static final String titles[]={"Mr","Mrs","Miss","Ms","Rev","Dr","Prof","Other"};
public static String[] getTitles(){
String titles[] = new String[PIForm.Titles.titles.length];
System.arraycopy(PIForm.Titles.titles,0,titles,0,PIForm.Titles.titles.length);
return titles;
}
public static String validate(String title){
title= title.trim();
for(String titleName:titles){
if(titleName.equalsIgnoreCase(title)){
return titleName;
}
}
throw new IllegalFieldValueException(title+" is not a recogernised title!");
}
}
/**
* @return the ismale
*/
public boolean getIsmale() {
return ismale;
}
/**
* @param ismale the ismale to set
*/
public void setIsmale(boolean ismale) {
this.ismale = ismale;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((dob == null) ? 0 : dob.hashCode());
result = PRIME * result + ((email == null) ? 0 : email.hashCode());
result = PRIME * result + ((firstname == null) ? 0 : firstname.hashCode());
result = PRIME * result + ((forenames == null) ? 0 : forenames.hashCode());
result = PRIME * result + (ismale ? 1231 : 1237);
result = PRIME * result + ((lastname == null) ? 0 : lastname.hashCode());
result = PRIME * result + ((nationality == null) ? 0 : nationality.hashCode());
result = PRIME * result + ((title == null) ? 0 : title.hashCode());
result = PRIME * result + ((user == null) ? 0 : user.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final PIForm other = (PIForm) obj;
if (dob == null) {
if (other.dob != null)
return false;
} else if (!dob.equals(other.dob))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (firstname == null) {
if (other.firstname != null)
return false;
} else if (!firstname.equals(other.firstname))
return false;
if (forenames == null) {
if (other.forenames != null)
return false;
} else if (!forenames.equals(other.forenames))
return false;
if (ismale != other.ismale)
return false;
if (lastname == null) {
if (other.lastname != null)
return false;
} else if (!lastname.equals(other.lastname))
return false;
if (nationality == null) {
if (other.nationality != null)
return false;
} else if (!nationality.equals(other.nationality))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
if (user == null) {
if (other.user != null)
return false;
} else if (!user.equals(other.user))
return false;
return true;
}
}
Here is the database schema
Code:
mysql> describe user;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | varchar(255) | NO | PRI | | |
| uname | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.02 sec)
mysql> describe pi_form;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| ra_id | int(11) | NO | PRI | | |
| userId | varchar(255) | NO | UNI | | |
| firstname | varchar(32) | YES | | NULL | |
| lastname | varchar(32) | YES | | NULL | |
| forenames | varchar(128) | YES | | NULL | |
| title | varchar(16) | YES | | NULL | |
| email | varchar(32) | YES | | NULL | |
| ismale | varchar(32) | YES | | NULL | |
| dob | datetime | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+