我正在使用没有注释的Hibernate 3.0。我有我的DAO方法,它与db交互并将数据发送到servlet。
在这个用例中,我希望在我使用第一个查询的许多表中插入一些记录。在第二个查询中,我需要在插入后更新数据库值。现在,我想基于规范更改添加另一个查询,这些更改必须持久存储到具有mhp
和mhvp_new
实例的数据库。我该怎么做?我可以在同一个会话中执行此操作,就像我为查询2所做的那样。
任何帮助都将不胜感激。
public boolean addHonorariumDetails(Monthly_volunteer_honorarium_processing mvhp, Monthly_honorarium_processing mhp, Monthlyhonorariumcompid mhid, List list)
{
Session session=HibernateUtil.getSessionFactory().openSession();
Transaction tx=null;
tx=session.beginTransaction();
try
{
Date temp = new SimpleDateFormat("yyyy-MM").parse(mhid.getMonthly_volunteer_ccyy()+"-"+mhid.getMonthly_volunteer_month());
Calendar cal=Calendar.getInstance();
cal.setTime(temp);
int lastDay = cal.getActualMaximum(cal.DAY_OF_MONTH);
String ccyymm = mhid.getMonthly_volunteer_ccyy()+mhid.getMonthly_volunteer_month();
String fullDate = mhid.getMonthly_volunteer_ccyy()+"-"+mhid.getMonthly_volunteer_month();
System.out.println("full date "+fullDate);
int totalGross = 0;
int totalAdvances = 0;
int totalNetPayment = 0;
int totalVolunteers = 0;
Monthly_volunteer_honorarium_processing mvhp_new = new Monthly_volunteer_honorarium_processing();
Monthly_honorarium_processing mhp_new = new Monthly_honorarium_processing();
Query q=session.createSQLQuery("select m.volunteer_id, m.monthly_emolument, sum(case when period_diff('"+ccyymm+"', date_format(v.advance_date, '%Y%m'))>=2 and v.installment_pending>0 then v.emi else 0 end) as aliaas from m_volunteer m left join volunteer_advance v on m.volunteer_id=v.volunteer_id where m.active_status='active' and m.volunteer_role!=(select m_volunteer_role from m_volunteer_role where m_volunteer_role_description='Priest') and m.m_deity_dependent_honorarium!='true' group by m.volunteer_id")
.addScalar("m.volunteer_id", Hibernate.INTEGER)
.addScalar("m.monthly_emolument", Hibernate.INTEGER)
.addScalar("aliaas", Hibernate.INTEGER);
ScrollableResults results = q.scroll();
while(results.next())
{
Object[] row = (Object[]) results.get();
System.out.println("rows " + row[0].toString() +" "+ row[2].toString() +" "+ row[1].toString());
Monthlyhonorariumcompid mhid_new = new Monthlyhonorariumcompid(mhid.getMonthly_volunteer_month(), mhid.getMonthly_volunteer_ccyy(), row[0].toString());
mvhp_new.setMonthlyhonorariumcompid(mhid_new);
mvhp_new.setGross_honorarium_amount(row[1].toString());
mvhp_new.setTotal_advances(row[2].toString());
Integer netPayment = (Integer)row[1] - (Integer)row[2];
mvhp_new.setNet_payment(netPayment.toString());
mvhp_new.setUpdated_by(mvhp.getUpdated_by());
mvhp_new.setUpdated_date_time(mvhp.getUpdated_date_time());
totalGross = totalGross + (Integer)row[1];
totalAdvances = totalAdvances + (Integer)row[2];
totalNetPayment = totalNetPayment + netPayment;
totalVolunteers++;
session.save(mvhp_new);
session.flush();
session.clear();
if(results.isLast())
{
Query q1 = session.createSQLQuery("select v.volunteer_advance_id, v.installment_pending from volunteer_advance v where v.installment_pending>0 and period_diff('"+ccyymm+"', date_format(v.advance_date, '%Y%m'))")
.addScalar("v.volunteer_advance_id", Hibernate.INTEGER)
.addScalar("v.installment_pending", Hibernate.INTEGER);
ScrollableResults scrl = q1.scroll();
while(scrl.next())
{
Object[] tuple = (Object[]) scrl.get();
M_volunteer_advance mva = (M_volunteer_advance) session.load(M_volunteer_advance.class, tuple[0].toString());
Integer decrementCount = (Integer)row[1]; // bug
decrementCount--;
mva.setInstallment_pending(String.valueOf(decrementCount));
session.update(mva);
session.flush();
session.clear();
}
}
}
mhp.setTotal_volunteers_processed(new Integer(totalVolunteers).toString());
mhp.setGross_honorarium_amount(new Float(totalGross).toString());
mhp.setTotal_advances(new Double(totalAdvances).toString());
mhp.setNet_payment(new Double(totalNetPayment).toString());
session.save(mhp);
tx.commit();
}
catch(Exception e)
{
tx.rollback();
e.printStackTrace();
return false;
}
finally
{
session.close();
}
return true;
}