【问题标题】:I cannot println of joined table using grails我无法使用 grails 打印连接表
【发布时间】:2015-07-24 17:39:24
【问题描述】:

我有三个课程: 参与者有许多会话 会议有许多参与者 Session_plan 将 Participants 和 Sessions 放在一起

所以,我想查询 Session_plan 并获取 Sessions 及其参与者的列表,但我不明白该怎么做。我已经注释掉了 'hasMany' 和 'belongsTo' 以确保我至少从类中获取数据......而且我是,但是当我试图将它们放回并列出数据时(通过连接,我根本没有得到任何数据。我不知道我做错了什么。请指教。

package mytest
import mytest.Session_plan

class Sessions {
//static hasMany = [session_plan: Session_plan]
//static mapping = {
//session_plan indexColumn: [name: "session_id", type: String],
//  joinTable: [column: " sessio_id"]}
String session_id
String session_name
String session_tier

    static constraints = {
session_id(blank: false)
session_name(blank: false)
session_tier(blank: false)
    }
}


package mytest

class Session_plan {
//static belongsTo = Sessions
String session_id
String participant_id
Boolean authorization
Boolean step
Boolean limit
Sessions sessions
Particiipant particiipant
static embedded = ['sessions','particiipant']
static constraints = {
session_id(blank: false)
participant_id(blank: false)
authorization(blank: false)
step (blank: false)
limit(blank: false)
}
}

package mytest
class Particiipant {
//static belongsTo = Session_plan
String participant_id_type
String participant_id

    static constraints = {
participant_id_type(blank: false)
participant_id(blank: false)
    }
}


import grails.converters.JSON
import org.hibernate.Query
import org.hibernate.criterion.CriteriaSpecification
import mytest.Session_plan
import mytest.Sessions
import mytest.Participants

class BootStrap {

    def init = { servletContext ->

    // Check whether the test data already exists.
    …
    // Create data
    …

// Check to see if we have data in the tables
//def list = Sessions.executeQuery("select e from Sessions e, Session_plan ed where ed.session_id = e ")
//list.each { sessions->
//println "session_id = ${sessions.session_id}"
//}

//def results = Sessions.list()
//results.each { sessions->
//println "session_id = ${sessions.session_id}: session_name = ${sessions.session_name}; sessions_tier = ${sessions.sessions_tier}"
//}


  def list = Session_plan.list()
  results.each { session_plan->
  println "session_id = ${sessions_plan.session_id}"
  }
}
    def destroy = {
    }
}

这列出了我的数据,但是当我尝试列出“限制”等其他字段时,什么也没有打印出来。

  def list = Session_plan.list()
  results.each { session_plan->
  println "session_id = ${sessions_plan.session_id}"
  }

我已经用头撞墙两天了。您可以提供的任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: grails join


    【解决方案1】:

    limit 不是保留字吗?考虑您正在处理的问题总是好的。问一下。

    如果您想限制从数据库返回的记录数量,您的典型 sql 语句中会使用什么词:

    select * from somewhere where something like '%something%' limit 3;
    

    请注意单词限制,但您似乎正试图将限制声明为域对象。

    你的应用真的启动了吗?我的默认 HQL 不会因此而启动。

    在尝试使这项工作正常进行时,我正在查看您在域类中发生的事情,并想知道您是否误解了关系的主体,或者是否有真正的原因,例如 nosql 后端或与mysql/oracl/psql等典型关系数据库

    所以也许这个答案完全取决于未定义的“你正在为数据库运行”

    就 HQL 而言,我尝试了这个并且效果很好:

    我的带有虚拟信息的引导程序:

    import customshiro.Particiipant
    import customshiro.UserSession
    import customshiro.SessionPlan
    
    class BootStrap {
    
        def init = { servletContext ->
    
            UserSession session1 = UserSession.findOrSaveWhere(sessionId:'abc', sessionName:'firstSession', sessionTier:'tier1').save(flush:true)
            UserSession session2 = UserSession.findOrSaveWhere(sessionId:'bbc', sessionName:'secondSession', sessionTier:'tier2').save(flush:true)
            UserSession session3 = UserSession.findOrSaveWhere(sessionId:'ccc', sessionName:'thirdSession', sessionTier:'tier3').save(flush:true)
            println "--- 1 ${session1}"
            Particiipant p1 = Particiipant.findOrSaveWhere( participantIdType: 'normal', participantId:'participantId1').save(flush:true)
            Particiipant p2 = Particiipant.findOrSaveWhere( participantIdType: 'super', participantId:'participantId2').save(flush:true)
            Particiipant p3 = Particiipant.findOrSaveWhere( participantIdType: 'reg', participantId:'participantId3').save(flush:true)
            println "-- 2 $p1"
            println "---------------------------------------"
            def s1 = SessionPlan.findOrSaveWhere(authorization:true, step: true, userLimit:true, currentsession:session1, particiipant: p1 ).save(flush:true)
            def s2 = SessionPlan.findOrSaveWhere(authorization:true, step: true, userLimit:true, currentsession:session2, particiipant: p2 ).save(flush:true)
            def s3 = SessionPlan.findOrSaveWhere(authorization:true, step: true, userLimit:true, currentsession:session3, particiipant: p3 ).save(flush:true)
            println "-- 3 $s1"
    
    
        }
    

    域类会话:

    package customshiro
    
    class UserSession {
    
        static hasMany = [sessionplan: SessionPlan]
    
        String sessionId
        String sessionName
        String sessionTier
    
        static constraints =  {
            sessionId(blank: false)
            sessionName(blank: false)
            sessionTier(blank: false)
        }
    }
    

    域类SessionPlan:

    package customshiro
    
    class SessionPlan {
    
        Boolean authorization
        Boolean step
        Boolean userLimit
        Particiipant particiipant
        UserSession currentsession
        //static embedded = ['currentsession','particiipant']
        static constraints = {
            authorization(nullable: false)
            step (nullable: false)
            userLimit(nullable: false)
        }
    }
    

    域类参与者:

    package customshiro
    
    class Particiipant {
    
        static belongsTo = SessionPlan
        String participantIdType
        String participantId
    
        static constraints = {
            participantIdType(blank: false)
            participantId(blank: false)
        }
    }
    

    测试控制器:

    package customshiro
    
    class TestController {
    
        def index() { 
            def results = SessionPlan.list()
    
            results.each { sp ->
                println "sessionId = ${sp.currentsession.sessionId}"
                println "sessionName = ${sp.currentsession.sessionName}"
                println "sessionTier = ${sp.currentsession.sessionTier}"
                println "participantId = ${sp.particiipant.participantId}"
                println "participantIdType = ${sp.particiipant.participantIdType}"
    
            }
            render "check console"
        }
    }
    

    控制台输出:

    | Server running. Browse to http://localhost:8080/testrelations
    sessionId = abc
    sessionName = firstSession
    sessionTier = tier1
    participantId = participantId1
    participantIdType = normal
    sessionId = bbc
    sessionName = secondSession
    sessionTier = tier2
    participantId = participantId2
    participantIdType = super
    sessionId = ccc
    sessionName = thirdSession
    sessionTier = tier3
    participantId = participantId3
    participantIdType = reg
    

    以上关于误解的简单信息是指我拥有的域类,它在 UserSession 类中声明了 sessionId 并且 sessionPlan 类不重复这些调用,如果它们包含重复信息,为什么还会有关系?因此,对于每个 session.plan 的控制器,它会列出 currentsession.values 和参与者值。

    如果是这样的话

    SessionPlan {
    
       static hasMany = [ anotherclass: AnotherClass ]
    

    然后在你的循环中

    results.each { sp ->
       sp.anotherclass.each { nextClass ->
          //nextClass.fields here and now its looping hasMany relation 
        }
    }
    

    【讨论】:

    • 对于手机上的任何错别字,我们深表歉意。以为你有重复的字段,然后你嵌入了表格。仍然不确定为什么你需要所有这些,如果你不应该会话计划简单地扩展会话类,因此获得它的所有属性以及它的新功能。这现在开始使它成为一个不同的架构
    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 2015-12-11
    • 2018-12-26
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 2023-03-27
    相关资源
    最近更新 更多