GORM 查询过滤器不是类的直接属性

发布于 2024-09-28 17:40:13 字数 711 浏览 8 评论 0原文

我试图找出使用 Grails GORM 执行某种类型查询的最有效的方法。

在这种情况下,我想查询多对一关系中的所有子项/链接项。这是一种单向关系,多方使用它所链接的事物的 ID。

一个例子是学年与该学年学期之间的关系。学期表中,表中存储的是该学期所属学年的id。然而,该学年的表格中没有任何内容可以链接到属于该学年的学期。这样做是为了保持学期数量的灵活性。我希望做的是使用 HQL 之类的东西来检索拥有相同学年的所有学期。在常规 SQL 中,我们可以使用学期表的学年 id 列来过滤行。在 HQL 中,事情就没那么简单了。学年 ID 不可用作域类的属性。相反,Hibernate 只是使用它来加载实际的学年对象。

在这种情况下应该像下面这样吗?

select from Semester as s where s.year.id = ?

在这种情况下,保存学年的学期域类的属性称为年份。该查询获取年份属性和该年份的 ID,并使用它来过滤学期。

这只是一个示例,但我正在开发的系统包含多个类似的安排,其中需要加载一组都具有相同链接域对象的域对象。

可能有一种更高效/有效的方法来做到这一点。一个示例是从域类中获取实际的学年 ID 值。然而,这意味着同一列被映射到域类的多个属性。这可能不是必需的,但这是解决此类问题的另一种可能的方法。

有一些 Hibernate 经验,但是当您想做更多不寻常的事情时,Hibernate 中的一些问题会变得复杂。

I am trying to figure out the most efficient / effective way of performing a certain type of query using Grails GORM.

This is the scenario where I want to query all of the children / linked items in a many to one relationship. This is a one way relationship whereby the many side used the id of the thing it is linked to.

One example is a relationship between an Academic Year and the Semesters of that Academic Year. In the semester table, the id of the academic year to which the semester belongs to is stored in the table. The academic year however does not have anything it its table to link to the semesters which belong to it. This is done to keep the number of semesters flexible. What I hoped to do is use something like HQL to retrieve all the semesters which have the same academic year owning them. In regular SQL we can filter the rows using the academic year id column of the semester table. In HQL, it is not as straightforward. The academic year id is not available as a property of the domain class. Instead Hibernate just uses it to load the actual academic year object.

Should something like below work in this case?

select from Semester as s where s.year.id = ?

In this case the property of the semester domain class which holds the academic year is called year. The query is taking the year property and the id of that year and using it to filter the semesters.

This is just one example but the system I am developing contains more than one similar arrangement where it would be desirable to load a group of domain objects which all have the same linked domain object.

There may be a more efficient / effective way of doing this. One example would be to make the actual Academic Year id value available from the domain class. This would however mean the same column is mapped to more than one property of the domain class. This may well not be necessary but it is another possible way of solving this kind of problem.

Have some Hibernate experience but some problems get complicated in Hibernate when you want to do more unusual things.

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

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

发布评论

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

评论(2

黎夕旧梦 2024-10-05 17:40:13

这看起来像基本的 1:M 关系,由belongsTo/hasMany 映射处理。
它不会使父表保留任何附加数据。只需拥有域对象:

class AcademicYear {
    static hasMany = [semesters: Semester]
}

class Semester {
    static belongsTo = AcademicYear
}

Semester 神奇地具有您可以引用的“academicYear”属性。一切,就像这样做:

AcademicYear y = AcademicYear.findByYear(2010)
Semester s = Semester.get(1)
y.addToSemesters(s)
y.semesters.each{ println it }
String year = s.academicYear.name
def a = s.academicYear.id

首先在“grails console”中尝试并享受。

This looks like basic 1:M relationship, which is handled by belongsTo/hasMany mappings.
It doesn't make parent table keep any additional data. Just have domain objects:

class AcademicYear {
    static hasMany = [semesters: Semester]
}

class Semester {
    static belongsTo = AcademicYear
}

And Semester magically has "academicYear" propery which you can reference. And everything, just do it like:

AcademicYear y = AcademicYear.findByYear(2010)
Semester s = Semester.get(1)
y.addToSemesters(s)
y.semesters.each{ println it }
String year = s.academicYear.name
def a = s.academicYear.id

Try it in "grails console" first and enjoy.

宛菡 2024-10-05 17:40:13

虽然 gorm 是 hibernate 的另一层,但它为常见场景提供了高效的 criteriaQuery 和动态查找器,当涉及到不寻常的场景时,我们必须查看条件查询。当涉及到更不寻常的场景时,我们必须查看hql,对于更复杂的情况,我们也可以编写基本的sql。

以下是可用于实现一种或其他类型的查询级任务的上层不同方法。

假设:我们有一个 Employee 域,如下所示:

class Employee{
String name
int age
Designation designation

static hasMany = [teams:Team]
}
  1. GORM 动态查找器:

     Employee.findByName("Ajay")
    
  2. 条件查询:

    Employee.createCriteria().list {
            eq '名字', '阿杰'   
    }
    
  3. where 子句:

    Employee.where {
        名称 == 'ajay' && (年龄>25&&年龄<30)
    }。列表()
    
  4. HQL:

    Employee.findAll('from Employee as e where e.name = :name', [name: 'Ajay'])
    
  5. 基本sql:

    字符串查询 = $/
    从员工 e 中选择 *
    WHERE e.name = :name
    /$  
    新员工()
    .domainClass
    .grails应用程序
    .mainContext
    .sessionFactory
    .currentSession
    .createSQLQuery(查询)
        .setString('名字', '阿杰')
        。列表()
    

下面是更细粒度的层次结构,其中 yes 表示适合或适用:

                    dynamic finder          where clause    criteria    HQL     SQL

    simple queries         yes                Yes             Yes        Yes    Yes

    complex filters                           Yes             Yes        Yes    Yes

    associations                              Yes             Yes        Yes    Yes

    property comparisons                      Yes             Yes        Yes    Yes

    some subqueries                           Yes             Yes        Yes    Yes

    eager fetches w/ complex filters                          Yes        Yes    Yes 

    projections                                               Yes        Yes    Yes 
    queries with arbitrary return sets                                   Yes    Yes

    highly complex queries (like self joins)                                    Yes

    some database specific features                                             Yes

    performance-optimized queries                                               Yes

更多内容可以从 http://tatiyants.com/how-and-when-to-use-various-gorm-querying-options/

Though gorm is another layer over hibernate, it provides efficient criteriaQuery and dynamic finders for usual scenarios and when it comes to unusual scenarios, we must look at criteria queries. And when it comes to even more unusual scenarios, we must look at hql and for even more complex cases we may write basic sql as well.

Following are upper level different ways which could be used to achieve one or other type of query level tasks.

Let's assume: we have a Employee domain as below :

class Employee{
String name
int age
Designation designation

static hasMany = [teams:Team]
}
  1. GORM dynamic finders :

     Employee.findByName("Ajay")
    
  2. Criteria Query:

    Employee.createCriteria().list {
            eq 'name', 'Ajay'   
    }
    
  3. where clause:

    Employee.where {
        name == 'ajay' && (age > 25 && age < 30)
    }.list()
    
  4. HQL:

    Employee.findAll('from Employee as e where e.name = :name', [name: 'Ajay'])
    
  5. Base sql :

    String query = $/
    SELECT * from employee e
    WHERE e.name = :name
    /$  
    new Employee()
    .domainClass
    .grailsApplication
    .mainContext
    .sessionFactory
    .currentSession
    .createSQLQuery(query)
        .setString('name', 'Ajay')
        .list()
    

Below is more granular hierarchy for same where yes is for suitable or applicable:

                    dynamic finder          where clause    criteria    HQL     SQL

    simple queries         yes                Yes             Yes        Yes    Yes

    complex filters                           Yes             Yes        Yes    Yes

    associations                              Yes             Yes        Yes    Yes

    property comparisons                      Yes             Yes        Yes    Yes

    some subqueries                           Yes             Yes        Yes    Yes

    eager fetches w/ complex filters                          Yes        Yes    Yes 

    projections                                               Yes        Yes    Yes 
    queries with arbitrary return sets                                   Yes    Yes

    highly complex queries (like self joins)                                    Yes

    some database specific features                                             Yes

    performance-optimized queries                                               Yes

More one can read from http://tatiyants.com/how-and-when-to-use-various-gorm-querying-options/

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