Understanding GORM
Understanding GORM
Engineer at Kaleidos
GORM? Really?
Is so easy, the easiest part of Grails!
Only a few POGO's to access the database
Peachy!
Some pitfalls
The 'When did I modified that object?'
def renderBook(String isbn) {
def book = Book.findByIsbn(isbn)
// Render as uppercase
book.title = book.title.toUpperCase()
[book: book]
}
Some pitfalls
The 'Rollback where are you?'
def buyBook(String user, String bookTitle, Long qty) {
def found = Book.findByTitle(bookTitle)
found.stock = found.stock - 1
user.followers.isEmpty()
Inspect /grails-app
String name
String isbn
Author author
}
2) GORM enhances classes
class Book {
static mapWith = "mongo"
String name
String isbn
}
DEBUG cfg.HibernateUtils - Enhancing GORM entity Book
GORM Enhancer
Instances API (save, delete...)
Dynamic Finders
Validation (unique)
GORM Enhancer
Instances API (save, delete...)
Dynamic Finders
Validation (unique)
Bootstraping
Distinct Grails-base vs GORM
Grails dependencies
Understanding Spring
HOW INTERACT DOMAIN CLASSES AND SPRING
class Book {
def bookService
String name
String isbn
Author author
String toString() {
return bookService.parseBook(this)
}
}
[DEBUG] BookService::parseBook
DI needs control on object instantiation
new Book()
BookPersistentClass
BookDomainClass
Book
new Book()
Book.constructor = {->
def ctx = .... // Spring context
context.getBean("Book")
}
What does that means?
Spring creates your objects
Better collections
But, Hibernate it's still there
class Book {
String name
String isbn
Author author
}
In-Memory cache
Provider specific
A controller completes
// Render as uppercase
book.title = book.title.toUpperCase()
[book: book]
}
Some pitfalls
The 'When did I modified that object?'
def renderBook(String isbn) {
def book = Book.findByIsbn(isbn)
// Render as uppercase
book.title = book.title.toUpperCase()
[book: book]
}
Where do I put my transactional logic?
withTransaction
Book.withTransaction {
// Transactional stuff
}
Transactional services
@Transactional
class BookService {
def doTransactionalStuff(){
...
}
}
Be careful!
Don't instanciate the services
new TransactionalService().doTransactionalStuff()
And...
DON'T THROW CHECKED EXCEPTIONS
Some pitfalls
The 'Rollback where are you?'
def buyBook(String user, String bookTitle, Long qty) {
def found = Book.findByTitle(bookTitle)
found.stock = found.stock - 1
found.stock = found.stock - 1
dispatchingService.dispatch(order)
order.sent = true
order.save()
}
}
}
}
Rule of thumb
Session = Thread
@alotor
http://goo.gl/U6sK5E
Bonus track (references)
http://spring.io/blog/2010/06/23/gorm-gotchas-part-1/
http://sacharya.com/tag/gorm-transaction/
http://www.anyware.co.uk/2005/2012/11/12/the-false-
optimism-of-gorm-and-hibernate/
http://docs.jboss.org/hibernate/orm/3.6/reference/en-
US/html_single/#transactions-basics
Bonus track (references)
http://www.infoq.com/presentations/GORM-Performance
http://www.infoq.com/presentations/grails-transaction
http://blog.octo.com/en/transactions-in-grails/
https://community.jboss.org/wiki/OpenSessioninView