when I use the bellow code see the exception Found two representations of same collection.
how can fix this problem?
class ProductModel implements Serializable{
static auditable = true
String name
Product product
Guarantee guarantee
String status
Integer width
Integer height
Integer weight
static hasMany = [variationValues: VariationValue, prices: Price]
static composites = ["prices"]
static mapping = {
sort 'id'
prices cascade: "all-delete-orphan"
searchKeys type: 'text'
}
in ProductModelController I have this code for delete()
def delete() {
def pricemodel = Price.findAllByProductModel(productModelInstance).findAll()
pricemodel.each { price ->
productModelInstance.removeFromPrices(price)
}
productModelInstance.save()
pricemodel.each {
it.delete()
}
productModelInstance.delete(flush: true)
Related
I have this class:
class Word {
String word
static hasMany = [group: WordGroup]
static belongsTo = [language: Language]
static constraints = {
word(unique: ['language'], maxSize:255)
}
}
Which is working fine, but I would need to associate the unique constraint to both language and group, so different groups can insert the same "word".
The groups are in an intermediate table (WordGroup) as this is a many to many relationship.
If I try to do
word(unique: ['language', 'group'], maxSize:255)
I get the error
Missing IN or OUT parameter in index:: 3
Is there a way to do this?
EDIT: I am adding the other classes referenced in the Word class if it helps
WordGroup
class WordGroup {
static belongsTo = [word: Word, group: Group]
static constraints = {}
}
Group
class Group {
String name
String url
static hasMany = [words: Word]
static constraints = {
name blank: false, unique: true
}
}
Language
class Language {
String name
String code
static constraints = {
name maxSize:255
code maxSize:255
}
}
Thanks.
I have 2 tables named 'employees' and 'dataentry'. I need to set the employee_id column in employees table as primary key, and also employee_id column in dataentry as a foreign key of employees.employee_id. How can I do this in groovy domain class.
here is my employees.groovy model class
package com.standout.utilityapplication
import java.util.Date;
import com.standout.utilityapplication.Dataentry
class Employees {
static mapping = {
table 'employees'
version false
}
static hasMany = [employee_id:Dataentry]
String employee_id
String employee_name
String team
Long contact_no
String designation
static constraints = {
employee_id(nullable: false,maxSize:10)
employee_name(nullable: false,maxSize:100)
team(nullable: true,maxSize:40)
contact_no(nullable: true,maxSize:10)
designation(nullable: true,maxSize:40)
}
}
and here is my dataentry.groovy model class
package com.standout.utilityapplication
import com.standout.utilityapplication.Employees
class Dataentry {
static mapping = {
table 'dataentry'
version false
}
static belongsTo = [employee_id:Employees]
String employee_id
String team
Date receipt_dt
String restaurant_name
int number_of_persons
float amount
Date bill_submitted_dt
String reimbursed
char presented_bank_fl
Date presented_bank_dt
String create_id
Date create_dt
String mod_id
Date mod_dt
static constraints = {
reimbursed(nullable: true)
presented_bank_fl(nullable: true)
presented_bank_dt(nullable: true)
mod_id(nullable: true)
mod_dt(nullable: true)
}
}
Please tell me how to make a mapping with cascaded
Something akin to this should get the entities joined. Convention is to name an entity in the singular, not a plural (Employee, not Employees):
class Employee {
static hasMany = [dataentries:Dataentry]
static mapping = {
id name:'employee_id'
}
}
class Dataentry {
static belongsTo = [employee:Employee]
}
Let the system take care of the details.
I'm having a weird error caused by a many-to-many relationship between two domain classes. The classes are below. When I try to save the NoteParagraph object, I DON'T immediately get an error:
NoteParagraph np = new NoteParagraph(number: noteNumber, content: noteText)
note.addToChildParagraphs(np).save()
However, on the next .save() action (on an unrelated object), I get the following error stacktrace:
Field 'note_paragraph_id' doesn't have a default value. Stacktrace follows:
java.sql.SQLException: Field 'note_paragraph_id' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2113)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2049)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2034)
at org.grails.datastore.gorm.GormStaticApi$_methodMissing_closure2.doCall(GormStaticApi.groovy:102)
Here are my classes:
class NoteParagraph {
String number
String content
Note parentNote
Date dateCreated
Date lastUpdated
static belongsTo = Note
static hasMany = [
childNotes: Note
]
static constraints = {
}
static mapping = {
content type:'text'
}
}
and the second one:
class Note {
Integer noteType
Integer parentType
Integer parentId
String heading
Date dateCreated
Date lastUpdated
static hasMany = [
childParagraphs: NoteParagraph
]
static constraints = {
}
static mapping = {
heading type:'text'
}
}
I've tried deleting and recreating my database as some other threads have suggested, and that did not help.
Fix:
class NoteParagraph {
String number
String content
Date dateCreated
Date lastUpdated
static belongsTo = [parentNote: Note]
static hasMany = [
childNotes: Note
]
static constraints = {
}
static mapping = {
content type:'text'
}
}
When you try to add NoteParagraph to Note, NoteParagraph instance is not persistent (it is created but not saved in db yet).
Try to change:
NoteParagraph np = new NoteParagraph(number: noteNumber, content: noteText)
note.addToChildParagraphs(np).save()
to:
note.save(flush: true) //add also this, if note is not persisted
NoteParagraph np = new NoteParagraph(number: noteNumber, content: noteText, parentNote: note).save(flush: true)
note.addToChildParagraphs(np).save()
Given
class Store {
String name
static hasMany = [departments: Department]
}
class Department {
String name
static belongsTo = [store: Store]
static hasMany = [products: Product]
}
class Product {
String name
Integer qty
static namedQueries = {
productsInStockByStore {store->
department {
store {
eq 'id', store.id
}
}
gt 'qty', 0
}
}
static belongsTo = [department: Department]
}
I get an error running:
def store = Store.first() // just for the sake of testing
Product.productsInStockByStore(store).list()
No signature of method: namedboom.Store.call() is applicable for
argument types:
(namedboom.Product$__clinit__closure1$_closure3$_closure4$_closure5)
values:
[namedboom.Product$__clinit__closure1$_closure3$_closure4$_closure5#768aab6a]
Possible solutions: wait(), last(), save(), any(), getAll(),
wait(long)
What would be the right way to accomplish this with named query? The only way I could get it to work is using createCriteria and declaring joints for the parent tables.
This is may be because store is defined in your named query. Try changing name of this variable in the named query, like
static namedQueries = {
productsInStockByStore {storeInstance->
department {
store {
eq 'id', storeInstance.id
}
}
gt 'qty', 0
}
}
in Grails, Is there a way to limit the size of the column to which the enum is mapped. In the following example, i would like the column type to be char(2)
enum FooStatus {
BAR('br'), TAR('tr')
final static String id
}
class Foo {
FooStatus status
static constraints = {
status(inList:FooStatus.values()*.id,size:2..2)
}
}
both inList and size do not have any effect when exporting the schema, the column type keeps its default value (varch(255))
Maybe i could do that if i define a new UserType. Any idea ?
Thank you
-ken
I don't think it's directly possible given the way enums are mapped internally in GORM. But changing the code to this works:
enum FooStatus {
BAR('br'),
TAR('tr')
private FooStatus(String id) { this.id = id }
final String id
static FooStatus byId(String id) {
values().find { it.id == id }
}
}
and
class Foo {
String status
FooStatus getFooStatus() { status ? FooStatus.byId(status) : null }
void setFooStatus(FooStatus fooStatus) { status = fooStatus.id }
static transients = ['fooStatus']
static constraints = {
status inList: FooStatus.values()*.id
}
static mapping = {
status sqlType: 'char(2)'
}
}
Adding the transient getter and setter allows you to set or get either the String (id) or enum value.
Grails ships with an undocumented (as far as I can tell anyway) custom Hibernate mapping for enums. The class is org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType. It won't let you set the column size but does make it easy to change what is stored in the DB for each enum value without having to add transient fields to your model.
import org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType
class MyDomainClass {
Status status
static mapping = {
status(type: IdentityEnumType)
}
enum Status {
FOO("F"), BAR("B")
String id
Status(String id) { this.id = id }
}
}
You can run an 'alter table' in Bootstrap.groovy to shrink the column:
DataSource dataSource
...
Sql sql = new Sql(dataSource)
sql.execute("alter table my_domain_class change column status status varchar(1) not null")
Even easier (works at least in Grails 2.1.0+)
class DomainClass {
Status status
static mapping = {
status(enumType: "string")
}
}
enum Status {
OPEN ("OPEN"),
CLOSED ("CLOSED"),
...
String name
Status (String name) {
this.name = name
}
}
Since GORM 6.1 identity enum mapping can be enabled with such construct
static mapping = {
myEnum enumType:"identity"
}