当前位置: 首页 > news >正文

网站做可信认证芜湖网站

网站做可信认证,芜湖网站,wordpress redis memcached,新闻源软文推广平台您可能会遇到必须对关系数据库中存储的大量数据集执行批量删除的情况。 如果您将JPA与Hibernate一起用作基础OR映射器#xff0c;则可以尝试通过以下方式调用EntityManager的remove#xff08;#xff09;方法#xff1a; public void removeById(long id) {RootEntity ro… 您可能会遇到必须对关系数据库中存储的大量数据集执行批量删除的情况。 如果您将JPA与Hibernate一起用作基础OR映射器则可以尝试通过以下方式调用EntityManager的remove方法 public void removeById(long id) {RootEntity rootEntity entityManager.getReference(RootEntity.class, id);entityManager.remove(rootEntity); } 首先我们加载要删除的实体的引用表示形式然后将此引用传递给EntityManager。 假设上面的RootEntity与名为ChildEntity的类有子关系 OneToMany(mappedBy rootEntity, fetch FetchType.EAGER, cascade CascadeType.ALL) private Set childEntities new HashSet(0); 如果现在打开hibernate的属性show_sql我们将想知道发出什么SQL语句 selectrootentity0_.id as id5_1_,rootentity0_.field1 as field2_5_1_,rootentity0_.field2 as field3_5_1_,childentit1_.PARENT as PARENT5_3_,childentit1_.id as id3_,childentit1_.id as id4_0_,childentit1_.field1 as field2_4_0_,childentit1_.field2 as field3_4_0_,childentit1_.PARENT as PARENT4_0_fromROOT_ENTITY rootentity0_left outer joinCHILD_ENTITY childentit1_on rootentity0_.idchildentit1_.PARENTwhererootentity0_.id?deletefromCHILD_ENTITYwhereid?deletefromROOT_ENTITYwhereid? 为什么Hibernate首先将所有数据加载到内存中以便随后立即删除该数据 原因是JPA的生命周期要求该对象处于“托管”状态然后才能将其删除。 仅在这种状态下所有生命周期功能如拦截器才可用请参阅此处 。 因此Hibernate会在删除之前发出SELECT查询以便将RootEntity和ChildEntity都转移到“托管”状态。 但是如果我们只想删除RootEntity和ChildEntity并且知道RootEntity的ID该怎么办 答案是使用简单的DELETE查询如下面的查询。 但是由于子表的完整性约束我们首先必须删除所有依赖的子实体。 以下代码演示了如何 List childIds entityManager.createQuery(select c.id from ChildEntity c where c.rootEntity.id :pid).setParameter(pid, id).getResultList(); for(Long childId : childIds) {entityManager.createQuery(delete from ChildEntity c where c.id :id).setParameter(id, childId).executeUpdate(); } entityManager.createQuery(delete from RootEntity r where r.id :id).setParameter(id, id).executeUpdate(); 上面的代码通过调用remove产生了我们期望的三个SQL语句。 现在您可能会说这种删除方式比仅调用EntityManager的remove方法更为复杂。 它还会忽略我们已放置在两个实体类中的注解例如OneToMany和ManyToOne。 那么为什么不编写一些代码来使用关于两个类文件中已经存在的两个实体的知识呢 首先我们在RootEntity类中使用反射查找OneToMany批注提取子实体的类型然后查找其后向字段并用ManyToOne批注。 完成此操作后我们可以轻松地以更通用的方式编写三个SQL语句 public void delete(EntityManager entityManager, Class parentClass, Object parentId) {Field idField getIdField(parentClass);if (idField ! null) {List oneToManyFields getOneToManyFields(parentClass);for (Field field : oneToManyFields) {Class childClass getFirstActualTypeArgument(field);if (childClass ! null) {Field manyToOneField getManyToOneField(childClass, parentClass);Field childClassIdField getIdField(childClass);if (manyToOneField ! null childClassIdField ! null) {List childIds entityManager.createQuery(String.format(select c.%s from %s c where c.%s.%s :pid, childClassIdField.getName(), childClass.getSimpleName(), manyToOneField.getName(), idField.getName())).setParameter(pid, parentId).getResultList();for (Long childId : childIds) {entityManager.createQuery(String.format(delete from %s c where c.%s :id, childClass.getSimpleName(), childClassIdField.getName())).setParameter(id, childId).executeUpdate();}}}}entityManager.createQuery(String.format(delete from %s e where e.%s :id, parentClass.getSimpleName(), idField.getName())).setParameter(id, parentId).executeUpdate();} } 上面的代码中的方法getFirstActualTypeArgumentgetManyToOneFieldgetIdField和getOneToManyFields并未在此处显示但是听起来像它们的名字。 实施后我们可以轻松删除所有以树的根开头的实体。 可以在github上找到一个可用于检查上述行为和解决方案的简单示例应用程序。 参考在Martins Developer World博客上 使用JPA和Hibernate从我们的JCG合作伙伴 Martin Mois 有效地删除了数据 。 翻译自: https://www.javacodegeeks.com/2013/11/efficiently-delete-data-with-jpa-and-hibernate.html
http://www.yutouwan.com/news/367449/

相关文章:

  • 泰州网站建设策划广州外贸网站开发
  • 做视频自媒体要投稿几个网站网站的流量建设
  • 哪个找房网站好做外贸无法登录国外网站怎么办
  • 手机网页设计网站建设企业网站建设的征求意见
  • 莆田 做网站的公司三乡网站建设
  • 济南网站开发薪酬龙岩全网搜系统开发
  • 360建站模板手机4g建立网站
  • asp汽车销售公司网站源码 4s店网站源码 汽车网站建设 完整无潍坊市住房和城乡建设网站
  • 建材建设网站做网站是什么专业
  • 虚拟主机销售网站模板苏州高端网站制作机构
  • 北京建设网站公司网站承建商有哪些
  • 免费软件不收费网站医疗网站建设好么
  • 英文网站建设方案模板高校北京公司网站建设公司
  • 网站建设与管理 管理课程快点tv下载安装
  • 专业企业网站建设哪家服务好河南重大项目建设网站
  • 网站源码之家oa软件开发公司
  • 做网站年薪百万购物网站cookie
  • 可直接打开网站的网页微信如何做积分商城网站
  • 比较还做的调查网站个人网站备案简介
  • 微网站的优势怎么样让客户做网站和小程序
  • 一般网站字体织梦手机网站
  • 深圳建设局网站首页怎样在建设部网站上查公司信息
  • 做网站 就上宝华建站石家庄外贸建站公司
  • 丽江旅游网站建设wordpress畅言怎么样
  • 中国对外贸易网站怎么做网站页面代码搜索
  • 北京网站设计公司招聘信息微信公众号和小程序的区别
  • 手机创建网站教程wordpress电影主题公园
  • 建设银行 网站无法打开wordpress一键登录
  • wordpress邮箱插件网站seo应用
  • 广东华星建设集团网站工程建设项目管理系统平台