Vieenay Siingh
Vieenay Siingh

Reputation: 867

dynamic fnder findAllBy getting null value from join table in many to many relationship using grails

I have 3 domain as Subscriber, Role and a join table as SubscriberRole for many to many relationship. Dynamic finder findAllBy getting null value from SubscriberRole domain.

Subscriber.groovy:

class Subscriber extends PartyRole{

    Date dateCreated
    Date lastUpdated
    List<Contact> contacts =  new ArrayList<Contact>();

    static belongsTo = [ customer: Customer]
    static hasMany = [seats: Seat, ownedEnquiries: Enquiry,enquiresSharedWith: SharedEnquiry, enquiriesSharedBy: SharedEnquiry ,
    managedTeams: Team , memberships: Membership, contacts: Contact , sharedByContacts: SharedContact, sharedWithContacts: SharedContact,
    vContacts: VContact, partOf: VContact,  sharedbyVContacts: SharedVcontact, sharedWithVcontacts: SharedVcontact, tokens : Token,
    notifications: Notification, discussions: Discussion]
    static mappedBy = [ managedTeams : "manager" , enquiresSharedWith: "sharedWith" , enquiriesSharedBy: "sharedBy"  ,
                                                   sharedByContacts : "sharedBy" , sharedWithContacts : "sharedWith" ,
                                                   vContacts: "forSubscriber"  ,  partOf :"ofContact",
                                                   sharedbyVContacts: "sharedby" , sharedWithVcontacts :"sharedWith"
                                                    ]

    StatusEnum status
    static constraints = {

        contacts nullable: true
        notifications nullable : true

    }


    Set<Role> getAuthorities() {
        SubscriberRole.findAllBySubscriber(this).collect { it.role } as Set
    }

    public Subscriber(){
        contacts = LazyList.decorate(contacts,  FactoryUtils.instantiateFactory(Contact.class))
    }




}

SubscriberRole.groovy:Domain

import org.apache.commons.lang.builder.HashCodeBuilder

class SubscriberRole implements Serializable {

    Subscriber subscriber
    Role role
    Date dateCreated
    Date lastUpdated

    boolean equals(other) {
        if (!(other instanceof SubscriberRole)) {
            return false
        }

        other.subscriber?.id == subscriber?.id &&
            other.role?.id == role?.id
    }

    int hashCode() {
        def builder = new HashCodeBuilder()
        if (subscriber) builder.append(subscriber.id)
        if (role) builder.append(role.id)
        builder.toHashCode()
    }

    static SubscriberRole get(long subscriberId, long roleId) {
        find 'from SubscriberRole where subscriber.id=:subscriberId and role.id=:roleId',
            [subscriberId: subscriberId, roleId: roleId]
    }

    static SubscriberRole create(Subscriber subscriber, Role role, boolean flush = false) {
        new SubscriberRole(subscriber: subscriber, role: role).save(flush: flush, insert: true)
    }

    static boolean remove(Subscriber subscriber, Role role, boolean flush = false) {
        SubscriberRole instance = SubscriberRole.findBySubscriberAndRole(subscriber, role)
        if (!instance) {
            return false
        }

        instance.delete(flush: flush)
        true
    }

    static void removeAll(Subscriber subscriber) {
        executeUpdate 'DELETE FROM SubscriberRole WHERE subscriber=:subscriber', [subscriber: subscriber]
    }

    static void removeAll(Role role) {
        executeUpdate 'DELETE FROM SubscriberRole WHERE role=:role', [role: role]
    }

    static mapping = {
        id composite: ['role', 'subscriber']
        version false
    }
}

Role.groovy domain:

package com.vproc.member

import java.util.Date;

class Role {

    String authority
    Date dateCreated
    Date lastUpdated

    static mapping = {
        cache true
    }


    static constraints = {
        authority blank: false, unique: true
    }
}

enter image description here

As you can see from image, subscriber_role has 3 entries but when I try with findAllBySubscriber, it gives me null value. Any idea,why this is happening?

Problem:. When I fetch data from SubscriberRole table using**

Subscriber subscriber = Subscriber.get(params.id) 
def subscriberRole = SubscriberRole.findAllBySubscriber(subscriber)

I am getting NULL [com.vproc.member.SubscriberRole : null] though SubscriberRole table has entries in db. Is there anything wrong with above dynamic finder.

Upvotes: 0

Views: 365

Answers (1)

swapy
swapy

Reputation: 290

It's showing you in that format ([com.vproc.member.SubscriberRole : null]). that null must be because you have composite key on SubscriberRole.

But you are getting proper data as needed.

For representation you could add nice toString to your SubscriberRole, like,

public String toString()
{
    return "${subscriber} :: ${role}"
}

Upvotes: 1

Related Questions