Hi,
Please help me resolve this problem.
Iam unable to retrieve records if i have date field as part of the composite-id. When I remove as part of the id, it works. But i need to have the field as part of primary key.
Thanks in Advance.
1) Iam using Hibernate version 3.0.
2) My mapping file Activity.hbm.xml
<?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="com.example.vo.Activity" table="Activity">
<composite-id>
<key-property name="acvyDt" column="Acvy_Dt" type="java.lang.String"/>
<key-property name="acvyTypCd" column="Acvy_Typ_Cd" type="java.lang.String"/>
<key-property name="acvyNo"><column name="Acvy_No"/></key-property>
</composite-id>
<property name="updDt" column="Upd_Dt" type="java.sql.Timestamp" not-null="true" />
<property name="updUsrId" column="Upd_Usr_ID" type="java.lang.String" not-null="true" />
</class>
</hibernate-mapping>
3) My vo class
Activity.java
public class Activity implements Serializable
{
/** The cached hash code value for this instance. Settting to 0 triggers re-calculation. */
private int hashValue = 0;
/** The value of the Acvy_Dt component of this composite id. */
private java.sql.Timestamp acvyDt;
/** The value of the Acvy_Typ_Cd component of this composite id. */
private java.lang.String acvyTypCd;
/** The value of the Acvy_No component of this composite id. */
private java.lang.String acvyNo;
/** The value of the simple updDt property. */
private java.sql.Timestamp updDt;
/** The value of the simple updUsrId property. */
private java.lang.String updUsrId;
/**
* Returns the value of the acvyDt property.
* @return java.sql.Timestamp
*/
public java.sql.Timestamp getAcvyDt()
{
return acvyDt;
}
/**
* Sets the value of the acvyDt property.
* @param acvyDt
*/
public void setAcvyDt(java.sql.Timestamp acvyDt)
{
hashValue = 0;
this.acvyDt = acvyDt;
}
/**
* Returns the value of the acvyTypCd property.
* @return java.lang.String
*/
public java.lang.String getAcvyTypCd()
{
return acvyTypCd;
}
/**
* Sets the value of the acvyTypCd property.
* @param acvyTypCd
*/
public void setAcvyTypCd(java.lang.String acvyTypCd)
{
hashValue = 0;
this.acvyTypCd = acvyTypCd;
}
/**
* Returns the value of the acvyNo property.
* @return java.lang.String
*/
public java.lang.String getAcvyNo()
{
return acvyNo;
}
/**
* Sets the value of the acvyNo property.
* @param acvyNo
*/
public void setAcvyNo (java.lang.String acvyNo )
{
hashValue = 0;
this.acvyNo = acvyNo;
}
/**
* Return the value of the Upd_Dt column.
* @return java.sql.Timestamp
*/
public java.sql.Timestamp getUpdDt()
{
return this.updDt;
}
/**
* Set the value of the Upd_Dt column.
* @param updDt
*/
public void setUpdDt(java.sql.Timestamp updDt)
{
this.updDt = updDt;
}
/**
* Return the value of the Upd_Usr_ID column.
* @return java.lang.String
*/
public java.lang.String getUpdUsrId()
{
return this.updUsrId;
}
/**
* Set the value of the Upd_Usr_ID column.
* @param updUsrId
*/
public void setUpdUsrId(java.lang.String updUsrId)
{
this.updUsrId = updUsrId;
}
/**
* Implementation of the equals comparison on the basis of equality of the primary key values.
* @param rhs
* @return boolean
*/
public boolean equals(Object rhs)
{
if (rhs == null)
return false;
if (! (rhs instanceof Activity))
return false;
Activity that = (Activity) rhs;
if (this.getAcvyNo() != null && that.getAcvyNo() != null)
{
return (this.getAcvyNo().equals(that.getAcvyNo()));
}
return true;
}
/**
* Implementation of the hashCode method conforming to the Bloch pattern with
* the exception of array properties (these are very unlikely primary key types).
* @return int
*/
public int hashCode()
{
if (this.hashValue == 0)
{
int result = 17;
if (this.getAcvyNo() == null)
{
result = super.hashCode();
}
else
{
result = this.getAcvyNo().hashCode();
}
this.hashValue = result;
}
return this.hashValue;
}
}
4) the database we r using is sybase 12.1
5) The java class executing the query
public Activity getActivities (String acvy) {
Activity acvy = null;
try {
Session s = HibernateUtil.currentSession();
Transaction tr = s.beginTransaction();
List acvyList = s.createCriteria(Activity.class)
.add(Restrictions.eq("acvyNo", acvy))
.addOrder(Order.desc("acvyDt")).list();
if (acvyList!= null && acvyList.size() > 0)
acvy = (Activity)acvyList.get(0);
} catch (Exception e) {
e.printStackTrace();
}
return acvy;
}
public static void main(String[] args) throws Exception {
retrieveData();
}
public static void retrieveData () {
try {
System.out.println ("retrieveData:");
String acvynum = "111111";
Activity pog = dao.getActivities (acvynum);
System.out.println ("Finished retrieval:");
}catch (Exception e) {
e.printStackTrace();
}
}
5) The exception i got is
Hibernate: select this_.Acvy_Dt as Acvy1_0_, this_.Acvy_Typ_Cd as Acvy2_0_, this_.Acvy_No as Acvy3_0_, this_.Upd_Dt as Upd16_6_0_, this_.Upd_Usr_ID as Upd17_6_0_ from Activity this_ where this_.Acvy_No=? order by this_.Acvy_Dt desc
java.lang.UnsupportedOperationException: cannot perform lookups on timestamps
at org.hibernate.type.TimestampType.getHashCode(TimestampType.java:99)
at org.hibernate.type.AbstractType.getHashCode(AbstractType.java:119)
at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:162)
at org.hibernate.engine.EntityKey.getHashCode(EntityKey.java:68)
at org.hibernate.engine.EntityKey.<init>(EntityKey.java:41)
at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:707)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:282)
at org.hibernate.loader.Loader.doQuery(Loader.java:389)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:208)
at org.hibernate.loader.Loader.doList(Loader.java:1522)
at org.hibernate.loader.Loader.list(Loader.java:1505)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:111)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1256)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:299)
at com.example.dao.ActivityDAO.getActivites(ActivityDAO.java:85)
at com.example.test.ActivityRetrievalTest.retrieveData(ActivityRetrievalTest.java:84)
at com.example.test.ActivityRetrievalTest.main(ActivityRetrievalTest.java:47)
|