Grails:hasMany不工作......我认为我的hibernate会话出错了

时间:2012-02-07 03:36:06

标签: hibernate session grails gorm

我有一个新的grails 1.3.7,有两个域类,有一些棘手的关系:

class NodePoint {
  String name
  static mappedBy=[outgoingConnections:'startPoint',incomingConnections:'endPoint']
  static hasMany=[outgoingConnections:Connections, incomingConnections:Connections]
}

class Connections {
  NodePoint startPoint
  NodePoint endPoint

}

我在引导程序中做错了(项目是“今天的问题”):

import todaysstupidproblem.*
class BootStrap {

  def init = { servletContext ->
    def startingPoint = new NodePoint(name:"This Point").save(failOnError:true)
    def endingPoint = new NodePoint(name:"That Point").save(failOnError:true)
    def someConnex = new Connections(startPoint:startingPoint,endPoint:endingPoint).save(failOnError:true, flush:true)
    println someConnex
    println "WHY ISNT THERE SOMETHING BETWEEN THESE???"
    startingPoint.outgoingConnections.each{
      println "WHY AM I NOT SEEING THIS!!?!?!?!?"
      println "Where did the outgoingConnections go?"
      println it
    }
    println "HIBERNATE FTL :("
  }
  def destroy = {
  }
}

打印:

Running Grails application..
todaysstupidproblem.Connections : 1
WHY ISNT THERE SOMETHING BETWEEN THESE???
HIBERNATE FTL :(
Server running. Browse to http://localhost:8080/todaysstupidproblem

为什么不打印连接?

2 个答案:

答案 0 :(得分:1)

这是因为startingPoint.outgoingconnections为null并且未初始化为任何内容。虽然someConnex引用了startingPoint和endingPoint,但后向引用尚未初始化。尝试在每个循环之前添加它,

startingPoint.addToOutgoingConnections someConnex

答案 1 :(得分:0)

是的,这是会议。我在这一行中添加了:

NodePoint.withSession { it.clear() }

然后用NodePoint.list().find{it}重新加载startPoint,它就在那里!

特别感谢@BurtBeckwith将此解决方案隐藏在对我的另一个问题的评论中!