Questions about implementing Domain class relationships & constraints in Grails - grails

Using ArgoUML, I very quickly created this trivial representation of a few Domain classes (Person, Store, Product) and their relationships.
I'm struggling with the implementation of the relationships. Below was my initial approach for the Person domain, but it seems that I am missing something important.
class PersonToPerson {
Person from
Person to
String relation
static constraints = {
relation(inList:["Friend to", "Enemy of", "Likes", "Hates"])
}
static belongsTo = [ Person ]
}
class Person {
String firstName
String secondName
.
.
.
static hasMany= [ personToPerson:PersonToPerson,
personToStore:PersonToStore ]
}
Edit: updated question for clarity
After thinking on the problem I think I have a better way to ask the question(s). In the implementation of PersonToPerson above I have the relation as a simple string. I want the user to be able to select from a list of unique relations, which are defined in the constraints, for the string value for PersonToPerson. So this leads to the questions...
Should personToPerson and personToStore be consolidated into one list of type Relationship? Or should they stay independent lists as shown?
What is the mechanism to allow the user to add new values to the relation constraint?

1) Domain model
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.
Actually suggested solution makes it possible to access relations as consolidated and independent list simultaneously.
For this problem I would use inheritance feature in GORM.
Your classes would look like this:
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 ]
}
DB schema for Person, Product and Store is usual. But for Relational domains look like this:
Relationship
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
Relationship domain makes possible to access all relational domain throw one domain.
2) Constraint
Just switch inList to validator. Than you can store constrain in file or DB.
See documentation or file example.
Example how to store constraint values in DB. First create a domain object.
class Constrain{
String name
String type
}
Than the domain class looks:
class PersonToPerson extends Relationship{
Person from
Person to
String relation
static constraints = {
relation(nullable:false, validator:{val, obj ->
def list = Constrain.findAllByType('person').collect{it.name}
return list.contains(val)
})
}
static belongsTo = [ from:Person ]
}

Look fine. You may want to consider a belongsTo in the PersonToPerson class.
Also, your has many in Person should be: [ personToPersons:PersonToPerson.... <- remove the s

Related

Unwanted GORM table creation

I have two domain classes, Person and Workshop. A Workshop has an owner of type Person, and many participants of type Person. A Person can be the participant of many workshops. When enrolling people in workshops I want to do so from the Workshop side like workshop.AddToParticipants() so here is how I set up my domain classes.
class Person {
String name
static hasMany = [enrolledWorkshops: Workshop]
static belongsTo = [Workshop]
}
class Workshop {
Date startDate
Date endDate
String name
Person owner
static hasMany = [participants: Person]
}
GORM correctly creates a WORKSHOP_PARTICIPANTS table with WORKSHOP_ID and PERSON_ID columns, and adds an OWNER_ID column to the WORKSHOP table. This is all good.
However, it ALSO creates a WORKSHOP_OWNER table with PERSON_ID and OWNER_ID columns! This makes no sense to me, and no matter how I try changing the GORM relationships I just can't get it to work how I want without this annoying extra table being created. How can I prevent the WORKSHOP_OWNER table from being created? Any help is greatly appreciated! (if it is of any help, I am using Grails 2.3.7)
In order to get rid of the WORKSHOP_OWNER table you'd have to replace Person owner with static belongsTo = [owner: Person] in the Workshop class. But that would conflict with static belongsTo = [Workshop] in the Person class. Both can't be owners.
Try this:
class Person {
String name
}
class Workshop {
Date startDate
Date endDate
String name
static belongsTo = [owner: Person]
static hasMany = [participants: Participant]
}
class Participant {
person person
static belongsTo = [workshop: Workshop]
}
In the above example, a Person owns a Workshop, and a Participant is owned by a Workshop. The workshop table will get a owner_id to refer to the person, which gets rid of the workshop_owner table.
To add a Person to a Workshop you simply wrap it in a Participant
workshop.addToParticipants(new Participant(person: somePerson))
You'd loose the enrolledWorkshops property in Person, but you can accomplish the same thing with a query.
def workshops = Workshop.withCriteria {
participants {
eq 'person', somePerson
}
}

how to add object in hasMany element

I have two domain class
class Company{
String name
....
static hasMany[product:product]
}
class Product{
String Pname
String Qty
}
i am tring to add product like this
Company comp= Company.get(1)
Product pdct = Product.findByPname("procut1");///procunt name is unique
comp.product.add(pdct)
comp.save(flush:true)
the above statement are successfully executed
but when i try to find comp.product i got a empty list like []
i also try addTo but this give exception
so what m i missing?
You should be using the GORM methods for addTo and removeFrom when adding and removing members of your collection.
comp.product.add(pdct)
should be:
comp.addToProduct(pdct)
Change this as static hasMany[products:Product]
Company comp= Company.get(1)
Product pdct = Product.findByPname("procut1");///procunt name is unique
comp.addToProducts(pdct)
comp.save(flush:true)
Also add static belongsTo = [company:Company] in your Product class to apply Cascade operations

How to set version property on domain object creation?

I'm trying to model a parent/child relationship.
I have the following domain class:
package test
class Person {
Person mother
Person father
String name
static hasOne = [father: Person, mother: Person]
static constraints = {
name()
father(nullable:true)
mother(nullable:true)
}
def Set<Person> children(){
return Person.findAllByMother(this)
}
}
I have performed generate-all
However, if I try to create a new Person, I get the following error:
Message: Parameter "#2" is not set; SQL statement:
insert into person (id, version, father_id, mother_id, name) values (null, ?, ?, ?, ?) [90012-164]
Line | Method
->> 329 | getJdbcSQLException in org.h2.message.DbException
Where should the version parameter be generated? I thought that this should be generated auto-magically during the save call.
UPDATE: The issue seems to be related to the father/mother relationship, since removing it and re-generating views means that elements are persisted ok.
The issue seems to be related to the fact that I had tried to create a di-directional relationship in the class. This in fact is not required. There is a uni directional relationship Person -> father and Person -> mother. The inverse is calculated under children (which I shall extend to include father as well.)
My final class is:
package test
class Person {
int id
Person mother
Person father
String name
static constraints = {
name()
father(nullable:true)
mother(nullable:true)
}
def Set<Person> children(){
return Person.findAllByMother(this)
}
}
I'm still new to Grails.

Columns in join table are NOT NULL

I have a domain class:
class Author {
String name
static hasMany = [superFantasticAndAwesomeBooks: Book, superBadAndUltraBoringBooks: Book]
}
This is all nice when using the in-memory database, however, when running on Oracle the Book collections are modeled in a join table which cannot be created because the column names are too long.
So, then I tried specifying the join table properties:
static mapping = {
superFantasticAndAwesomeBooks joinTable: [key: awesomeBooks]
superBadAndUltraBoringBooks joinTable: [key: boringBooks]
}
The problem (which doesn't happen if joinTable isn't specified) is that the join table is created where columns correspoinding to awesomeBooks and boringBooks are NOT NULL (they need to be nullable because a Book will be an awesomeBook or a boringBook)
Is there any way to configure joinTable to allow NULL columns?
Another option would be to map the join table yourself with a Domain class, for example:
class AuthorBook {
Author author
Book book
String status
static constraints = {
author(nullable:false)
book(nullable:false)
status(nullable:false,inList:['SuperFantasticAndAwesome','SuperBadAndUltraBoring'])
}
}
So your Author class becomes:
class Author {
...
static hasMany = [authorBooks:AuthorBook]
}
In this way the status is stored as a value of the join and statuses can be added, updated, or removed as needed in the future. It does have the side effect of having to query through the AuthorBook class to get to the associated Books.
See also: http://grails.org/Many-to-Many+Mapping+without+Hibernate+XML
I just ended up using 2 join tables:
static mapping = {
superFantasticAndAwesomeBooks joinTable: [name: 'awesomeBooks', key: 'book_id']
superBadAndUltraBoringBooks joinTable: [name: 'boringBooks', key: 'book_id']
}

how to made one-to-one bidirectional relationships in grails?

I have two domain classes and want to have one-to-one BIDIRECTIONAL relation between them. I write:
class Person {
Book book;
String name
Integer age
Date lastVisit
static constraints = {
book unique: true // "one-to-one". Without that = "Many-to-one".
}
}
class Book {
String title
Date releaseDate
String ISBN
static belongsTo = [person:Person] // it makes relationship bi-directional regarding the grails-docs
}
So, i want to have bi-directional, i could NOT find link from Book to Person in generated SQL:
CREATE TABLE `book` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`isbn` varchar(255) NOT NULL,
`release_date` datetime NOT NULL,
`title` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
So then it means it is not bidirectional then? How to make bidirectional?
check out the hasOne property, in which class you define hasOne and belongsTo depends on where you want the FK to be stored, check this grails doc regarding hasOne:
http://www.grails.org/doc/latest/ref/Domain%20Classes/hasOne.html
To make this relationship one to one bidirectional,you should define them as fallows
//Person is the owning side of relationship
class Person {
//a foreign key will be stored in Book table called person_id
static hasOne = [book:Book]
static constraints = {
book unique: true
}
}
Class Book{
Person person
}
belongsTo is used for specifying the owner side(which manages relationship) of a one-to-many, many-to-one, or many-to-many relationship and for making relationship bidirectional
belongsTo should be used always in owned side

Resources