I am posting the whole code. I have changed to cascade="delete-orphan" but still getting the same error. Is there a problem with the maping
Code:
/*
* Created on Mar 11, 2006
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.uprr.app.eco;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.uprr.app.eco.persistenceDelete.EcoCmdyDelete;
import com.uprr.app.eco.util.HibernateUtil;
public class DeleteOldData {
public static final Logger logger = Logger.getLogger(DeleteOldData.class);
public static final Date TODAYS_RECORD_TIME1 =new Date();
public static final String PROP_FILE_STR = "prop.file";
public static final String HIBERNATE_CFG_FILE_STR = "hibernate.config";
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
public static final Timestamp TODAYS_RECORD_TIME =new Timestamp(System.currentTimeMillis());
private static String hiberConfig = null;
private static Session session = null;
private static void init()
{
TODAYS_RECORD_TIME.setHours(0);
TODAYS_RECORD_TIME.setMinutes(0);
TODAYS_RECORD_TIME.setSeconds(0);
String propFile = System.getProperty(PROP_FILE_STR);
if (propFile == null || propFile.trim().equals("")) {
System.out.println("Usage java -D"+PROP_FILE_STR);
System.exit(1);
}
Properties prop = new Properties();
InputStream fin = null;
try {
fin = new FileInputStream(propFile);
prop.load(new FileInputStream(propFile));
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fin != null) {
fin.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
hiberConfig = (String) prop.get(HIBERNATE_CFG_FILE_STR);
session=HibernateUtil.currentSession(hiberConfig);
}
/**
*
*
*/
public static void deleteOldDataSQL()
{
try{
String delSql = " delete EcoCmdyDelete where lastUptdDt< :lastUpDt ";
Query delQry = session.createQuery(delSql);
delQry.setTime("lastUpDt", TODAYS_RECORD_TIME );
delQry.executeUpdate();
}
catch(Exception e)
{
System.out.println("deleteOldDataSQL" + e.getMessage());
}
}
public static void deleteOldData()
{
Transaction tx=session.beginTransaction();
try{
System.out.println("deleteOldData");
String delSql = "from EcoCmdyDelete c where c.lastUptdDt< :lastUpDt ";
Query delQry = session.createQuery(delSql);
delQry.setTime("lastUpDt", TODAYS_RECORD_TIME );
List qList =delQry.list();
int count =0;
if(qList!=null && qList.size()>0 )
{
Iterator i= qList.iterator();
while(i.hasNext() && count<1)
{
count++;
EcoCmdyDelete cmdy =(EcoCmdyDelete)i.next();
// Iterator htdt=cmdy.getEcoHzmtDtls().iterator();
// if( htdt.hasNext())
// {
// session.delete(htdt.next());
// }
// Iterator htemr=cmdy.getEcoHzrdEmgyResps().iterator();
// if(htemr.hasNext())
// {
// session.delete(htemr.next());
// }
// Iterator htdtNms=cmdy.getEcoHzmtNameDescs().iterator();
// if( htdtNms.hasNext())
// {
// session.delete(htdtNms.next());
// }
// Iterator prdcls=cmdy.getEcoCommodities().iterator();
// if( prdcls.hasNext())
// {
// session.delete(prdcls.next());
// }
session.delete( cmdy );
}
}
//
tx.commit();
}
catch(HibernateException e)
{
System.out.println("error HibernateException in deleting the records " + e.getMessage());
e.printStackTrace();
}
catch(Exception e2)
{
System.out.println("error Exception in deleting the records" );
e2.printStackTrace();
}
finally{
if(session!=null);
{
session.close();
}
}
}
public static void main(String[]args)
{
try{
logger.debug("Deleting records not updated with the current load");
init();
deleteOldData();
// deleteOldDataSQL();
logger.debug("Successfully Deleted older records from the database");
}
catch(Exception e )
{
logger.debug("Error in Deleting records: "+ e.getStackTrace());
}
}
}
From logs, they look ok
[Scheduling collection removes/(re)creates/updates]
[2006-03-13 21:53:57,956][DEBUG] [org.hibernate.event.def.AbstractFlushingEventListener] [Flushed: 0 insertions, 18038 updates, 1 deletions to 18039 objects]
[2006-03-13 21:53:57,956][DEBUG] [org.hibernate.event.def.AbstractFlushingEventListener] [Flushed: 0 (re)creations, 0 updates, 4 removals to 72156 collections]
03-13 21:53:57,956][DEBUG] [org.hibernate.event.def.AbstractFlushingEventListener] [executing flush]
[2006-03-13 21:53:57,972][DEBUG] [org.hibernate.persister.entity.BasicEntityPersister] [Updating entity: [com.uprr.app.eco.persistenceDelete.EcoCmdyDelete#01 ]]
[2006-03-13 21:53:57,972][DEBUG] [org.hibernate.jdbc.AbstractBatcher] [about to open PreparedStatement (open PreparedStatements: 0, globally: 0)]
[2006-03-13 21:53:57,972][DEBUG] [org.hibernate.SQL] [update ECO.ECO_CMDY set HZRD_IND=?, EFF_DATE=?, CMDY_ABRV=?, INTL_HMZD_CODE=?, SVC_RQMT_CODE_1=?, SVC_RQMT_CODE_2=?, SVC_RQMT_CODE_3=?, WGT_TOLR_FCTR=?, CMDY_BASE_NBR=?, UNNA_NBR=?, CRTN_DT=?, CRTN_USER_ID=?, LAST_UPTD_DT=?, LAST_UPTD_USER_ID=?, CMDY_DESC=? where STCC_CODE=?]
[2006-03-13 21:53:57,972][DEBUG] [org.hibernate.jdbc.AbstractBatcher] [preparing statement]
[2006-03-13 21:53:57,972][DEBUG] [org.hibernate.persister.entity.BasicEntityPersister] [Dehydrating entity: [com.uprr.app.eco.persistenceDelete.EcoCmdyDelete#01 ]]
[2006-03-13 21:53:57,972][DEBUG] [org.hibernate.type.StringType] [binding null to parameter: 1]
[2006-03-13 21:53:57,972][DEBUG] [org.hibernate.type.DateType] [binding null to parameter: 2]
[2006-03-13 21:53:57,972][DEBUG] [org.hibernate.type.StringType] [binding null to parameter: 3]
[2006-03-13 21:53:57,972][DEBUG] [org.hibernate.type.StringType] [binding '000000' to parameter: 4]
[2006-03-13 21:53:57,972][DEBUG] [org.hibernate.type.StringType] [binding '0' to parameter: 5]
[2006-03-13 21:53:57,972][DEBUG] [org.hibernate.type.StringType] [binding '0' to parameter: 6]
[2006-03-13 21:53:57,972][DEBUG] [org.hibernate.type.StringType] [binding '0' to parameter: 7]
[2006-03-13 21:53:57,972][DEBUG] [org.hibernate.type.IntegerType] [binding '0' to parameter: 8]
[2006-03-13 21:53:57,987][DEBUG] [org.hibernate.impl.SessionImpl] [closing session]
[2006-03-13 21:53:57,987][DEBUG] [org.hibernate.jdbc.AbstractBatcher] [closing JDBC connection (open PreparedStatements: 1, globally: 1) (open ResultSets: 0, globally: 0)]
[2006-03-13 21:53:57,987][DEBUG] [com.uprr.app.eco.DeleteOldData] [Successfully Deleted older records from the database]