gpt4 book ai didi

grails - 愿意支付$$我无法使用Grails打印联接表

转载 作者:行者123 更新时间:2023-12-02 16:00:30 24 4
gpt4 key购买 nike

我有三节课:
参加者有很多场
session 有很多参与者
Session_plan将参与者和 session 放在一起

因此,我想查询Session_plan并获取 session 及其参与者的列表,但我不知道该怎么做。我已经注释掉“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}"
}

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

最佳答案

不限制保留字吗?最好考虑一下您要处理的问题。我们去问问吧。

如果要限制从数据库返回的记录数量,那么典型的sql语句中将使用一个单词:

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

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

您的应用程序实际上启动了吗?由于这个原因,默认HQL上的Mine无法启动。

在尝试使此工作正常进行时,我正在查看您在域类中所做的事情,并想知道您是否误解了关系的本质,或者是否存在真正的原因,例如nosql后端或不同于典型关系数据库的东西例如mysql / oracl / psql等。

因此,也许所有答案都取决于未定义的含义,即“您正在为数据库运行”

就HQL而言,我尝试过此方法,并且有效:

我的 bootstrap 带有虚拟信息:
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"


}

DomainClass session :
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)
}
}

Realm 类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)
}
}

DomainClass参与者:
package customshiro

class Particiipant {

static belongsTo = SessionPlan
String participantIdType
String participantId

static constraints = {
participantIdType(blank: false)
participantId(blank: false)
}
}

TestController:
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的 Controller ,它然后列出currentsession.values和参与者值。

如果是这样的话
SessionPlan {

static hasMany = [ anotherclass: AnotherClass ]

然后在你的循环
results.each { sp ->
sp.anotherclass.each { nextClass ->
//nextClass.fields here and now its looping hasMany relation
}
}

关于grails - 愿意支付$$我无法使用Grails打印联接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31616603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com