Grails Framework's domain and Gorm

Source: Internet
Author: User
Tags grails

A grails overview

"Simplicity is the ultimate sophistication"-lenardo da Vinci

As the art guru Leonardo da Vinci said, "Simplicity is the ultimate complex form", the most beautiful things are often the most essential, the simplest thing. Grails is a lightweight, open-source framework for simplifying java-web enterprise application development. Traditional Java programmers have experienced writing complex and daunting XML configuration files, Grails is based on "convention is over configuration" (Convention greater than config) and "Don t Repeat yourself" (Do not make wheels repeatedly) Development concept, developers no longer have to manually write complex and cumbersome configurations, and focus more on business logic to greatly improve development efficiency. Grails is a higher layer of abstraction built on spring and hibernate, based on the JVM and using groovy's dynamic language to seamlessly integrate the Java Platform's development technology. In layman's terms, Grails is a simplified framework based on traditional Java development techniques designed to quickly build small Web applications based primarily on Hibernate, Spring, Sitemesh (a robust and stable page layout rendering framework), Tomcat, H2 ( A relational database management system implemented using pure Java technology. The relationship between the technology stack components is as follows:

To be sure, Grails is a full-stack development environment, not just a web open-source framework, and it can be seen that grails is based entirely on the Java ecosystem, where groovy is a more flexible, dynamic language derived from Java5, seamlessly fused with Java, and based on the JDK, Share most of the APIs. Therefore, it is easy for Java programmers to master the Grails framework.

Grails's advantages are mainly in the development of high efficiency, suitable for the rapid construction of small web applications, to meet most of the system requirements are not very high development, anything must have its short. Grails is based on the traditional open-source framework on the higher layer of abstraction technology, the bottom-level implementation has been well encapsulated, so not very well qualified to build large-scale enterprise application development, this may be grails has been in the open source community but not very good promotion reasons, so give people feel this technology is very small, Lack of good application prospects. Architecture, Grails rejects the traditional DAO layer, directly divided into the view layer, controller (control) layer, service (business logic) Layer 3 layer architecture, the specific request processing logic as shown:

Design of two domain data model

The domain entity class is in the core position in any application scenario, it is the encapsulation abstract form of the data, so its importance is self-evident. The domain entity class in the Grails framework is primarily used for data model design, can define its constraints and relationship between entity classes, and maps to the database's inter-table relationships. Case is a good way to describe things, can make the concept of abstract complex image, simplification. Suppose you need to build an online song store application, users can buy songs, comment songs, preview songs, etc., in order to simplify the description of the problem without regard to the implementation of the system, according to demand analysis of the application at least than include artist (artist), Album (album), Song (songs) 3 entities, Each entity has its own properties, and each artist (artist) will have more than one album (album), and each album contains several songs.

Data persistence is a frequently used operation in development, and grails maps each entity class to a table in the database, with attributes mapped to corresponding fields. To ensure data integrity and avoid dirty data, Grails Domain class can set constraints and relationships between tables. Grails provides a variety of relationships between domain classes, including one-to-one (the simplest), such as a one-to-many relationship between car and engine

Class car{    Engine engine}class engine{   car Car}

The model is a one-to-two relationship, and there is no affiliation, but in the actual situation, we will often encounter inter-entity dependencies, such as an engine belongs to a car,but a Car does don't belong to an engine grails mention Mechanism for implementing the relationship.

Class car{    Engine engine}class engine{    static  belongsto=[Car:car]}// an Engine belongs to a Car

Engine class Belongsto attribute value is a map, key value key is car (reference to car Class), Belongsto indicates that the car entity is the owner of the relationship, when the car table in the database refers to the engine primary key ID as the outer key, There is no foreign key application in the engine table. If you want the engine to refer to the car table's primary key ID as the outer key, you can define the relationship between the two:

Class car{    engine engine   static hasone=[engine:engine]}class engine{      Static belongsto=[Car:car]}

If an engine belongsto a car,but have no reference to its owner, you can locate the engine entity as:

Class engine{   static belongsto=car}//There is no foreign key referencing the Car table in this Engine table

Regression to the case itself, grails in One-to-many is also very simple, according to needs analysis: (artist and album is a 1-to-many relationship, album and song is also a one-to-many relationship), while defining the relationship can also define the constraints:

//artistclassArtist {String nameStaticconstraints ={Name (nullable:false, Unique:true)//define a non-null UNIQUE constraint    }    Staticmapping ={version (false)//Eliminate version default propertiesID: ' Identity '//primary key self-increment    }    StaticHasmany = [Albums:album]//establish a one-to-many relationship with album}//albumsclassAlbum {String titleStaticconstraints = {    }    Staticmapping ={version (false) ID:' Identity '    }    StaticHasmany = [Songs:song]//establish a one-to-many relationship with song    StaticBelongsto = [Artist:artist]//album table foreign key references artist table's primary key}//SongsclassSong {String title Integer duration//Song duration    Staticconstraints ={title (blank:false, Unique:true)    }    Staticmapping ={version (false) ID:' Identity 'Duration (min:1)    }    StaticBelongsto = [Album:album]//Song table foreign key application album table primary key}

At this point, the relationship between the entities is: the database is mapped to artist, Album, song three tables, where artist and Album artist_id Association, Album and song album_id Association.

Hibnerate has done a good job of encapsulating the Grails bottom layer, making the routine crud operations exceptionally simple, and the entity class simply calls the API to complete the data persistence operation, where the description is no longer expanded

Two Gorm principle

Grails Framework's domain and Gorm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.