Strange activity from many to many in grails - grails

Hi I am getting some strange activity from a many to many in grails.
It seems to be calling its self recursively.
my domains are set up like:
Product domain:
class Product {
String name
String comments
static hasMany = [components:Components]
}
Component domain:
class Components {
Product product
static hasMany = [alternatives:Product]
static belongsTo = Product
}
This seems to be causing a infinate loop and not saving the components correctly.
I know when using JSON.use("deep") on a Product I get a ../.. in components. The next strange thing is that. If I as for a product as JSON after I saved that product everything works fine, but when I try and get the same product as JSON later I get the ../.. in components.
I am totally lost about this.
If you require more details please let me know and I will me best to comply.

If you're going to have a many-to-many relationship you need to store the relationship data somewhere. If your relationship was one-to-many you could store them in the client table (like how you already have a component having one specific product in your component table) but in many to many you need the relationship to be its own table. To do this in Grails, use the JoinTable attribute or create a separate domain class to handle the relationship. Probably the fact that you have one-to-many relationship with Product already for Components and the many-to-many relationship with no join table is why you are getting the weird recursion issue.

Related

Can Grails 3.3 fields plugin f:display show many-to-many

I'm working with Grails 3.3.9 but I can change that as necessary.
I would like to let the Grails fields plugin manage my CRUD as generated, or at least with minimal manual changes. The problem I have not been able to solve is when I have a many-to-many relationship and want to show the related instances from either side.
A simple example: two domain classes, Company and Worker. Each have a String name and each have a hasMany:
static hasMany = [companies: Company] // in Worker
static hasMany = [workers: Worker] // in Company
I don't identify that either class belongsTo the other.
When I generate-all and run the app, then use Worker / new CRUD to create new instances of Worker, all seems fine.
Then, when I create a company using Company / new CRUD, I am offered a drop-down of the already defined workers, which looks good; so I shift-click on a couple to include them in the new company definition. But then after the save, the show CRUD does not show a list of selected workers for the Company instance I just created. There is a field label "Workers" but no value shown next to it.
I can't seem to find any obvious way to encourage f:display to show that list - or maybe somehow the multi-select didn't produce the desired results.
Yes it can. Solved my problem. This page:
https://github.com/grails-fields-plugin/grails-fields/blob/master/grails-app/views/templates/_fields/_list.gsp
shows what the template is (should be?) to render fields. Wondering if that was somehow different than what's in my version, I installed that file in
...grails-app/views/templates/_fields/_list.gsp
and suddenly my many-to-many relations were visible.
Extra info that may be useful for others stumbling over this:
I followed the GORM reference manual recommendation for creating a many-to-many relationship where each side includes a
static hasMany = [...]
and the "owned" side also has a
static belongsTo =
I think I may have been confused as to the meaning of the belongsTo in this case - what it means is well explained here:
http://docs.grails.org/3.0.4/guide/GORM.html#manyToMany

Grails: Simple hasMany relation create more tables than necessary

Hi I have a simple problem.
My domain class is like this:
class Example {
long seq
hasMany = [example_array: ExampleData]
long count
}
class ExampleData {
String type
long description
static belongsTo = Example
static constraints = {
}
}
This results in 3 tables, like a many to many relation.
Why is this?
Thanks
The reason for the extra table is that you've modeled the relation only in one direction - an Example can access its ExampleData instances via the example_array Set that's added to your class bytecode because of the hasMany property, but an ExampleData instance has no way to reference its owning Example.
You added a belongsTo property, but only specified the class name. That's sufficient to configure ownership, cascaded deletes, etc. but doesn't provide a property in the class to access the Example instance.
If you change it to the other supported syntax it will work as you expected:
static belongsTo = [example: Example]
Here example will end up being the name of an Example property (and you can change it and/or example_array to any valid property name), which is basically the same as declaring
Example example
Now that both sides can access the other, the relationship is bidirectional and you no longer need the third table. That's because a 1-many is typically implemented using a foreign key in the child table, in this case in the table for ExampleData that points to the table for Example. That wasn't possible without a property in the class to wire up to that column, so the join table was necessary.
I believe that you have to map the BelongsTo, like this:
static belongsTo = [example:Example]
Hope it helps :)
From the definition of hasMany Grails will, by default, map this kind of relationship with a join table.That join table is the 3rd table you mentioned.No need to worry about that.
Well the one-to-many relationship is constructed by having additional table (i.e. Example_ExampleData) containing two columns each id fields from tables of the entities forming the relationship(i.e. Example and ExampleData).
The newly added table is child to parent tables – Example and ExampleData.
So in your case when you run your application the 3rd table gets created by Grails by default as your table relationship falls under the one-to-many relationship.

How to make a domain class that belongs to one of a number of possible classes in Grails

I want to create a Comment class to allow my users to add comments to a number of different things.
class Comment {
User author
String text
static belongsTo = [post:Post, user:User]
static contraints= {
post(nullable:true)
user(nullable:true)
}
}
When I try and create a comment object using a post and without a user, i get the error "Property [user] of class [class Comment] cannot be null".
What is the best way to create a class that can belong to one of a number of different classes?
The best solution is to use a plugin like commentable.
However, if you want to roll your own solution then you need to understand that making something BelongTo another object makes it a required relationship. Thus it can't be null. Remove the belongs to and do a uni-directional one-to-many relationship. See the Grails documentation on modeling relationships.

Grails: Delete relation Domain

I want to asked about my project.
I Have Two Domain like this
I suggest you to look in the GORM documentation. You have some ways to declare your relationship between classes, and depending on them the delete will be cascade or not.
This behavior is explained in "6.3.3 Understanding Cascading Updates and Deletes".
Whether it is a one-to-one, one-to-many or many-to-many, defining
belongsTo will result in updates cascading from the owning class to
its dependant (the other side of the relationship), and for
many-/one-to-one and one-to-many relationships deletes will also
cascade.
So you should consider declaring hasMany and belongsTo, to enable the cascading deletes.
class User {
// Group details should not be referenced here
}
class Group {
String Name
String Description
GroupDetails gd
}
class GroupDetails {
User user
static belongsTo = [group:Group]
}
In this case if Group will have a child GroupDetails then when you delete Group, child entity(ies) will also be deleted.
Have a look at first example in grails docs: http://grails.org/doc/2.2.x/ref/Domain%20Classes/belongsTo.html
Also as Sérgio Michels there are more ways to make it work.
example: https://github.com/aprudnikovas/testGrailsOneToOneCascade

One-to-many relationship not working as expected

I'm having some problems getting a one to many relationship in grails working properly.
I have a person instance and this person has relationships to other persons. This relationship is defined in a relationship object.
The relevant code is as follows.
class Person {
static hasMany = [relationships:Relationship]
String name
}
class Relationship {
Person relationShipTo
// Enum containing married, living together, parent etc.
RelationshipType typeOfRelationship
}
Now what i want is a one to many reference to that relationship to be persisted but what happens in grails is that it seems to think the relationShipTo instance is refering back to the Person that has this relationship with someone else, and not to the other person.
So a person has a reference to a relationship, and that relationship has a type and a reference to the person with whom you have a relationship with.
I'm not able to change the domain model for this. Is there any way of accomplishing what i want?
What is currently happening if i use the generated views and controllers for the Relationship and try to create a relationship with a type and a person it is refering to, only the type is persisted and the person is ignored. When i then try to add it to the person in the persons edit or create page, all the relationShipTo properties of the relationships i add is saved with the id of the person.
Hopefully what i wrote is understandable.
Finally got it working.
Had to add a static mappedBy =[relationship: 'belongsTo'] to person
and a static belongsTo = [belongsTo: Person].
Not exactly how i wanted it but it works and is an ok compromise

Resources