Grails集合中的第一个对象在每个循环中不更新
我正在Grails中实现一个简单的聊天系统,作为现有应用程序的一部分Grails集合中的第一个对象在每个循环中不更新,grails,collections,gorm,Grails,Collections,Gorm,我正在Grails中实现一个简单的聊天系统,作为现有应用程序的一部分 主要考虑的类为: User.groovy class User { ... static hasMany = [ ... chatMessages : ChatMessage, conversationParticipations:ConversationParticipation ] static constraints = { ... } } 聊天室 c
主要考虑的类为:
User.groovyclass User {
...
static hasMany = [
...
chatMessages : ChatMessage,
conversationParticipations:ConversationParticipation
]
static constraints = {
...
}
}
聊天室
class ChatConversation {
static hasMany = [
conversationParticipations:ConversationParticipation,
chatMessages:ChatMessage
]
static constraints = {
}
}
groovy—一个中间类,用于删除用户和聊天室会话之间的多个会话
class ConversationParticipation {
ChatMessageBuffer chatMessageBuffer
static constraints = {
chatMessageBuffer nullable : true
}
static belongsTo = [
user:User,
chatConversation:ChatConversation
]
}
ChatMessageBuffer.groovy-用于保存聊天信息,会话参与者尚未阅读
class ChatMessageBuffer {
static hasMany = [
chatMessages : ChatMessage
]
static belongsTo = [
conversationParticipation:ConversationParticipation
]
static constraints = {
chatMessages nullable : true
conversationParticipation nullable : true
}
}
在一个服务中,我调用了一些方法来创建一个对话,然后将任何发送的消息发送到ChatMessageBuffers,以便进行类似这样的对话
def createChatConversation(chatDetails)
{
def chatConversation = new ChatConversation()
chatConversation.save(flush:true, failOnError:true)
new ConversationParticipation(
user:getCurrentUser(),
chatConversation:chatConversation,
chatMessageBuffer:new ChatMessageBuffer()
).save(flush:true, failOnError:true)
new ConversationParticipation(
user:User.get(chatDetails.id),
chatConversation:chatConversation,
chatMessageBuffer: new ChatMessageBuffer()
).save(flush:true, failOnError:true)
return chatConversation
}
def sendMessage(chatMessageDetails)
{
//save the message
def chatMessage = new ChatMessage(
body:chatMessageDetails.chatMessage,
dateSent: new Date(),
user:getCurrentUser(),
chatConversation:ChatConversation.get(chatMessageDetails.chatConversationId)
).save(flush:true,failOnError : true)
//add the message to the message buffer for each participant of the conversation.
ConversationParticipation.findAllByChatConversation(
ChatConversation.get(chatMessageDetails.chatConversationId)
).each {
if(it.chatMessageBuffer.addToChatMessages(chatMessage).save(flush:true, failOnError:true))
{
println"adding to ${it.chatMessageBuffer.id}"
println"added to : ${it.chatMessageBuffer.dump()}"
}
}
def chatMessageBuffer = ChatMessageBuffer.get(1)
println"service : message buffer ${chatMessageBuffer.id}: ${chatMessageBuffer.dump()}"
return chatMessage
}
正如您在创建ConversationParticipation
对象时所看到的,我还创建了一个ChatMessageBuffer
,当我在新建ConversationParticipation
上调用save时,它是级联save
我的问题是,当我将ChatMessage
s添加到两个ChatMessageBuffer
s时,第一个ChatMessageBuffer
没有保存,但第二个是。因此,当我向相同的缓冲区添加另一个ChatMessage
时,第一个缓冲区为空,但第二个缓冲区包含先前添加的ChatMessage
有人知道我哪里出错了吗?为什么第一个缓冲区不会保存/更新?尝试显式声明缓冲区并将其强类型化。如果你还没有,至少可以帮你找出问题所在!