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