大家好,又见面了,我是你们的朋友全栈君。
具体做法是遍历对象对象的所有get方法,然后通过方法反射获取到对象的值。然后再判断值是否为空,从而组装HQL语句
public class testH {
public static void main(String[] args) {
Session session = sessionFactory.getCurrentSession();
//删除
TestT<TestBean> testT = new TestT();
TestBean testBean = new TestBean();
testBean.setName("zhan");
testBean.setCode("hhaa");
/*
* @Date: 2021/1/9 11:37
* 用session删除 (这种方式没有设置id 删除不了)
*/
session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.delete(testBean);
session.getTransaction().commit();
/*
* @Date: 2021/1/9 11:37
* 采用自己 构造删除HQL的方法删除
*/
List<Object> paramList = new ArrayList<>();
// createDeleteHql 方法详情看:https://blog.csdn.net/zhan107876/article/details/112389288
// createDeleteHql 方法 演示 完整工程 github:https://github.com/zhan107876/method-invoke-demo)
String deleteHql = testT.createDeleteHql(testBean, paramList);
// 构造hibernate查询
Query query = session.createQuery(deleteHql);
if (paramList != null) {
int size = paramList.size();
for (int i = 0; i < size; i++) {
query.setParameter(i, paramList.get(i));
}
}
// 执行删除操作
query.executeUpdate();
}
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/148727.html原文链接:https://javaforall.cn
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有