0
class Candidate  {
    String username;
    static HasMany=[applications:Application]
}

class Vote {
   String name;
   Date firstdate;
   Date enddate ;
   static HAsMany=[applications:Application]
}



  class Application {
        Date datedemand;
        Candidate candidate; 
        Vote vote;
   static belongsTo = [candidate:Candidate,vote:Vote]
    }

我想显示投票列表,如果我点击投票,它会显示该投票的候选人列表。
我开始了以下尝试,但我仍然被阻止:

def candidatsGrpByVte(){
    def results = Application.withCriteria {
    createAlias("vote", "vote")
        projections {
            groupProperty("vote.name") 
        }
    }
}

    def candidatesByVName(def vname){
        def results= Application.createCriteria().list() {
            createAlias("candidate","candidatAlias")
            createAlias("vote","voteAlias")
            eq('voteAlias.name',vname)
            projections {
               property('candidatAlias.username')
            }
        }
        return results;
    }

我想在 Application 的投票中看到候选人。
我如何在视图中显示。
你能给我个主意吗?

4

1 回答 1

0

映射看起来比应有的复杂。Vote与您没有直接关系,Candidate并且您希望按投票列出候选人。

唯一Vote相关的方法Candidate是通过Application。所以你必须抓住ApplicationaVote并显示所有的Candidates Application

//Lazily:-
def voteCandidature = [:]
Vote.list()?.each{vote->
    voteCandidature << [(vote.name) : vote.application?.candidate?.asList()]
}

   //Eagerly:-
   def candidatesByVote(){
        def results = Application.createCriteria().list{
           vote{

           }
           candidate{

           }
        }
    }

    results.each{application->
       def votes = application.vote
       def candidates = application.candidate
       //Lost after this
       //You have bunch of votes and bunch of candidates
       //but both belong to the same application
    }

Vote实际上意味着什么?你能详细说明每个域类使用的主要是VoteApplication吗?为什么不使用复数形式hasMany

于 2013-05-25T03:05:23.600 回答