Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>1) Domain model</h1> <p>Keep your code simple. Don't create generic data model. It's way to hell. When you personToPerson and personToStore keep separate, it's much easier to follow your code. </p> <p>Actually suggested solution makes it possible to access relations as consolidated and independent list simultaneously.</p> <p>For this problem I would use <a href="http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20(GORM).html#5.2.3%20Inheritance%20in%20GORM" rel="nofollow noreferrer">inheritance feature in GORM</a>.</p> <p>Your classes would look like this:</p> <pre><code>class Person { String name static hasMany = [personToPerson:PersonToPerson, personToProduct:PersonToProduct, personToStore:PersonToStore] static mappedBy = [personToPerson:"from"] } class Product{ String productName } class Relationship{ String note } class Store{ String storeName } class PersonToPerson extends Relationship{ Person from Person to String relation static constraints = { relation(inList:["Friend to", "Enemy of", "Likes", "Hates"]) } static belongsTo = [ from:Person ] } class PersonToProduct extends Relationship{ Person person Product product String relation static constraints = { relation(inList:["likes", "dislikes"]) } static belongsTo = [ person:Person ] } class PersonToStore extends Relationship{ Person person Store store String relation static constraints = { relation(inList:["Stock person", "Owner", "Manager", "Patron"]) } static belongsTo = [ person:Person ] } </code></pre> <p>DB schema for Person, Product and Store is usual. But for Relational domains look like this:</p> <p><strong>Relationship</strong></p> <pre><code>Field Type Null Default id bigint(20) No version bigint(20) No note varchar(255) No class varchar(255) No person_id bigint(20) Yes NULL product_id bigint(20) Yes NULL relation varchar(8) Yes NULL from_id bigint(20) Yes NULL to_id bigint(20) Yes NULL store_id bigint(20) Yes NULL </code></pre> <p>Relationship domain makes possible to access all relational domain throw one domain.</p> <h1>2) Constraint</h1> <p>Just switch <em>inList</em> to <em>validator</em>. Than you can store constrain in file or DB. See <a href="http://www.grails.org/Validation" rel="nofollow noreferrer">documentation</a> or <a href="http://grails.1312388.n4.nabble.com/Dynamic-inList-constraints-td2279264.html" rel="nofollow noreferrer">file example</a>.</p> <p>Example how to store constraint values in DB. First create a domain object.</p> <pre><code>class Constrain{ String name String type } </code></pre> <p>Than the domain class looks:</p> <pre><code>class PersonToPerson extends Relationship{ Person from Person to String relation static constraints = { relation(nullable:false, validator:{val, obj -&gt; def list = Constrain.findAllByType('person').collect{it.name} return list.contains(val) }) } static belongsTo = [ from:Person ] } </code></pre>
 

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