Where adding pessimistic lock in using hasMany? - grails

Suppose I have this code
Domain
class Parent{
static hasMany = [Childs:Child]
}
class Child {
String name
}
Controller
def ParentInstance = Parent.get(1) //Parent.lock(1)
ParentInstance.Childs.each {
//it.lock()
it.name = "titi"
}
ParentInstance.save()
The lock should be added for each child instance or just adding it when getting the parent?

Related

Abstract domain class and tablePerHierarchy

In my Grails 3.2.6 app I have 2 classes:
abstract class Base {
static mapping = {
tablePerHierarchy false
}
}
and
class Child extends Base {
static mapping = {
collection 'child'
}
}
Upon saving the instances of Child are dumped into "base" collection (with _class = Child field) instead of "child".
How to make it work right?
UPDATE
I defined the Base as a trait under src/main/groovy:
trait Base { }
and
class Child implements Base { }
then it worked properly.
In your Child class mapping method, add this
table "child"

grails - calling overriden getter in domain class

I'm using Grails 2.4.4. I've got domain class
class Purchase {
static hasMany = [dataList:DataByPeriod]
String name
Set<DataByPeriod> dataList
public Set<DataByPeriod> getDataList(){
dataList?.findAll { (!it.isDeleted) }
}
def getPeriodList(){
this.dataList?.period?.unique()?.sort{it.name}
}
}
and another two:
class DataByPeriod {
static belongsTo = [purchase:Purchase, period:Period]
Long value
}
class Period {
static hasMany = [dataList:DataByPeriod]
String name
}
Now I want to receive all periods for given purchaseInstance. If I do:
purchaseInstance?.dataList?.period?.unique()?.sort{it.name}
I'll get correct data (getter for dataList will be called and all records with isDeleted==true will be ignored
But if I I do:
purchaseInstance?.getPeriodList()
getter will not be called and all records will be shown?
Why does this happens? Why I even cannot change my 'getPeriodList()` method to:
def getPeriodList(){
this.getDataList()?.period?.unique()?.sort{it.name}
}
It says that 'there is no method getDataList() for class Purchase'.

Can we have relation like - Grails hasMany-belongsTo and instance variable

Class A
{
static constraints = {
bObj(nullable:true)
}
B bObj
static hasMany = [listB:B]
}
Class B
{
static constraints = {
aObj(nullable:true)
}
A aObj
}
Problem that Im facing is, when I retrive instance of A(whereas bObj.aObj is null) & call save on it, automatically adds reference of A into bObj.aObj, which was null before calling save().
Any thoughts on why would it happen.

grails 2.2.2 service don't know domain model: "No signature of method"

I have a grails-plugin called "listadmin" there is a domain model "Liste":
package listadmin
class Liste {
String internal_name
String public_name
Boolean edtiable = true
Boolean visible = true
static hasMany = [eintrage : ListenEintrag]
static constraints = {
internal_name(unique : true , blank : false);
}
String toString() {
"${public_name}"
}
}
I have service called "SECO_ListenService" in the same module (grails-plugin):
package listadmin
class SECO_ListenService {
def getEntriesOfList(String intnalListName) {
def aList = Liste.findByInternal_name(intnalListName)
return aList
}
}
Now I try to call this service from an other module (grails-plugin) called "institutionadmin". The SECO_ListenService should return a list of strings for an select of a domain model in the inistitutionadmin:
package institutionadmin
import listadmin.SECO_ListenService
class Einrichtung {
Long einrichtungs_type
Long type_of_conzept
int anzahl_gruppen
int anzahl_kinder_pro_Gruppe
String offnungszeiten
static hasMany = [rooms : Raum]
static constraints = {
def aList = []
def sECO_ListenService = new SECO_ListenService()
aList=sECO_ListenService.getEntriesOfList("einrichtung_type")
einrichtungs_type(inList: aList)
}
}
If I try to run this application with the both modules. I get the following error:
Caused by MissingMethodException: No signature of method:
listadmin.Liste.methodMissing() is applicable for argument types: ()
values: []
It seemed to be that the service class don't know the "Liste"-domain-model. But I don't know where the error is. I also tried to call other standard methods like "findAll" but without any success.
Has anybody an idea where my mistake could be?
To get a service in a static context you need to access the grailsApplication spring bean. This can be done thought Holders. Example:
class MyService {
List<String> getAvailable() {
return ['A','B','C']
}
}
class MyDomainClass {
String something
static constraints = {
something inList: getSomethingList()
}
static List<String> getSomethingList() {
def myService = Holders.grailsApplication.mainContext.getBean('myService')
return myService.getAvailable()
}
}

Grails first object in a collection not updating whilst in an each loop

I am implementing a simple chat system in Grails as part of an existing application.
The main classes to consider are:
User.groovy
class User {
...
static hasMany = [
...
chatMessages : ChatMessage,
conversationParticipations:ConversationParticipation
]
static constraints = {
...
}
}
ChatConversation.groovy
class ChatConversation {
static hasMany = [
conversationParticipations:ConversationParticipation,
chatMessages:ChatMessage
]
static constraints = {
}
}
ConversationParticipation.groovy - the intermediate class to remove the many-many between User and ChatConversation
class ConversationParticipation {
ChatMessageBuffer chatMessageBuffer
static constraints = {
chatMessageBuffer nullable : true
}
static belongsTo = [
user:User,
chatConversation:ChatConversation
]
}
ChatMessageBuffer.groovy - used to hold chat messages, not yet read by a Conversation Participant
class ChatMessageBuffer {
static hasMany = [
chatMessages : ChatMessage
]
static belongsTo = [
conversationParticipation:ConversationParticipation
]
static constraints = {
chatMessages nullable : true
conversationParticipation nullable : true
}
}
In a service I am calling methods to create a conversation and then to send any sent messages to the ChatMessageBuffers for that conversation like this
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
}
As you can see on creating a ConversationParticipation object, I am also creating a ChatMessageBuffer, which is cascading save when I call save on the new ConversationParticipation.
My problem is when I am adding the ChatMessages to the two ChatMessageBuffers, the first ChatMessageBuffer is not saving, but the second is. So when I go to add another ChatMessage to the same buffers, the first buffer is empty, but the second buffer contains the previously added ChatMessage(s).
Does anybody have any idea where I am going wrong? Why the first one will not save/update?

Resources