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

网站建设注意细节问题然后制作网页

网站建设注意细节问题,然后制作网页,手机网站 尺寸,竞价服务托管公司让我们介绍另一个休眠性能提示。 你还记得以前的休眠的模式后 #xff1f; 我们有一个与一对多协会有关的星际飞船和军官。 Entity public class Starship {Id GeneratedValue(strategyGenerationType.SEQUENCE) private Long id;public Long getId() {return id;}protected v… 让我们介绍另一个休眠性能提示。 你还记得以前的休眠的模式后 我们有一个与一对多协会有关的星际飞船和军官。 Entity public class Starship {Id GeneratedValue(strategyGenerationType.SEQUENCE) private Long id;public Long getId() {return id;}protected void setId(Long id) {this.id id;}OneToMany(mappedBystarship, cascade{CascadeType.ALL}) private ListOfficer officers new ArrayListOfficer();public ListOfficer getOfficers() {return Collections.unmodifiableList(officers);}protected void setOfficers(ListOfficer officers) {this.officers officers;}public void addOfficer(Officer officer) {officer.setStarship(this);this.officers.add(officer);}//more code }Entity public class Officer {Id GeneratedValue(strategyGenerationType.SEQUENCE) private Long id;public Long getId() {return id;}protected void setId(Long id) {this.id id;}ManyToOne private Starship starship; public Starship getStarship() {return starship;}protected void setStarship(Starship starship) {this.starship starship;}//more code } 现在我们有下一个要求 我们将按字母顺序将所有军官分配给星际飞船。 为了解决这个要求我们可以 使用order by子句实现HQL查询。 使用排序方法。 使用订单方法。 第一个解决方案在性能方面不错但是作为开发人员意味着更多的工作因为我们应该编写一个查询来查找按名称排序的给定飞船的所有人员然后在DAO层中创建finder方法如果您使用的是DAO模式 。 让我们探索第二个解决方案我们可以使用SortedSet类作为关联并使Officer实现Comparable 因此Officer具有 自然秩序。 该解决方案的工作量少于第一个但需要在关联定义上使用Sort 休眠注释因此让我们修改以前的模型以满足我们的新要求。请注意 JPA规范中没有等效的注释。 首先我们要实施 Entity public class Officer implements ComparableOfficer{//getters, setters, equals, ... codepublic int compareTo(Officer officer) {return this.name.compareTo(officer.getName());}} 官员类中的可比接口。 我们通过简单地比较名称字段来按名称订购人员。 下一步是使用Sort注释关联。 Entity public class Starship {//more codeOneToMany(mappedBystarship, cascade{CascadeType.ALL})Sort(typeSortType.NATURAL)private SortedSetOfficer officers new TreeSetOfficer();public SortedSetOfficer getOfficers() {return Collections.unmodifiableSortedSet(officers);}protected void setOfficers(SortedSetOfficer officers) {this.officers officers;}public void addOfficer(Officer officer) {officer.setStarship(this);this.officers.add(officer);} } 注意现在使用SortedSet而不是List来实现人员关联。 此外我们在关系中添加了Sort批注表明官员应自然而有序。 在完成本文之前我们将在Sort主题中坚持使用更多内容但到目前为止已经足够。 最后是一种方法该方法可以按名称对给定星际飞船的所有人员进行排序并将其打印在日志文件中。 EntityManager entityManager this.entityManagerFactory.createEntityManager(); EntityTransaction transaction entityManager.getTransaction();transaction.begin(); log.info(Before Find Starship By Id);Starship newStarship entityManager.find(Starship.class, starshipId); SortedSetOfficer officers newStarship.getOfficers();for (Officer officer : officers) {log.info(Officer name {} with rank {}, officer.getName(), officer.getRank()); }log.info(After Find Starship By Id and Before Commit);transaction.commit(); entityManager.close(); 所有人员均按其姓名排序但让我们检查将哪些查询发送到RDBMS 。 Hibernate: select starship0_.id as id1_0_, starship0_.affiliationEnum as affiliat2_1_0_, starship0_.launched as launched1_0_, starship0_.height as height1_0_, starship0_.length as length1_0_, starship0_.power as power1_0_, starship0_.width as width1_0_, starship0_.registry as registry1_0_, starship0_.starshipClassEnum as starship9_1_0_ from Starship starship0_ where starship0_.id?Hibernate: select officers0_.starship_id as starship7_1_1_, officers0_.id as id1_, officers0_.id as id0_0_, officers0_.affiliationEnum as affiliat2_0_0_, officers0_.homePlanet as homePlanet0_0_, officers0_.name as name0_0_, officers0_.rank as rank0_0_, officers0_.speciesEnum as speciesE6_0_0_, officers0_.starship_id as starship7_0_0_ from Officer officers0_ where officers0_.starship_id? 第一个查询是在EntityManager实例查找星舰上调用find方法导致的。 因为默认情况下当我们调用getOfficers方法并且第一次访问SortedSet时 一对多关系是惰性的所以执行第二个查询来检索所有人员。 看到查询中不存在order by子句但仔细查看输出会按字母顺序检索人员。 Officer name Beverly Crusher with rank COMMANDER Officer name Data with rank LIEUTENANT_COMMANDER Officer name Deanna Troi with rank COMMANDER Officer name Geordi La Forge with rank LIEUTENANT Officer name Jean-Luc Picard with rank CAPTAIN Officer name William Riker with rank COMMANDER Officer name Worf with rank LIEUTENANT 那么谁是整理人员实体 说明在Sort注释上。 在休眠状态下一个排序的集合在Java内存中排序它负责使用compareTo方法对数据进行排序。 显然此方法不是对元素集合进行排序的最佳性能方法。 在使用SQL子句和使用注释而不是编写查询之间我们可能需要一种混合解决方案。 这使我们使用排序方法来解释第三种可能性。 OrderBy注释可以用作休眠注释和JPA注释让我们指定如何通过在生成的SQL中添加“ order by ”子句来对集合进行排序 。 请记住使用javax.persistence.OrderBy允许我们通过对象属性指定集合的​​顺序同时org.hibernate.annotations.OrderBy对集合进行排序将SQL的片段不是HQL 直接附加到order by子句中。 现在不应该触动Officer类我们不需要实现compareTo方法或java.util.Comparator 。 我们只需要使用OrderBy注释来注释人员字段。 由于在这种情况下我们通过简单的属性进行排序因此使用JPA注释来保持与其他“支持JPA的 ” ORM引擎的完全兼容性。 默认情况下假定升序。 Entity public class Starship {//codeOneToMany(mappedBystarship, cascade{CascadeType.ALL})OrderBy(name)private ListOfficer officers new ArrayListOfficer();public ListOfficer getOfficers() {return Collections.unmodifiableList(officers);}protected void setOfficers(ListOfficer officers) {this.officers officers;}public void addOfficer(Officer officer) {officer.setStarship(this);this.officers.add(officer);} } 如果我们重新运行所有人员的方法则会发送下一个查询 Hibernate: select starship0_.id as id1_0_, starship0_.affiliationEnum as affiliat2_1_0_, starship0_.launched as launched1_0_, starship0_.height as height1_0_, starship0_.length as length1_0_, starship0_.power as power1_0_, starship0_.width as width1_0_, starship0_.registry as registry1_0_, starship0_.starshipClassEnum as starship9_1_0_ from Starship starship0_ where starship0_.id?Hibernate: select officers0_.starship_id as starship7_1_1_, officers0_.id as id1_, officers0_.id as id0_0_, officers0_.affiliationEnum as affiliat2_0_0_, officers0_.homePlanet as homePlanet0_0_, officers0_.name as name0_0_, officers0_.rank as rank0_0_, officers0_.speciesEnum as speciesE6_0_0_, officers0_.starship_id as starship7_0_0_ from Officer officers0_ where officers0_.starship_id? order by officers0_.name asc 这两个查询仍然执行但请注意现在select查询也包含order by子句。 使用此解决方案您可以节省处理时间从而允许RDBMS快速对数据进行排序而不是一旦接收到Java中的数据就对其进行排序。 此外 OrderBy批注不会强制您使用SortedSet或SortedMap集合。 您可以使用HashMap HashSet甚至Bag之类的任何集合因为hibernate将在内部分别使用LinkedHashMap LinkedHashSet或ArrayList 。 在这个例子中我们已经看到了正确选择订购策略的重要性。 只要有可能您都应该尝试利用RDBMS的功能因此您的第一个选择应该是使用OrderBy注释 休眠或JPA 而不是Sort 。 但是有时OrderBy子句是不够的。 在这种情况下我建议您使用具有自定义类型的Sort注释使用java.util.Comparator类而不是按自然顺序进行中继以避免触摸模型类。 Sort(typeSortType.COMPARATOR, comparatorTimeComparator.class) 我希望这篇文章可以帮助您了解休眠状态下 “排序”和“顺序”之间的区别。 保持学习。 参考 Hibernate提示我们的JCG合作伙伴 Alex Soto的“ 排序和排序”在One Jar To Rule All All博客中。 翻译自: https://www.javacodegeeks.com/2012/04/hibernate-tip-sort-and-order.html
http://www.yutouwan.com/news/26788/

相关文章:

  • 青岛找网站建设公司新产品推广方案怎么写
  • 网站app开发平台建设网站服务
  • 网站的市场如何制作安徽企业平台网站建设
  • 网页怎么发布网站描述优化
  • 网站用户维度wordpress d压缩
  • 定远建设小学投诉网站重庆网站建设推广服务
  • 网站的弹窗是怎么做的东道设计作品图片
  • 网站建设内容国家建设网资质查询
  • 网站蜘蛛怎么看wordpress粘帖图片
  • 阿里云企业网站怎么收费百度云网盘免费资源
  • 怎么做夜场网站网站建设模板漏洞
  • 有哪些做外贸网站企业网站ui设计欣赏
  • 滨海新区网站建设国内软件上市公司排行榜
  • 常州云之家网站建设网络公司怎么样建设银行网站收款怎么打明细
  • 网站导航栏下拉菜单app运营推广策划方案
  • 网站建设先修课程sedo这种多语言网站怎么建设
  • 怎样做网站流量升级系统
  • 自己做响应式网站难吗德兴市建设局网站
  • 科技类网站简介怎么做网站空间是先备案后买
  • 河南建设银行招聘网站网站建设后期
  • 企业被网站骗做会员移动端网站和app区别
  • 高要网站建设公司品牌宣传方案
  • phpstudy2016快速搭建网站手机开发人员选项在哪
  • 网站301做下国家企业信息官网查询
  • 金融行业网站建设方案公司想做个自己的网站怎么做的
  • 雄安网站建设费用中国建设工程电子信息网
  • 黑龙江省网站前置审批网站成都建站费用
  • 免费开源网站微信网站页面设计
  • 织梦网站首页内容wordpress链接样式表
  • 网站套餐方案百度账户登录