Do grails domain classes have to be tied to a database?

后端 未结 4 1376
不知归路
不知归路 2021-01-02 12:31

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 a

相关标签:
4条回答
  • 2021-01-02 13:21

    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).

    0 讨论(0)
  • 2021-01-02 13:27

    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.

    0 讨论(0)
  • 2021-01-02 13:28

    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!

    0 讨论(0)
  • 2021-01-02 13:31

    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

    0 讨论(0)
提交回复
热议问题