何时使用 Hibernate 投影?
我对 Hibernate 的预测和标准有点困惑。何时使用预测以及何时使用标准?
I am a little confused about Hibernate's projections and criteria. When to use projections and when to use criteria?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它们并不相互排斥,您可以同时使用两者。预测通常在某些标准的背景下使用。
简而言之,Hibernate Projections 用于仅查询您使用 Criteria 查询的实体或实体组的属性子集。您还可以使用投影来指定
distinct
子句和聚合函数,例如max
、sum
等。这就像引用您正在获取哪些数据。就像修改 SQL 查询中的select
子句一样。Hibernate Criteria 用于定义数据必须满足的条件才能被选择。这就像指您正在获取的数据如何。就像修改 SQL 查询的
from
和where
子句一样。请注意,这个如何和哪个并不严格正确,它只是一个旨在帮助OP的方向。例如,您可以使用
createCriteria(String AssociationPath)
更改要提取的数据。我建议看看这篇文章 Hibernate:深度条件查询
They're not mutually exclusive, you can use both at the same time. Projections are generally used in the context of some Criteria.
To put it simple, Hibernate Projections are used in order to query only a subset of the attributes of an entity or group of entities you're querying with Criteria. You can also use Projections to specify
distinct
clauses and aggregate functions likemax
,sum
and so on. It's like referring to which data you're fetching. Like modifying theselect
clause in an SQL query.Hibernate Criteria are used to define conditions the data has to satisfy in order to be selected. It's like referring to how is the data you're fetching. Like modifiying the
from
andwhere
clauses of an SQL query.Note that this how and which is not strictly true, it's just an orientation aimed to aid the OP. You can change which data you're fetching with
createCriteria(String associationPath)
for instance.I'd suggest to take a look at this article Hibernate: Criteria Queries in Depth
投影用于执行聚合操作并获取单列查询,使用限制我们可以访问ROW,但使用投影我们可以访问整个COLUMN
EX -
在上面的示例中我使用投影调用将投影属性“名称”添加到条件中。它返回 winchester winchester winchester winchester ,它是表中的姓氏COLUMN。
输出=
注意 - 我们只能添加一个投影,如果添加超过 1 个投影,前一个投影将被覆盖。如果您想添加多个投影,则需要 ProjectionList 类
Orignal Table -
Projections are used to execute aggregate operations and to get single column query,with Restrictions we can access a ROW but with PROJECTIONS we can access whole COLUMN
EX -
In the above example i used projection call to ADD a projection property "name" to the criteria. It returns winchester winchester winchester winchester , which is the lastName COLUMN in the table.
Output =
Note - We can add only one projection, if we add more than 1 projection previous one will be overridden. if you want to add more than one projection you will nedd ProjectionList class
Orignal Table -
Hibernate 投影对于
1) 仅获取表的某些列
2) 执行聚合(如计数、总和、最大值、最小值、平均值)很有用。
仅获取所需的列可以提高查询的性能。
投影示例
使用 DTO 的投影
投影列表示例
Hibernate projections are useful for
1)Fetching only certain columns of the table
2)Performing aggregations like count,sum,max,min,avg.
Fetching only the columns which are needed improves the performance of the query.
Examples of projection
Projections using DTO
Projection List example
Projection是“org.hibernate.criterion”包中给出的接口,Projections是同一包中给出的类,实际上Projection是一个接口,Projections是一个类,是一个用于生成投影对象的工厂。
在Projections类中,我们有所有静态方法,并且该类的每个方法都返回Projection接口对象。
如果我们想向 Criteria 添加一个 Projection 对象,那么我们需要调用 setProjection() 方法
请记住,在向 Criteria 添加投影对象时,可以一次添加一个对象。这意味着如果我们添加第二个投影对象,那么第二个投影对象将覆盖第一个投影对象(第一个投影对象不起作用),因此一次我们只能将一个投影对象
添加到条件对象使用条件,如果我们想从数据库,那么我们需要为要从数据库加载的属性创建一个投影对象
如果我们将多个投影添加到条件,那么最后添加的投影将被视为执行,请参阅...
Projection is an Interface given in “org.hibernate.criterion” package, Projections is an class given in same package, actually Projection is an interface, and Projections is an class and is a factory for producing projection objects.
In Projections class, we have all static methods and each method of this class returns Projection interface object.
If we want to add a Projection object to Criteria then we need to call a method setProjection()
Remember, while adding projection object to criteria, it is possible to add one object at a time. It means if we add 2nd projection object then this 2nd one will overrides the first one (first one wont be work), so at a time we can only one projection object to criteria object
Using criteria, if we want to load partial object from the database, then we need to create a projection object for property that is to be loaded from the database
If we add multiple projections to criteria then the last projection added will be considered to execute see…