Note that there are some explanatory texts on larger screens.

plurals
  1. POGroovy Map vs. Table Relations within Domain Classes - How to decribe & persist this?
    text
    copied!<p>In Grails/Gorm I am storing a <strong>physical site</strong> which <strong>holds objects</strong> at <strong>defined positions</strong>.</p> <p>The domain class would simply be:</p> <h3>EXAMPLE A</h3> <p><em>SITE DC</em></p> <ul> <li>sitename</li> <li>positionmap (1: dog, 2: cat, 3:foo, 4:bar, ... up to an average of 30 items)</li> </ul> <p>... but the objects change their positions and I need to later be able to see WHICH positions were occupied by WHAT at a given time (WHEN).</p> <p>Thus I added another domain class</p> <h3>EXAMPLE B</h3> <p><em>POSITIONS DC</em></p> <ul> <li>validfrom</li> <li>validto</li> <li>positionmap (1: dog, 2: cat, 3:foo, 4:bar)</li> <li>belongsTo <em>SITE</em></li> </ul> <p><em>SITE DC</em></p> <ul> <li>sitename</li> </ul> <p>As all the objects will have a description AND there will be loads of SITEs using the same objects in it, I added a domain class to hold each and every OBJECT. Result:</p> <h3>EXAMPLE C</h3> <p><em>OBJECT DC</em></p> <ul> <li>name</li> <li>description</li> </ul> <p><em>POSITIONS DC</em></p> <ul> <li>validfrom</li> <li>validto</li> <li>positionmap (1: reference_to_dog, 2: reference_to_cat, 3:reference_to_foo, 4:reference_to_bar)</li> <li>belongsTo <em>SITE</em></li> </ul> <p><em>SITE DC</em></p> <ul> <li>sitename</li> </ul> <p><em>Now the Map, to me, doesn't seem reasonable anymore.</em></p> <p>I am thinking about removing the map, replacing it by yet another domain class:</p> <h3>EXAMPLE D</h3> <p><em>OBJECT DC</em></p> <ul> <li>name</li> <li>description</li> </ul> <p><em>OBJECT-TO-POSITION DC</em></p> <ul> <li>positionnumber (1, 2, 3, 4, ...)</li> <li>object (reference_to_cat, reference_to_dog, reference_to_foo, ...)</li> <li>belongsTo <em>POSITIONS</em></li> </ul> <p><em>POSITIONS DC</em></p> <ul> <li>validfrom</li> <li>validto</li> <li>belongsTo <em>SITE</em></li> </ul> <p><em>SITE DC</em></p> <ul> <li>sitename</li> </ul> <h3>QUESTIONS</h3> <ol> <li>Is it generally a good/bad idea to put maps into the database?</li> <li>How could this be achieved easier?</li> <li>What's the most performant way to do it?</li> </ol> <h3>EDIT: A NEW APPROCH BASED ON ROBS ANSWER</h3> <p>Inspired by your suggestion, Rob, I drafted this data model which grants a "front-row" role to the "SeasonPlan" (amended "ResidenceHistory").</p> <pre><code>class Zoo { static hasMany = [seasons: SeasonPlan] String name } // one way of representing histories class SeasonPlan = { static belongsTo = [zoo: Zoo] // a SeasonPlan belongs to a single particular Zoo static hasMany = [cages: Cage] DateTime from DateTime until } class Cage { static belongsTo = [seasonPlan: SeasonPlan] // a cage belongs to a single seasonplan Species species // a cage has a single Species Integer cageNumber } class Species { // static hasMany = [cages: Cage] // commented out - no reverse-lookup necessary String name } </code></pre> <p>This has one drawback: For every season plan there's a new cage - though <em>in reality the cages stay the same</em>! (Imagine a "Integer squareMeters" within the "Cage" to make it more obvious why this isn't desired.)</p> <p>For me applying such a thing to a data model is often hard to comprehend - <strong>how do I fit "pseudo-static" data like this into the application while retaining real-world corelation?</strong></p> <p>I hope what I mean is understandable - sorry if not.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload