0

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

JoeyH
  • 3
  • 2
  • "or I can maybe make an instance of the domain object with a string that is provided into the method" - You should not create an instance of a domain object in order to execute a query. – Jeff Scott Brown Jan 31 '18 at 19:24

2 Answers2

0

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.

Joshua Moore
  • 24,706
  • 6
  • 50
  • 73
  • "In your specific case I'd just use a single domain class to execute all the queries from." - That would be a non-standard and potentially confusing thing to do. Having just 1 domain class that you execute all of your queries on is not a recommended best practice. – Jeff Scott Brown Jan 31 '18 at 19:23
0

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...

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47