我有3个域作为Subscriber,Role和一个连接表作为SubscriberRole,用于多对多的关系。动态查找器findAllBy从SubscriberRole域获取空值。
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:域
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域:
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
}
}
正如您从图像中看到的那样,subscriber_role有3个条目但是当我尝试使用findAllBySubscriber时,它会给我null值。任何想法,为什么会这样?
问题:即可。当我使用**
从SubscriberRole表中获取数据时Subscriber subscriber = Subscriber.get(params.id)
def subscriberRole = SubscriberRole.findAllBySubscriber(subscriber)
虽然SubscriberRole表在db中有条目,但我得到NULL [com.vproc.member.SubscriberRole:null]。 上面的动态查找器有什么问题。
答案 0 :(得分:1)
它以这种格式显示你([com.vproc.member.SubscriberRole:null])。 null必须是因为您在SubscriberRole上有复合键。
但是你需要根据需要获得适当的数据。
对于表示,您可以将好的toString添加到SubscriberRole,例如
public String toString()
{
return "${subscriber} :: ${role}"
}