GithubHelp home page GithubHelp logo

spring-xd-gorm's Introduction

Groovy, Gorm with Spring XD

This piece covers the setup and use of Gorm (Grails ORM layer) as a stand-alone entity that can be used by Spring XD jobs and modules.

Foremost - why? What's Gorm and why use it with Spring XD?

Gorm is an extension to Hibernate in a way. Hibernate is good at ORM i.e. mapping classes to database objects. Hibernate is also good at querying. What Hibernate is not good at is the actual usage. There's simple too much you have to do get things going and too much noise afterwards. You have to annotate classes (or worse use XML descriptors), you have to setup persistent context, you have to use Hibernate session to execute actual queries. Now these are the areas addressed by Gorm. With Gorm you can use meaningful defaults. Let's have a look at this Groovy class

class EntityName {
	String name
	String value
	String someOtherValue
	Date lastUpdated
	Date dateCreated
}

A class like this when seen by Gorm would automagically get converted into something similar

@Entity
@Table (name = "entity_name")
class EntityName {
	@Id @GeneratedValue
    @Column(name = "id")
	int id

	@Version
	int version

    @Column(name = "name")
    String name

    @Column (name = "value")
    String value

    @Column (name = "some_other_value")
    String someOtherValue

    @Column (name = "last_update")
	Date lastUpdated

	@Column (name = "date_created")
	Date dateCreated


    @PrePersist
    void onCreate() {
       dateCreated = new Date();
    }

    @PreUpdate
    protected void onUpdate() {
       lastUpdated = new Date();
    }
}

As you can see you can get a lot for free just by using Gorm. Now that's just a tip of the iceberg as with Gorm you also get validations, constraints and many, many more.

Hope it's clear now that it makes sense to use Gorm even outside of Grails. So what else you have to do to get Gorm running in Spring XD?

First of all, you have to get all Gorm dependency classes loaded by the initial classloader. To this end you can either copy all the classes into the lib folder (you don't really want to do that do you?) or tweak the startup script a bit and add another folder , say called ext, to added onto the classpath. See the attached patch for details.

Now with all the required classes on the classpath we can start playing with Gorm. First, we need to add it into our module's context.

<gorm:sessionFactory base-package="your.package" data-source-ref="dataSource"
                     message-source-ref="messageSource">
    <property name="hibernateProperties">
        <util:map>
            <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        </util:map>
    </property>
</gorm:sessionFactory>

Obviously you have to define the messageSource and dataSource (dataSource is normally already bound the one define in the main yaml file) and the messageSource is easy to define

<bean id="messageSource"
      class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages"/>
</bean>

Just add all your messages files on the classpath (your module config will suffice). And that's pretty much it.

All your domain classes need to be annotated by the import grails.persistence.Entity annotation and have to implement equals and hash - for the lazy use the import groovy.transform.EqualsAndHashCode annotation and an AST transformation will take care of it for you.

There are several things you have to keep in mind should you wish to share the model classes between stand-alone Gorm and Grails' Gorm.

  • Autostamping is not working (dateCreated, lastUpdated). You can always use the beforeUpdate closures though.
  • You have to use session explicitly (DomainClass.withSession) or make a component using Gorms domain classes org.springframework.transaction.annotation.Transactional. In both cases you just make sure the Hibernate session is threadlocal.
  • The Entity annotation adds (or more specifically AST adds) some extra fields to the domain classes. Gorm then tries to blindly maps those to the underlaying model and the fails. To avoid this add these fields into the transient section
static transients = ['errors', 'instanceGormInstanceApi', 'instanceGormValidationApi',
'staticGormStaticApi', 'instanceConvertersApi', 'attached', 'dirty']

spring-xd-gorm's People

Contributors

ondrej-li avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.