Hibernate大量刪除的兩種方式

來源:互聯網
上載者:User

標籤:nts   ace   info   exce   sql   int   over   i++   port   

第一種:使用Hibernate映射類對對象進行封裝操作

---------------------------------------------------------------------------

@Override
    public boolean deleteTrainee(long[] id) {
        try {
            Session session = HibernateSessionFactory.getSession();
            Transaction ts = session.beginTransaction();
            
            //採用hibernate映射類的方法進行刪除
            for (long idList: id) {
                Trainee t = (Trainee)session.load(Trainee.class, idList);
                if (t != null)
                    session.delete(t);
            }
            ts.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            HibernateSessionFactory.closeSession();
        }
    }

---------------------------------------------------------------------------

第二種:採用hql語句,拼接字串設定預留位置的方式進行操作

---------------------------------------------------------------------------

@Override
    public boolean deleteSupportTracking(Long[] id) {
        try {
            Session session = HibernateSessionFactory.getSession();
            Transaction ts = session.beginTransaction();
            
            //設定預留位置的個數
            String sql = "";
            for (int i = 0; i < id.length; i++) {
                if (i == 0) {
                    sql = sql + "?";
                } else {
                    sql = sql + "," + "?";
                }
            }
            Query Query = session.createQuery("DELETE FROM SupportTracking WHERE id in(" + sql + ")");
            //設定預留位置中的參數
            Long[] ints = new Long[id.length];
            for(int i=0; i<id.length; i++){
                ints[i] = id[i];
                Query.setParameter(i, ints[i]);
            }
                int k = Query.executeUpdate();
            ts.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            HibernateSessionFactory.closeSession();
        }
    }

---------------------------------------------------------------------------

2.1網上抄來的寫法,未測試(先放這,有空再試試)

---------------------------------------------------------------------------

public void deletes(List<Integer> idList) {
String hql = "";
for (int i = 0; i < idList.size(); i++) {
if(i==0) {
hql = "id="+idList.get(i);
} else {
hql =hql + " or id="+idList.get(i);
}
Session session= this.getSession();
Query q= session.createQuery("delete from Timeliftinfo where "+hql);
q.executeUpdate();
}

}

---------------------------------------------------------------------------

Hibernate大量刪除的兩種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.