我正在使用带有 MySQL 和 vaadin 的 hibernate、spring security 和 spring security acl 插件编写一个带有 grails 的基本 Web 应用程序,用于管理委员会、成员以及委员会和成员之间的成员资格。所有 CRUD 都适用于成员和委员会,但是当我创建一个 CommitteeMember 对象时,它无法引用该类的 Member 字段来获取有关成员资格所涉及的成员的信息。这是我的课程:
class Committee {
String name
String description
String bylaws
static hasMany = [members: CommitteeMember]
static constraints = {
name blank: false, nullable: false, matches: "[a-zA-Z0-9 ]*", unique: true
description blank: false, nullable: false, matches: "[a-zA-Z0-9 ]*"
bylaws blank: false, nullable: false, matches: "[a-zA-Z0-9 ]*"
}
}
class Member {
String firstName
String middleName
String lastName
String address
String phone
static hasMany = [committees: CommitteeMember]
static constraints = {
firstName blank: false, nullable: false, matches: "[a-zA-Z]*"
middleName blank: false, nullable: false, matches: "[a-zA-Z]*"
lastName blank: false, nullable: false, matches: "[a-zA-Z]*"
address blank: false, nullable: false, matches: "[0-9a-zA-Z ]*"
phone blank: false, nullable: false, minSize: 10, maxSize: 10, matches: "[0-9]*"
}
}
class CommitteeMember {
Member member
Committee committee
Date startDate
Date endDate
String title
Date created = new Date()
Date updated = new Date()
static constraints = {
startDate blank: false, nullable: false, format: 'mm/dd/yyyy'
endDate blank: false, nullable: false, format: 'mm/dd/yyyy'
title blank: false, nullable: false, matches: "[a-zA-Z0-9 ]*"
created blank: false, nullable: false, editable: false, display: false
updated blank: false, nullable: false, editable: false, display: false
}
}
以及添加这些类的服务层的方法:
@Transactional
def saveCommittee(name, desc, bylaws) {
new Committee(name: name, description: desc, bylaws: bylaws).save(flush: true);
}
@Transactional
def saveMember(fname, mname, lname, address, phone) {
new Member(firstName: fname, middleName: mname, lastName: lname,
address: address, phone: phone).save(flush: true);
}
@Transactional
def saveCommitteeMember(cname, lname, fname, title, start, end) {
def cmem = new CommitteeMember(title: title, startDate: start, endDate: end);
cmem.member = Member.findByLastNameAndFirstName(lname, fname);
cmem.committee = Committee.findByName(cname);
cmem.save(flush: true);
}
创建这些类是可行的,它们存储在数据库中。我的更新和删除方法对成员和委员会非常有用。但是,当我尝试类似
//this works
def cm = CommitteeMember.get(1);
def title = cm.title;
//this does not
def firstName = cm.member.firstName;
委员会成员引用的成员字段均不可访问。我在这里做错了什么?我做了很多研究,找不到任何与此完全一样的东西。帮助将不胜感激。在这件事上拉我的头发..
**更新
在服务层方法saveCommitteeMember()
中,我添加了一行println(cmem.member.firstName);
,并且在我的 grails 启动的终端中,它确实打印了我在调用该函数时添加到委员会的成员的名字。