Japanese GORM - Creating a domain cl...

Last updated by admin 4 years ago

?????????? {excerpt:hidden=true} Creating a Domain Class {excerpt}

?? {excerpt:hidden=true} Basics {excerpt}

{excerpt:hidden=true} A domain class in Grails is essentially a normal Groovy class. It looks like any other class except at runtime it gets injected with 2 additional properties: an "id" property and a "version" property: {excerpt} Grails?????????????????Groovy?????????? "id" ?????? "version" ??????2?????????????????????????????????:

class Book {
String title
}
{excerpt:hidden=true} To help you get started you can run the following convenience target from the root of your Grails project: {excerpt} Grails??????????????????????????????Grails?????????????????:
grails create-domain-class

???????????????? {excerpt:hidden=true} Optional and Transient properties {excerpt}

{excerpt:hidden=true} By default all properties are both persistent and required, to change that you can define List properties called "optionals" and "transients": {excerpt}

????????????????????????????????????"optionals"?"transients"?List?????????????"??" ?"???????????"????????

class Book {
static optionals = [ "releaseDate" ]
static transients = [ "digitalCopy" ]

Author author String title String author Date releaseDate File digitalCopy }

??? {excerpt:hidden=true} Default values {excerpt}

{excerpt:hidden=true} You can also create default values for properties that you don't want to make optional. This is done like so: {excerpt} ???????????????????????????????????????????:

class Book {
//omitted code...

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

{excerpt:hidden=true} This is also helpful when you want to pre-populate form fields when creating new entries. {excerpt} ????????????????????????????????

????????? {excerpt:hidden=true} Changing the Table {excerpt}

{excerpt:hidden=true} You can change the name of the table used when doing the mapping using the {{withTable}} property: {excerpt} {{withTable}} ??????????????????????????????????:

class Book {

static withTable = "book_table"

String title = '' }

SQL?????? {excerpt:hidden=true} SQL Reserved Words {excerpt}

{excerpt:hidden=true} Be careful with SQL reserved words. The code bellow will have problem because of the "order" attribute. "order" is a SQL reserved word:{excerpt} SQL????????????????????? "order" ?SQL??????? "order" ???????????????:
class Book {
Integer order
}