Grails问题列出了相关列表中的重复姓氏

时间:2013-02-17 19:40:11

标签: grails groovy gorm

我有一个带有Grails的脚手架应用程序,它有两个域,Person和Course。人属于课程,课程有很多人。我修改了show.gsp for Course以列出与所选课程相关联的所有人员。

我遇到的问题是,显示给定课程中所有人员的列表不会显示数据库中具有重复姓氏的人员。例如,如果我有4个人:“John Doe”,“Jane Doe”,“Joe Doe”,“Edward Smith”,那么列表只会显示:

  • Jane Doe
  • 爱德华史密斯

然而,所有4个人都在数据库中。此外,/ person / list将显示所有名称。所以问题只出在与课程相关的人员名单上。请参阅下面的相关代码。有什么想法吗?

人员网域:

class Person implements Comparable {

    static mapping = { sort lastName: "asc" }

    // Needed to sort association in Course domain (due to Grails bug)
    int compareTo(obj) {
        lastName.compareToIgnoreCase( obj.lastName );
    }

    String firstName
    String lastName
    String email

    Course course

    static belongsTo = [ course:Course ]

    static constraints = {
        firstName size: 1..50, blank: false
        lastName size: 1..50, blank: false
        email email: true
        course()
        firstName(unique: ['lastName', 'email'])
    }

    String toString() {
        return this.lastName + ", " + this.firstName;
    }
}

课程范围:

class Course {

    int maxAttendance
    SortedSet persons

    static hasMany = [ persons:Person ]

    static mapping = {
        persons cascade:"all-delete-orphan"
    }

    def getExpandablePersonList() {
        return LazyList.decorate(persons,FactoryUtils.instantiateFactory(Person.class))
    }

    static constraints = {
        maxAttendance size: 1..3, blank: false
    }
}

修改了课程的show.gsp:

<g:if test="${courseInstance?.persons}">
                <br />
                <table>
                    <thead>
                        <tr>
                            <th>#</th>
                            <g:sortableColumn property="person"
                                title="${message(code: 'person.lastName.label', default: 'Person')}" />

                        </tr>
                    </thead>
                    <tbody>
                        <g:set var="counter" value="${1}" />
                        <g:each in="${courseInstance.persons}" status="i" var="p">
                            <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
                                <td>
                                    ${counter}
                                </td>
                                <td class="property-value" aria-labelledby="persons-label"><g:link
                                        controller="person" action="show" id="${p.id}">
                                        ${p?.encodeAsHTML()}</td>

                            </tr>
                            <g:set var="counter" value="${counter + 1}" />
                        </g:each>
                    </tbody>
                </table>
            </g:if>

1 个答案:

答案 0 :(得分:2)

您已使用SortedSet进行关联,而Person compareTo认为具有相同姓氏的两个人相同,因此具有相同姓氏的第二个和后续个人将不会添加到设置在第一位。