Do grails domain classes have to be tied to a database?
Asked Answered
C

4

8

I am a complete noob when it comes to grails (and still very noobish when it comes to groovy) so I apologise if this is a dumb question.

I am building a simple web app and I want to control portions of the domain in my app based on file system objects (i.e. directory structure and file type) rather than database data. How easy is it to do this or are the domain objects so entwined with GORM that it is not worth the effort to try?

Cubby answered 16/8, 2010 at 1:52 Comment(1)
Not an answer, but you may be interested to hear that the persistence implementation is being abstracted away from GORM. This will allow GORM to work with noSQL implementations like Redis, Neo4j, etc. and others such as JCR repositories. It should be possible to add a basic filesystem implementation too. Keep an eye out for Grails announcements.Tamartamara
U
9

I ran into this question myself a couple of weeks ago.

You can just add the following snippet to the Domain Class.

def isAttached() 
{
   return false
}

Now it isn't connected to your database. Voila!

Urano answered 17/10, 2012 at 2:20 Comment(1)
It appears that while there was no answer when I asked later versions of grails do handle this situation.Cubby
R
4

You can also use:

class YourDomainClass {

    static mapWith = "none" // disable persistence for this domain class

See grails documentation and this answer. Appears to have been added in Grails 2.0.1 but not documented until version 2.3.0.

Rozalie answered 14/8, 2014 at 17:51 Comment(0)
S
1

A couple of ways to do this.

First, you can declare your properties that map to file system data as transient, and go to file system when getters / setters are called (you have to override them). You can also load them using onLoad if you need them to be in memory always.

Second - Hibernate handles the persistence. Hibernate allows for you to define your own user type, which can handle the persistence whichever way you want. This way it might happen for you more transparently (though you'd have to make sure you understand hibernate fairly well, to make sure there aren't any side effects, I am not sure).

http://i-proving.com/space/Technologies/Hibernate/User+Types+in+Hibernate

Secluded answered 16/8, 2010 at 7:16 Comment(0)
S
1

There is no built-in way to map domain classes to file system objects as you've described, but equally there is no requirement that your domain classes map to a relational database. The subject of how to create a Grails app that doesn't use a relational database is addressed here and here (and possibly elsewhere).

Suture answered 16/8, 2010 at 7:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.