0
votes

I'm developing a Grails application that will access a large legacy database (that will continue to be used by other legacy software). I've created various groovy classes to mimic domain models for the legacy database (because they pull data from multiple tables they can't be actual grails domain model classes since grails/hibernate can only do one table per class).

I'm now working on new parts of the application that will add new tables to the database, so for these I can use actual grails domain model classes and have grails/hibernate do its thing. The problem I'm running into is that my actual grails domain model classes need to have some of the pretend domain model classes as their properties. However, hibernate fails on this since it doesn't know how to deal with these pretend domain model classes.

I'm looking for suggestions on how to deal with this situation from a design point of view. One way that I can think of is to not actually have the pretend domain model class be a property of the grails domain model class, but instead store some sort of unique identifier that can identify/recreate the pretend domain model class. However, I'm hoping for a more elegant solution to this or if there isn't one, then a way to automate this, so that it's as seemless as possible.

Any suggestions, idea, etc. are much appreciated.

4
just thinking aloud - can you create db views that mirror your domain class, but are based on the multiple tables? - aldrin

4 Answers

0
votes

If you really cannot make the pretend domain models real domain models, an option you have is to keep the pretend domain model classes in transient properties in your real domain model classes and then manage these by hand inside Hibernate events:

class RealDomain {

    transient pretendDomain

    def beforeValidate() { /* validate pretendDomain */ }
    def beforeInsert() { /* save pretendDomain */ }
    def beforeUpdate() { /* update pretentDomain */ }
    // and so on

}
0
votes

You can put a list of transients in the domain object like this:

MyObject1 property1
MyObject2 property2

static transients = ['property1', 'property2', etc.]
0
votes

I ended up using custom hibernate types to solve my problem (http://grails.org/doc/2.3.x/guide/GORM.html#customHibernateTypes). They allow me to automate the process of storing some sort of identifier(s) in the domain model class to recreate/reload the pretend domain model class.

0
votes

You can also depict the non-domain class with the static mapWith GORM property. More info here: The Grails Framework - mapWith