Korean GORM

Last updated by admin 4 years ago

Grails ?? ?? ??(Grails Object Relational Mapping - GORM)

??

?? ???? ??????? ??? ??? ??? ??? ???. ??? ???? ???? ????? ?? ??? ????, ???? ??? ?????. ??? ????? ??? ?? ??? ??? ?? ???? ????.

GORM? Grails? ?? ?? ??(ORM) ??????. GORM? ?? ???? ??? ???? ORM ???? Hibernate 3? ???? ???????. ??? GORM? Hibernate? ??, Groovy? ??? ???? ?? ?? ??? ?? ??? ?? ???? Grails? ??(convention) ?? ??? ???? ?? ? ??? ??(configuration)? ? ????.

??? ???? Grails ??? ???? ?? ?? ????. ???? Grails ??? ???? ???, Grails? ??? ?? ????? ???? ??? ????? Hibernate? ???? ??? ?????.

??? ??? ???

Grails? ??? ???? ? ?? ??? ??? ?? ??? Groovy ??????. ? ?? ???? "id" ??? "version" ??? ?????:

class Book {
    Long id
    Long version

String title }

?? ??? ???? ?????? Grails ????? ?? ?????? ?? ??? ?????:
grails create-domain-class

?? ???

??? ??? ????? ?? Set ??? ??? ???? "relatesToMany" Map ?? ??? ??? ?????? ?? ???:

class Author {
    Long id
    Long version

def relatesToMany = [ books : Book ]

String name Set books = new HashSet() }

Groovy? ?? ?? ? ??? ??? ?? ???? ????? ????. ?? ?? Grails??? ??? ?? ??? ??? ??? ? "relationships" Map? ??? ??? ??? ????:
class Book {
    Long id
    Long version

Author author String title }

??? ????? "?"? "?"? ???? ??? ?????(??? ? ????? "Author.groovy"? ??????). ??? ??? ??? ??? ??? ???? ???? ?? ?? ?????. ?? ?? ? ???? ??? Book? Author ??? ??? ??? ???? ????(?, ??? ??), Book? Author? ???? ??? ??? ?? ????. ?? ?? ??? ????? ?? ??? ??? ?????!

??? ???? ?? ??, ???? ?? Book? ????? ???? ?? ??? Author ???? ?? ????? ????. ?? ??? ??? ??? ?? ????. ??? ???? ???? ?(belongings)? ???? ???? "belongsTo" ??? ?????:

class Book {
    Long id
    Long version

belongsTo = Author

Author author String title }

??? ???? ??, ??? ???? ?? ??? ???? ??? ?? ????:
def belongsTo = [Author,Publisher]
?,
  • ???(owner)? ???(belongings)? ?? ? ??
  • ???? ??? ???? ???? ?? 'belongsTo' ??? ????
  • ???? ??? ???? ?? ??? ?? ?????.
??? ??? ?? ???? ????. ??? ??? ??? ????? ??? ? ?? ??? ???? ???? ?? ???? ?????? ???.

?? ??

?? ?? ? ??? A? B ??? ??? ???? ???? ???? ????.

??? ???(->)??? ???(<->)? ? ??, ??? (1:1), ??? (1:m), ??? (m:1), ??? (n:m) ? ??? ???? ????. ???? ?? ??? ??????.

||A:B || ??? || ??? || |1:1 | A -> B | A <-> B ; B.belongsTo = [A] | |1:m | A -> Set ; A.relatesToMany= [B] | A -> Set ; A.relatesToMany = [B]; B -> A (*) | |m:1 | A -> B | A -> B ; B -> Set ; B.relatesToMany = [A] (*g) | |n:m | n/a | n/a |

(*) ??? ????? "?" ?? ?? ?????? {{belongsTo}} ??? ?? ????. (g) ()? ??

?? ??? ????

GORM? ?? ?? ?? ? ??? ???? ????? ??? ?????. ??? ?? ???? ? ???? ?? ?? ????? ??? ???? ???? ???:

class Content {
   Long id
   Long version
   String author
}
class BlogEntry extends Content {
   URL url
}
class Book extends Content {
   String ISBN
}
class PodCast extends Content {
   byte[] audioStream
}

? ?? ??? ?? ??? ??(polymorphic query)? ??? ? ????:

def content = Content.findAll() // ??? ???, ?, ?? ???? ?? ??
def podCasts = PodCast.findAll() // ?? ???? ??

??? ??? ?? ??: ?? ????, ?? ?? ?? ? ??? ???? ????? ??? ? ??? ??? ????? ?????. class ?? ??? ??? ?? ?? ?? ???? ????? ?? ??? ???? ?? ????. ??? ?? ????? ??? ??? ??? ?? ? ??? ???? ?? ?????. ?? ???? ???? "??(required)"? ??? ? ??? ???? ?? ?? ???? ?? ???? ???? ?? ??? ???? ?? ??? nullable? ? ??? ?? ?????. ?? ???? ?? ??? ??? ???? ?? ?? ???? ??? "??(required)"? ??? ??? ? ?? ?? ????.

?? ???(optional) ??? ????(transient) ??

????? ?? ??? ???(persistent)?? ???(required)???. ? ???? ????? "optionals"? "transients"?? ??? List ??? ????? ???.

class Book {
    Long id
    Long version

optionals = [ "releaseDate" ] transients = [ "digitalCopy" ]

Author author String title String author Date releaseDate File digitalCopy }

???

?? ??? ?? ???? ??? ?? ????. ?? ??? ?????:

class Book {
//omitted code...

String title = '' String author = '[author unknown]' //etc.. }

? ??? ??? ???? ??? ???? ????? ? ? ?? ?????.

CRUD ?????

Grails ??? ???? CRUD ??(??/??/??/?? - Create/Read/Update/Delete)? ???? ?? ?? ???? ?????.

??

Grails ??? ???? "save" ???? ???? ????? ?? ?? ??? ?? ????? ?? ????? ?????? ??? ? ????. ?? ????? "Author"? "save"?? ???? ??? ????? "Author"? "Book"? ????? ?? ?????:

def a = new Author(name:"Stephen King")
def b = new Book(title:"The Shining",author:b)
a.books.add(b)

// ?? a.save()

?? ?? ?? ??? ???? 'books'? ?? ???? ??? ???? ??? ???? ???? ??? ???? ?? ????. ?? ?? ?? ?? ???? ???? 'addBook' ???? ???? ????:
def addBook(book) {
   if(!books)books = new HashSet()
   book.author = this
   books.add(book)
   return this
}
GORM? ??? ??????? ???? ??? ?? ??? ????, ?? ?? ????? ????? ?? ????. ??? ?? ?? ??? ??? ?? ?? ?????. ???? ?? ??? ??? ?? ????:
def a = new Author(name:"Stephen King")
a.addBook(new Book(title:"The Shining"))
 .addBook(new Book(title:"IT"))
 .save()

??

Grails? ??? ??? ????? ???? ? ?? ??? ?????. ???? ??? ?? ? ??? ??? #??? ??? ???? ??? ?????. ??? "id"? ?? ?? ??? "get" ?? ???? ???? ???:

Book.get(1)
?? ??? ??? ????? "findAll", "list", "listOrderByX" ?? ???? ?????:
Book.findAll() // ?? ????
   Book.list(10) // ?? 10?? ????? ????
   Book.listOrderByTitle() // "title" ??? ?? ??? ?? ???? ????

??

??? ??? ????? ???? ?? ???? ?? ????? ?? ????. ??? ????? ???? ????? "save"? ???? ??? ??? ??? ???? ?????.

def b = Book.get(1)
b.releaseDate = new Date()

?? ?? ??? ?? ??? ?? ????. ?? ??? ??(validation constraints)? ?? ??? ??? ?????(?, ???? ?? ??? ??? ???? ?? ??? ?? ????). ?? ?? ????? "save"? ????? ???? ??? ??? ???? ??? ???? ?? ?? ??? ?? ?? ???:

def b = Book.get(1)
b.title = null // ??? null? ? ??
b.save() // ???? ???? ???? ????.

?? ??? ??? ??? ?? ???? ??? ??? ??? ? ????. ??? ??? save ???? ????? ???? ??? ???? ?????. ? validate ???? ??? ??? ??? ???? ?? ??? ??? ????. ?? ??? "validate" ???? "discard" ???? ????:

def b = Book.get(1)
b.publisher = "Print It"

if(!b.validate()) { b.publisher = Publisher.DEFAULT }

validate ???? ??? ??? ??? ???? ??? ?? ??? ??? validate ???? true ??? ??? ???:

b.validate(true)

?? ????? "discard" ???? ???? ?? ?? ??? ????? ? ? ????:

def b = Book.get(1)
b.title = "A New Title"

// ?? ??? ???, ???? ??? ?? b.discard()

??

"delete" ???? ???? ???? DB??? ????? ??? ? ????:

def b = Book.get(1)
b.delete()

??? ??? ????

?? ???? ???? ????

??? ??? ????? ???? ??? ????? ????. ? ? ???? Grails ?? ???? ???? ????. ? ??? ??? ??? ??? ?? ???? ?????:

def results = Book.findByTitle("The Stand")

results = Book.findByTitleLike("Harry Pot%") results = Book.findByReleaseDateBetween( firstDate, secondDate ) results = Book.findByReleaseDateGreaterThan( someDate ) results = Book.findByTitleLikeOrReleaseDateLessThan( "%Something%", someDate )

// find by relationship results = Book.findAllByAuthor( Author.get(1) )

??? ?? ????

find() ???? ??? ?? ??? ??? ??? ??? ??? ?? ????:

def b = Book.find( new Book(title:'The Shining') )

Criteria ??? ???? ????

? ??? ?? ?? ?? ?? ?? ???? ?? ??? ???? Criteria? ?????. Criteria? ?? ?? ????? ??? ?????.

def c = Book.createCriteria()
def results = c {
     like("author.name", "Stephen%")
     between("releaseDate", firstDate, secondDate )
}

HQL? ???? ????

? ?? ??? ????. Grails? ????? Hibernate? ???? ????, HQL ??? ?? ? ?? ????:
def results = Book.find("from Book as b where b.title like 'Lord of the%'")
?? ?? ?? ??(positional parameters)? ?? ? ?? ????:
def results = Book.find("from Book as b where b.title like ?", ["The Shi%"])