-->
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: Problem in accessing duplicate primary key
PostPosted: Tue Jun 26, 2007 6:01 am 
Newbie

Joined: Tue Jun 26, 2007 5:45 am
Posts: 4
Currently we are using oracle database which is not normalize.
e.g. We have a table Position which have these columns
1) Id
2) Desc

Example of record in this table is
ID DESC
P Partner1
A Associate
L Paralegal
P Partner
P Partner

Id is declared as primary key in POJO
I tried the following code
Query query=getSession().createQuery("from "+type+" t where t.id is not null");
insurances=query.list();
log.debug("Records are ="+insurances);

This returns the 5 rows as describe above
But when print the list I got the following values
P Partner1
A Associate
L Paralegal
P Partner1
P Partner1

The problem with the above result is duplication of Partner1 thric. Ideal answer should be like
P Partner1
A Associate
L Paralegal
P Partner

Please suggest me. Thanx in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 26, 2007 9:01 am 
Newbie

Joined: Tue Jun 26, 2007 8:53 am
Posts: 5
Why is ID primary key on your POJO when records in base tables aren't structured that way? You could use composite key if it better suits your application.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 26, 2007 9:24 am 
Beginner
Beginner

Joined: Mon Apr 23, 2007 8:30 am
Posts: 27
Location: India
This can be definitely done, with a little twist :-). Here is the trick.

The Mapping file will look like this:-

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>
    <class name="projectmember.Position" table="POSITION">
       <id name="id" type="string">
            <column name="ID" length="3" />
            <generator class="assigned" />
        </id>
        <property name="description" type="string">
            <column name="DESCRIPTION" length="25" />
        </property>
      </class>
    <query name="Position.findByID">
      <![CDATA[SELECT distinct position.id, position.description from projectmember.Position as position
            where position.id= (:id)
      ]]>
   </query>
</hibernate-mapping>


Notice the query section carefully, we are selecting the columns individually rather than selecting the whole object.

Add the following/similar code to your DAO or whatever that executes the Hibernate query:-

Code:
public List findByNonUniqueId(String id){
      Session session = getHibernateTemplate().getSessionFactory().openSession();
      Query query = session.getNamedQuery("Position.findByID");
      query.setParameter("id", id);
      
      List positionListFromQuery = query.list();
      
      List positionList = new ArrayList();
      Iterator it = positionListFromQuery.iterator();
      while (it.hasNext()){
         Object [] positionArray =(Object[]) it.next();
         Position position = new Position();
         position.setId(positionArray[0].toString());
         position.setDescription(positionArray[1].toString());
         positionList.add(position);
      }   
      session.close();
      return positionList;
   }


I got the following output on running the above code:-

Code:
Hibernate: select position0_.ID as col_0_0_, position0_.DESCRIPTION as col_1_0_ from POSITION position0_ where position0_.ID=?
List Size - 2
ID - P Description - Partner
ID - P Description - Partner1


Hope this helps. If it does, dont forget to rate:-)

PS: Ideally, I would add a primary key to the table, someway or the other. That is the right way to go :-)

Thanks,
Nikhil


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.