Find Parent By Child One to Many Relation Grails - grails

I have this classes:
class Parent{
static hasMany = [children:Child]
}
class Children{
static belongsTo = [Parent]
}
I want to do something like
Parent.findByChildren(ChildInstance)
In the database there is a table with the relationship id's, but I don't know how to access to it.
But that doesn't work, which is the proper way?
Thanks

Change your Children class belongsTo clause :
class Children{
static belongsTo = [parent: Parent]
}
This will allow you to access a child's parent instance : childInstance.parent

First I would change the relationship in Children domain to
static belongsTo = [parent: Parent] // suggested by #bassmartin
or
Parent parent
both does the same thing.
Once you have the ChildInstance and the reference to the parent, you can simply do
ChildInstance.parent // returns instance of parent
Similarly, if you want to find all the children of a parent, you can do
parent.children // return an array of children which you can iterate over.

Related

belongsTo array plus hasMany ownership

I have a Grails class that both has got a many-to-many relationship (with the other side being the owner) as well as a n:1 relationship.
I could not find an answer on the web how to put the ownership into the belongsTo clause.
This is the code:
class PanelType {
static hasMany = [elements: LabValueType]
}
class LabValueType {
static belongsTo = [labUnit: LabUnit]
// This is what would be needed to have a bidirectinal n:m relationship
// belongsTo = PanelType
// static hasMany = [panelTypes: PanelType]
}
If I leave it like this, the application builds the database correctly, but I won't be able to navigate from LabValueType to PanelType.
I found one answer (from 2008!) that said I should write:
static belongsTo = [PanelType, LabUnit]
BUT this way, the field lab_unit_id is not created in the database, so it does not seem to be correct.
I have found that I can work around this problem by declaring the relationships like this:
LabUnit labUnit
static belongsTo = PanelType
static hasMany = [panelTypes: PanelType]
But somehow it is not really 100% satisfying.

No owner defined between domain classes

Currently i'm getting the following error :
No owner defined between domain classes [class mp.ra.Classgroup] and [class mp.ra.Event] in a many-to-many relationship.
The domain classes are set up as followed,
The event class:
class Event {
static hasMany = [classgroups:Classgroup]
static belongsTo = [eventgroup:Eventgroup,classgroup:Classgroup]
static constraints = {
eventgroup nullable:true
}
And the Classgroup
class Classgroup {
static hasMany = [courses:Course,events:Event]
static constraints = {
courses nullable:true
}
An Event can have multiple classgroups and a classgroup can have multiple events.
I use the belongs to property so i don't see why i'm getting this error.
EDIT: I changed The Event class and the error is gone , i dont know if this is a good solution
class Event {
Eventgroup eventgroup
static hasMany = [classgroups:Classgroup]
static belongsTo = [Eventgroup, Classgroup]
static constraints = {
eventgroup nullable:true
}
In Grails many-to-many mapping we should define an owner class between both of the associated classes.
As earlier you defined :
belongsTo = [eventgroup:Eventgroup,classgroup:Classgroup]
with this GORM tried to create a column with classgroup name under Event table, but for many-to-many association with classgroup, database should suppose to have third table to have multiple records for many-to-many associations between Event and Classgroup. So was giving such error.
Hence later when you mention :
belongsTo = [Eventgroup, Classgroup]
it worked as it just got owner information.
So here Classgroup would be the owner of association, as Event belongsTo Classgroup.
Hope this helps. Thanks.

Many to many relationship in grails and status of each record

I have 2 domain models having many to many relation with themselves
Candidate{
String name
static hasMany = [positions:Position]
}
Position{
static hasMany = [candidates:Candidate]
static belongsTo = [Candidate]
}
my requirement is to fetch the shortlisted status of each candidate for each position, but grails create the intermediate table itself so any idea how to implement it.
Any comments,ideas,examples will appreciated.
If the status is to be stored on the relationship you could create a mapping domain class for the relationship like described in this answer.
The statuses could then be retrieved by (assuming a CandidatePosition class name):
def candidatePositionList = CandidatePosition.findAllByCandidate(candidateInstance)
candidatePositionList.each {
it.position.name // Position name
it.status // Status of candidate for Position
}

How can a Domain Class cascade 'all-delete-orphan' to a child it has no reference to?

In Grails you can have a child class:
class Child {
Father father
static belongsTo = [Father, Mother]
}
With two parent classes
class Mother{
}
class Father {
}
It appears that if I father.delete(), then Grails throws a database error saying that the Father can't be deleted because the child is still around.
How do I cascade all-delete-orphan the Child if the Father class doesn't have a direct reference to the Child class?
Make it bi-directional using hasMany.
class Mother{
static hasMany = Child
}
class Father{
static hasMany = Child
}
Doing this should make the cascading work such that when you delete one of the parents the child will also be deleted.
Peter Ledbrook has a good article that covered this
GORM Gotchas Part 2
I couldn't get the belongsTo only part to work, but this works for me:
class Father {
static hasMany = [children: Child]
}
class Child {
static belongsTo = [father: Father]
}
void testDeleteItg() {
def father = new Father().save()
def child = new Child()
father.addToChildren child
child.save()
def childId = child.id
father.delete(flush:true)
assertNull(Child.get(childId))
}

Grails GORM self-referential belongsTo deletes opposite direction from expected

I have a Grails domain class that is a hierarchy of categories. Each Category has a parent category (except for the root category which is null).
class Category {
String name
static mapping = {
cache true
name index:'category_name_idx'
}
static belongsTo = [parent:Category]
static constraints = {
parent(nullable:true)
}
}
My problem: deletes cascade exactly opposite of what I'd expect:
someSubCategory.delete() deletes the category then tries to delete the parent category (which fails with an integrity violation if the parent has other children).
parentCategory.delete() does NOT cascade delete its children, but instead just fails with an integrity violation.
What am I doing wrong? My understanding is that the 'belongsTo' above should tell the GORM to cascade deletes from the parent to all children, but not from a child to its parent.
If I am understanding correctly a Category belongs to a parent and a parent can have multiple children, so I think you need a hasMany relationship, something like this:
class Category {
String name
static mapping = {
cache true
name index:'category_name_idx'
}
static belongsTo = [parent:Category]
static hasMany = [children: Category]
static constraints = {
parent(nullable:true)
}
}
I had had similar structures and never have issues with the delete doing it this way.
Hope this helps!
It's not an answer, but I found a workaround to my own question. You can remove the belongsTo = [parent:Category], replacing it with a simple instance variable. This stops subCategory.delete() from cascading to the parent.
class Category {
String name
Category parent
static mapping = {
cache true
name index:'category_name_idx'
}
static constraints = {
parent(nullable:true)
}
}

Resources