Suppose I have the following Domain class:
class Book {
String title
String author
byte[] largeCoverArtImage
}
I have a list view where I do not need to display largeCoverArtImage
, how can I perform the following SQL query using GORM Criteria?
select title, author from Book
It uses the name of your type. Here the type is User so gorm will use the users table by default. You can also configure it to not use the plural form if you'd like.
One major concern when using gorm is that it runs very slowly when the traffic is high. There are some benchmarks on the internet which shows that gorm can runs 3-5 times slower than the standard library.
Golang ORMs Luckily, the Go community has built a number of Object Relational Mapping libraries (ORMs) to allow Go developers to use JSON key:value pair syntax and encoding to map directly to a SQL database like PostgreSQL. ORMs enable developers to use their native programming paradigm to map data to SQL.
You can run HQL queries that select individual columns with executeQuery:
def titlesAndAuthors = Book.executeQuery('select title, author from Book')
This will return a List of Object[], e.g.
for (row in titlesAndAuthors) {
String title = row[0]
String author = row[1]
...
}
In Grails (tested with 1.3.7 version) you can write:
def titlesAndAuthors = Book.withCriteria {
projections {
property 'title', 'title'
property 'author', 'author'
}
}
And you'll get a list of Object[] like above example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With