Grails问题在关联列表中列出重复的姓氏
我有一个框架式的Grails应用程序,它有两个域:Person和Course。人属于课程,课程有很多人。我已修改了课程的show.gsp,以列出与所选课程相关的所有人员 我遇到的问题是,显示给定课程中所有人员的列表不会显示数据库中姓氏重复的人员。例如,如果我有4个人:“John Doe”、“Jane Doe”、“Joe Doe”、“Edward Smith”,那么列表将只显示:Grails问题在关联列表中列出重复的姓氏,grails,groovy,gorm,Grails,Groovy,Gorm,我有一个框架式的Grails应用程序,它有两个域:Person和Course。人属于课程,课程有很多人。我已修改了课程的show.gsp,以列出与所选课程相关的所有人员 我遇到的问题是,显示给定课程中所有人员的列表不会显示数据库中姓氏重复的人员。例如,如果我有4个人:“John Doe”、“Jane Doe”、“Joe Doe”、“Edward Smith”,那么列表将只显示: 无名氏 爱德华·史密斯 然而,这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
}
}
<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>
课程域:
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
}
}
<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>
修改了show.gsp课程:
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
}
}
<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>
#
${counter}
${p?.encodeAsHTML()}
您使用了一个分类数据集进行关联,Person的比较将两个姓氏相同的人视为相同的人,因此第二个和后续的姓氏相同的人不会首先添加到集合中。很好的捕获,谢谢。你认为最好的解决方法是什么?我遇到了许多问题,无法正确地对关联进行排序,因此我当前的实现@littleK说实话,我真的不知道。在我的应用程序中,我总是在排序无关紧要时使用普通集(如果我需要在gsp中以特定顺序显示结果,则使用代码排序),或者在排序无关紧要时使用列表。无论如何,谢谢。虽然我知道这对性能不利,但我只想在show.gsp中对列表进行排序。我想不出别的办法了!您可以将“compareTo”函数更改为首先检查姓氏,但如果响应为0(表示相等),则返回姓氏的比较。注意,这仍然是一件坏事。。。compareTo应该检查ID,因为如果两个人的名字相同,你会希望他们出现两次。您认为sort调用是正确的,您可以将其改为NamedQuery,以使其更加干燥