简体   繁体   中英

Does it matter what Domain Object you execute a query on in grails

Does it matter what domain object you use when you execute a query? For instance, I have these two domain objects

Class A {
    String name
}

Class B {
    String name
}

If I want to get all A objects, I can do the following

A.executeQuery('FROM A')

But I can also call the same query from a different domain object and get the exact same results as so

B.executeQuery('FROM A')

Is there a difference between these two statements performance wise? Maybe something under the hood that is happening differently?

For a little more context, I am writing a service where the application will be executing queries off of domain objects dynamically. So I could either pick a base domain object and just execute off that every time, or I can maybe make an instance of the domain object with a string that is provided into the method.

Thanks

No, it does not matter. In this case it's just executing HQL (hibernate query) and either domain class acts exactly the same in this respect for executeQuery .

In your specific case I'd just use a single domain class to execute all the queries from. No need to change the type.

Does it matter what domain object you use when you execute a query?

It depends on what query technique you are using. For executeQuery in particular it does not. For most other query techniques it does. For example, A.executeQuery('FROM A') is the same as B.executeQuery('FROM A') . A.list() is not the same as B.list() . A.findAllByTitle('Tribe') is not the same as B.findAllByTitle('Tribe') , A.where { ... } is not the same as B.where { ...} , etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM