首页 \ 问答 \ Hibernate Criteria n + 1问题与maxresults(Hibernate Criteria n+1 issue with maxresults)

Hibernate Criteria n + 1问题与maxresults(Hibernate Criteria n+1 issue with maxresults)

使用休眠ctiteria我想选择一个对象,并且它与一对多对象的列表相关联。 我想通过这个列表来分页,避免可怕的休眠n + 1选择问题

这是一个工作解决方案,需要11次访问10个父对象的数据库。

Criteria criteria = this.getSession().createCriteria(Mother.class);
criteria.addOrder(Order.asc("title"))
.setMaxResults(details.getMaxRows())
.setFirstResult(details.getStartResult())
.setFetchMode("kittens", FetchMode.SELECT);
List test = criteria.list();

这是一个解决方案,它只执行一个sql语句(hurray),但无法处理分页,即setMaxResults和setFirstResult在父对象上不正确(母语)

Criteria criteria = this.getSession().createCriteria(Mother.class);
criteria.addOrder(Order.asc("title"))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.setMaxResults(details.getMaxRows())
.setFirstResult(details.getStartResult())
.setFetchMode("kittens", FetchMode.JOIN);
List test = criteria.list();

这似乎是这样一个常见的要求,但我已经找到了一个没有运气的解决方案。

任何接受者?


Using hibernate ctiteria I want to select an object and it's associated oneToMany list of objects. I want to paginate through this list avoiding the dreaded hibernate n+1 select issue

Here's a working solution which requires 11 trips to the database for 10 parent objects.

Criteria criteria = this.getSession().createCriteria(Mother.class);
criteria.addOrder(Order.asc("title"))
.setMaxResults(details.getMaxRows())
.setFirstResult(details.getStartResult())
.setFetchMode("kittens", FetchMode.SELECT);
List test = criteria.list();

And here's a solution which executes only one sql statement (hurray) but cannot handle pagination ie the setMaxResults and setFirstResult are incorrect on the parent object Mother (boo)

Criteria criteria = this.getSession().createCriteria(Mother.class);
criteria.addOrder(Order.asc("title"))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.setMaxResults(details.getMaxRows())
.setFirstResult(details.getStartResult())
.setFetchMode("kittens", FetchMode.JOIN);
List test = criteria.list();

This seems like such a common requirement but I've dug around for a solution with no luck.

Any takers?


原文:https://stackoverflow.com/questions/5567754
更新时间:2022-06-11 06:06

最满意答案

我不知道任何jQuery插件,但是建议您可以创建一些可以将字符串或哈希转换为图像(甚至可能在canvas元素中)的效果并不合理,尽管它可能是最好远离客户端计算机并在服务器上执行此类操作。

一段时间后,Stack Overflow推出了一个自定义的gravatar-ish替代“unicornify”每个人的头像。 您可以在http://unicornify.appspot.com/use-it上了解有关该特定设置的更多信息,以及此处的历史记录: https//meta.stackexchange.com/questions/37328/my-godits-full- - 独角兽 这是@balpha的工作。

Gravatar将他们的算法保持在锁定和密钥之下,这是可以理解的。 Avatarco是一个开源的PHP替代品,旨在提供类似于Gravatar的功能。 我鼓励你仔细阅读他们的来源,以便更好地理解你如何能够完成类似的事情。


I don't know of any jQuery plugins, however it's not beyond reasonable to suggest that you could create something to that effect which would convert either a string, or a hash into an image (perhaps even within a canvas element), though it's probably best to keep that away from the client machine and perform such actions on the server.

Some time back Stack Overflow launched a custom gravatar-ish alternative to "unicornify" everybody's avatars. You can learn more about that particular setup at http://unicornify.appspot.com/use-it, and the history of it here: https://meta.stackexchange.com/questions/37328/my-godits-full-of-unicorns. This was the work of @balpha.

Gravatar keeps their algorithm under lock and key, understandably. Avatarco is an open-source PHP alternative that seeks to provide functionality similar to that of Gravatar. I would encourage you to peruse their source to for a better understanding of how you might accomplish something similar.

相关问答

更多
  • 我不知道任何jQuery插件,但是建议您可以创建一些可以将字符串或哈希转换为图像(甚至可能在canvas元素中)的效果并不合理,尽管它可能是最好远离客户端计算机并在服务器上执行此类操作。 一段时间后,Stack Overflow推出了一个自定义的gravatar-ish替代“unicornify”每个人的头像。 您可以在http://unicornify.appspot.com/use-it上了解有关该特定设置的更多信息,以及此处的历史记录: https : //meta.stackexchange.com ...
  • 尝试检查jQuery UI - 更相关的Draggable功能。 Try checking out jQuery UI - more relevantly the Draggable feature.
  • 它来自Office 2007,当然。 它们可以在Word,Excel和Powerpoint 2007中创建。 Office有一个教程来创建他们称之为“SmartArt图形”的东西 SmartArt Graphic创建者http://www.java2s.com/Tutorial/Microsoft-Office-Word-2007Images/Smartart_Graphic_Purposes___List_Show_Nonsequential_Information.PNG 这是一张自制的图片,来自Wor ...
  • 创建动画本身非常简单。 你不需要一个库。 有两种选择适合大多数情况,还有其他方法可以动画,但这些是最重要的: 查看动画 对象动画师 这两者在如何使用方面没有太大差别,但他们可以做不同的事情。 1)查看动画: 对于视图动画,首先必须编写动画xml。 在其中,您将描述动画应该是什么样子以及它持续多长时间。 您当然也可以以编程方式创建这些动画,但在大多数情况下,最好使用xml创建动画。 例如,这里有两个动画xmls,一个从顶部向下滑动视图,另一个向下淡出视图。 滑下:
    目标是限制您迫使客户必须立即进行的更改。 他们最终可能不得不做出一些改变,但希望它只是在不可避免的情况下,例如它们背后有多个版本而你正在逐步淘汰它。 不间断的变化可以是: 添加可选属性 添加新操作 突破性的变化包括: 添加所需的属性 删除属性 更改属性的数据类型 更改属性的名称 删除操作 重命名操作 如果明确指定,则更改属性的顺序 更改绑定 更改服务命名空间 改变操作的含义。 我的意思是,例如,如果操作总是返回所有记录,但它被更改为仅返回某些记录。 这将打破客户预期的反应。 选项: 添加新操作以处理更新的属 ...
  • 有关详细信息,请参阅mathworks文档 。 对于你的情况,你去: data = rand(6,2); figure % You need to somehow fix your axes, so that they don't resize axis tight manual ax = gca; ax.NextPlot = 'replaceChildren'; % Let's record this to file v = VideoWriter('myAnimation.avi'); v.Fra ...
  • 您还可以查看JQuery UI,因为这样您也可以为颜色设置动画或阅读有关插件的文章 ,以获得难以忘怀的用户体验。 You could also check out JQuery UI as this allows you to animate colours too or read this article about plugins for unforgetable user experiences.
  • 是的,有。 以下是一些帮助您入门的内容: 自由: http://drupal.org/documentation/customization/tutorials Drupal Dojo: http : //drupaldojo.com Lullabot的 learnbythedrop mogdesign - ultimate-drupal-tutorial-collection Yes, there are. Here are a few to help you get started: Free: htt ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)