何时使用 Hibernate 投影?

发布于 2024-12-05 17:10:16 字数 64 浏览 1 评论 0原文

我对 Hibernate 的预测标准有点困惑。何时使用预测以及何时使用标准?

I am a little confused about Hibernate's projections and criteria. When to use projections and when to use criteria?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

穿透光 2024-12-12 17:10:16

它们并不相互排斥,您可以同时使用两者。预测通常在某些标准的背景下使用。

简而言之,Hibernate Projections 用于仅查询您使用 Criteria 查询的实体或实体组的属性子集。您还可以使用投影来指定 distinct 子句和聚合函数,例如 maxsum 等。这就像引用您正在获取哪些数据。就像修改 SQL 查询中的 select 子句一样。

Hibernate Criteria 用于定义数据必须满足的条件才能被选择。这就像指您正在获取的数据如何。就像修改 SQL 查询的 fromwhere 子句一样。

请注意,这个如何哪个并不严格正确,它只是一个旨在帮助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 like max, sum and so on. It's like referring to which data you're fetching. Like modifying the select 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 and where 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

陈甜 2024-12-12 17:10:16

投影用于执行聚合操作并获取单列查询,使用限制我们可以访问ROW,但使用投影我们可以访问整个COLUMN

EX -

public static void main(String[] args) {
    SessionFactory factory = new Configuration().configure().addAnnotatedClass(Student.class).buildSessionFactory();
    Session session = factory.getCurrentSession();
    try {
        session.beginTransaction();
        Criteria c = session.createCriteria(Student.class);
        Projection p = Projections.property("lastName");
        List<String> students = c.setProjection(p).list();
        for(String s:students)
            System.out.println(s);
        session.getTransaction().commit();
        session.close();
    } finally {
        factory.close();
    }
}

在上面的示例中我使用投影调用将投影属性“名称”添加到条件中。它返回 winchester winchester winchester winchester ,它是表中的姓氏COLUMN

输出=

Hibernate:从学生 this_ 中选择 this_.last_name 作为 y0_
温彻斯特
温彻斯特
温彻斯特
温彻斯特

注意 - 我们只能添加一个投影,如果添加超过 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 -

public static void main(String[] args) {
    SessionFactory factory = new Configuration().configure().addAnnotatedClass(Student.class).buildSessionFactory();
    Session session = factory.getCurrentSession();
    try {
        session.beginTransaction();
        Criteria c = session.createCriteria(Student.class);
        Projection p = Projections.property("lastName");
        List<String> students = c.setProjection(p).list();
        for(String s:students)
            System.out.println(s);
        session.getTransaction().commit();
        session.close();
    } finally {
        factory.close();
    }
}

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 =

Hibernate: select this_.last_name as y0_ from student this_
winchester
winchester
winchester
winchester

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 -

enter image description here

冬天的雪花 2024-12-12 17:10:16

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

后知后觉 2024-12-12 17:10:16

Projection是“org.hibernate.criterion”包中给出的接口,Projections是同一包中给出的类,实际上Projection是一个接口,Projections是一个类,是一个用于生成投影对象的工厂。

在Projections类中,我们有所有静态方法,并且该类的每个方法都返回Projection接口对象。

如果我们想向 Criteria 添加一个 Projection 对象,那么我们需要调用 setProjection() 方法

请记住,在向 Criteria 添加投影对象时,可以一次添加一个对象。这意味着如果我们添加第二个投影对象,那么第二个投影对象将覆盖第一个投影对象(第一个投影对象不起作用),因此一次我们只能将一个投影对象

添加到条件对象使用条件,如果我们想从数据库,那么我们需要为要从数据库加载的属性创建一个投影对象

Criteria crit = session.createCriteria(Products.class);
crit.setProjection(Projections.proparty("proName"));
List l=crit.list();
Iterator it=l.iterator();
while(it.hasNext())
{
    String s = (String)it.next();
    // ---- print -----
}

如果我们将多个投影添加到条件,那么最后添加的投影将被视为执行,请参阅...

Criteria crit = session.createCriteria(Products.class);

Projection p1 = Projection.property("proName");
Projection p2 = Projection.property("price");

crit.setProjection(p1):
crit.setProjection(p2):
List l=crit.list();

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

Criteria crit = session.createCriteria(Products.class);
crit.setProjection(Projections.proparty("proName"));
List l=crit.list();
Iterator it=l.iterator();
while(it.hasNext())
{
    String s = (String)it.next();
    // ---- print -----
}

If we add multiple projections to criteria then the last projection added will be considered to execute see…

Criteria crit = session.createCriteria(Products.class);

Projection p1 = Projection.property("proName");
Projection p2 = Projection.property("price");

crit.setProjection(p1):
crit.setProjection(p2):
List l=crit.list();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文