I am having one table.This table do'nt have any primary key.I am trying to fetch records
For the identifiers RegNumber and linNumber there are 3 rows in the table.When I fetch
the object.I get first object 3 times insteed to 3 diferent objects.
I read in Hibernate documentation that
if, within a single session, you request two objects which have the same database identifier,
then you will get references to the same actual objects.
Following is the code:
[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 package="domain.model">
<class name="Comp" table="COMP_IND">
<composite-id name="key">
<key-property name="regNumber" column="REG_NUMBER"/>
<key-property name="linNumber" column="LIN_NUMBER"/>
</composite-id>
<property name="lastName"
type="string"
column="LASTNAME"
update="false"/>
<property name="firstName"
type="string"
column="FIRSTNAME"
update="false"/>
<property name="middleName"
column="MIDDLENAME"
type="string"
update="false"/>
<property name="fullName"
column="FULLNAME"
type="string"
update="false"/>
<property name="sex"
column="SEX"
type="string"
update="false"/>
<property name="birthDate"
column="BIRTHDATE"
type="java.util.Date"
update="false"/>
<property name="companyName"
column="COMPANY_NAME"
type="string"
update="false"/>
<property name="groupName"
column="GROUP_NAME"
type="string"
update="false"/>
</class>
</hibernate-mapping>[/code]
I have created the class for composite key but this key is not unique.
2.Class Comp.java has following methods with all the gettere and setters.
[code]public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Comp)) return false;
final Comp uo = (Comp) o;
if (key != null ? !key.equals(uo.key) : uo.key != null) return false;
return true;
}
public int hashCode() {
return (key != null ? key.hashCode() : 0);
}[/code]
3.Folowing is the Sql querry
[code]List info = getHibernateTemplate().find("from Comp as e where e.key.regNumber= ? and e.key.linNumber= ? ",
new Object[]{"34343434","444544"});
for (Iterator it = insurances.iterator(); it.hasNext(); ) {
Comp cmp = (Comp)it.next();
String Company = cmp.getCompanyName();
System.out.println("Company is " + Company);
} [/code]
This query is supposed to return 3 different objects but returning First object 3 times.
I will appreciate if some body tell me how to deal with table having no primary key.
Thanks,
|