-->
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.  [ 3 posts ] 
Author Message
 Post subject: Fetching Objects from Table having no primary key.
PostPosted: Wed Dec 27, 2006 5:25 pm 
Newbie

Joined: Fri Dec 22, 2006 10:45 am
Posts: 5
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,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 27, 2006 5:29 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
you have to have a primary key ....

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 27, 2006 6:00 pm 
Newbie

Joined: Fri Dec 22, 2006 10:45 am
Posts: 5
I know primary key is required.I was wondering if there is some workarround to deal with the table with no primary key.

Thanks,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.