5.1 Guía de inicio rápido - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
Table of Contents
5.1 Guía de inicio rápido
A domain class can be created with the create-domain-class command:
Una clase de dominio puede ser creada con el comando create-domain-class:grails create-domain-class helloworld.Person
If no package is specified with the create-domain-class script, Grails automatically uses the application name as the package name.Si el paquete no se especifica con el script create-domain-class, Grails automáticamente utilizara el nombre de la aplicación como nombre del paquete.
This will create a class at the location
Esto creara una clase como la siguiente en la ubicación grails-app/domain/helloworld/Person.groovy such as the one below:
grails-app/domain/helloworld/Person.groovy:package helloworldclass Person {
}If you have theSi la propiedaddbCreateproperty set to "update", "create" or "create-drop" on your DataSource, Grails will automatically generate/modify the database tables for you.dbCreate, se establece a "update", "create" o "create-drop" en el origen de datos, Grails generara/modificara automáticamente las tablas de la base de datos por usted.
You can customize the class by adding properties:
Usted puede personalizar la clase añadiendo propiedades:class Person {
String name
Integer age
Date lastVisit
}grails console
This loads an interactive GUI where you can run Groovy commands with access to the Spring ApplicationContext, GORM, etc.
Este comando cargara una GUI interactiva donde podrá ejecutar comandos Groovy y con acceso al ApplicationContext de Spring, GORM, etc.
5.1.1 CRUD Básico
Try performing some basic CRUD (Create/Read/Update/Delete) operations.
Pruebe realizando algunas operaciones CRUD (Create/Read/Update/Delete por sus siglas en ingles) básicas.Create
Crear
To create a domain class use Map constructor to set its properties and call save:
Para crear una clase de dominio utilice un constructor Mapa para establecer sus propiedades y llame save:def p = new Person(name: "Fred", age: 40, lastVisit: new Date()) p.save()
The save method will persist your class to the database using the underlying Hibernate ORM layer.
El método save persistirá la clase a la base de datos utilizando la capa ORM subyacente de Hibernate.Read
Leer
Grails transparently adds an implicit
De forma transparente, Grails añade a la clase de dominio la propiedad implícita id property to your domain class which you can use for retrieval:
id que puede utilizarse para su recuperación:def p = Person.get(1) assert 1 == p.id
This uses the get method that expects a database identifier to read the
En este ejemplo se utiliza el método get que espera un identificador para leer el objeto Person object back from the database.
You can also load an object in a read-only state by using the read method:
Person desde la base de datos. Para cargar un objeto en estado de sólo-lectura utilice el método read:def p = Person.read(1)
In this case the underlying Hibernate engine will not do any dirty checking and the object will not be persisted. Note that
if you explicitly call the save method then the object is placed back into a read-write state.
En este caso el motor subyacente de Hibernate no hará ningún "dirty checking" y el objeto no sera persistido. Si usted de manera explicita llama al método save entonces el estado del objeto se modificara a lectura-escritura.
In addition, you can also load a proxy for an instance by using the load method:
Adicionalmente puede utilizar el método load para cargar un proxy para una instancia:def p = Person.load(1)
This incurs no database access until a method other than getId() is called. Hibernate then initializes the proxied instance, or
throws an exception if no record is found for the specified id.
El uso de este método no incurre en acceso a la base de datos hasta que un método distinto a getId () es llamado, entonces Hibernate inicializara la instancia del proxy, o se producirá una excepción si no encuentra un registro con el id especificado.Update
Actualizar
To update an instance, change some properties and then call save again:
Para actualizar una instancia, modifique algunas propiedades y entonces llame save nuevamente:def p = Person.get(1)
p.name = "Bob"
p.save()Delete
Eliminar
To delete an instance use the delete method:
Para eliminar una instancia utilice el método delete:def p = Person.get(1) p.delete()

