我正在尝试在两个 while 循环中插入许多行。我的理解是我必须在事务的循环体中创建一个单独的新瞬态实例。
根据我的经验,不创建单独的瞬态实例(循环体外部的瞬态实例)至少插入一行,即最后一行。在提交交易之前我save
只需要在这里使用一次吗?
在这种情况下使用flush()
和正确吗?clear()
我对hibernate比较陌生。我会感谢任何帮助或指出我正确的方向。
请原谅我蔑视变量和方法的骆驼案例约定。
public boolean addVariableHonorariumDetails(Monthly_variable_honorarium_processing mvhp
, Monthly_variable_group_honorarium_processing mvghp
, Monthlyvariablecompid compositeID)
{
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
String ccyymm = mvhp.getMvhp().getVariable_monthly_ccyy()+mvhp.getMvhp().getVariable_monthly_month();
try
{
tx = session.beginTransaction();
// ctrl query
M_control_table mct = (M_control_table) session.createQuery("from M_control_table").uniqueResult(); //single result
Query query = session.createSQLQuery(
"Select c.Booklet_type, SUM(c.End_Leaf - c.Start_Leaf +1) as No_of_Coupons from m_coupon_sale c where date_format(c.Booklet_Sale_Date, '%Y%m') = :string and c.Booklet_type in ('GR','PI') group by c.Booklet_Type")
.addScalar("c.Booklet_type", Hibernate.STRING)
.addScalar("No_of_Coupons", Hibernate.STRING)
.setString("string", ccyymm);
ScrollableResults results = query.scroll();
Integer sumTotalOfPayment = 0;
while ( results.next() )
{
Object[] row = results.get();
mvghp.setMvhpgroup(new Monthlyvariablecompid(mvhp.getMvhp().getVariable_monthly_month()
, mvhp.getMvhp().getVariable_monthly_ccyy()
, row[0].toString())
);
mvghp.setNo_of_items(row[1].toString());
Integer no_of_items = Integer.parseInt(mvghp.getNo_of_items(), 10);
mvghp.setGroup_rate( (row[0].toString().equals("GR")) ? mct.getRate_for_green_coupons()
: mct.getRate_for_pink_coupons() );
Integer variable_payment = no_of_items * Integer.parseInt( mvghp.getGroup_rate(), 10);
sumTotalOfPayment = sumTotalOfPayment + variable_payment;
mvghp.setVariable_payment(variable_payment.toString());
session.save(mvghp);
session.flush();
session.clear();
}
Query query2 = session.createSQLQuery(
"select m.pooja_name, count(*) as no_of_items from t_pooja_booking t inner join m_pooja_detail m on t.pooja_id=m.pooja_id where date_format(t.pooja_date, '%Y%m')= :string and m.pooja_name in ('A','B' ) group by m.pooja_name")
.addScalar("m.pooja_name", Hibernate.STRING)
.addScalar("no_of_items", Hibernate.STRING)
.setString("string", ccyymm);
ScrollableResults results1 = query2.scroll();
while ( results1.next() )
{
Object[] row = results1.get();
mvghp.setMvhpgroup(new Monthlyvariablecompid(mvhp.getMvhp().getVariable_monthly_month()
, mvhp.getMvhp().getVariable_monthly_ccyy()
, row[0].toString())
);
mvghp.setNo_of_items(row[1].toString());
Integer no_of_items = Integer.parseInt(mvghp.getNo_of_items(), 10);
mvghp.setGroup_rate((row[0].toString().equals("A"))?mct.getRate_for_108_vadamalas_cook()
:mct.getRate_for_51_vadamalas_cook() );
Integer variable_payment = no_of_items * Integer.parseInt(mvghp.getGroup_rate() , 10) ;
sumTotalOfPayment = sumTotalOfPayment + variable_payment;
mvghp.setVariable_payment(variable_payment.toString());
session.save(mvghp);
session.flush();
session.clear();
}
mvhp.setTotal_variable_payment(sumTotalOfPayment.toString());
session.save(mvhp);
tx.commit();
}
catch(Exception e)
{
if(tx!=null)
{
tx.rollback();
}
e.printStackTrace();
return false;
}
finally
{
session.close();
}
return true;
}