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.
Related
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.
Good day to all. I am very new in using grails and I have followed several tutorials for beginners using grails until I come up in creating domain relationships. However, I got stuck with this problem right now. I have 3 domain classes namely, todo, category and user. And as I defined their relationships, it returns me an error saying unable to resolve class. Please see my codes below. Please help. Thank you so much.
Todo.groovy Class
package todoScaff
class Todo {
String name
String note
Date createDate
Date dueDate
Date completedDate
String priority
String status
User owner
Category category
static belongsTo = [User, Category]
static constraints = {
name(blank:false)
createDate()
priority()
status()
note(maxsize:1000, nullable:true)
completedDate(nullable:true)
dueDate(nullable:true)
}
String toString() {
name
}
}
Category.groovy Class
package categoryScaff
class Category {
String name
String description
User user
static belongsTo = User
static hasMany = [todos: Todo]
static constraints = {
name(blank:false)
}
String toString(){
name
}
}
User.groovy Class
package userScaff
class User {
String userName
String fname
String lname
static hasMany = [todos: Todo, categories: Category]
static constraints = {
userName(blank:false, unique:true)
fname(blank:false)
lname(blank:false)
}
String toString(){
"$lname, $fname"
}
}
Since you've placed your domain classes in different packages, you must import the classes at the head of the file.
package categoryScaff
import todoScaff.Todo
import userScaff.User
class Category {
The same needs to happen in your other domain classes that reference classes outside the current package.
Okay, so I understand how to organize the fields in grails without using the gsp pages by writing the fields in the constraints like so.
Class User{
String firstName;
String nickName;
static constraints = {}
}
this will make first name appear before nick name in the default scaffolding because f comes before n in the alphabet.
Class User{
String firstName;
String nickName;
static constraints = {
nickName()
firstName()
}
}
this makes nick name appear before first name in the CRUD model in scaffolding. It's the order you name the constraints.
Now, how do you make the relations appear in a specific order? For example if I had this
Class User{
String firstName;
String nickName;
static belongsTo = {company:Company}
static constraints = {
}
}
How would I rearrange this order? would it be done in constraints? I know it can be done in gsp page, but how would I do it here?
I'm pretty sure it's the same as regular fields...
i.e. if you want the order of the fields to appear as nickName, firstName, company you'd do this:
Class User {
String firstName
String nickName
static belongsTo = {company: Company}
static constraints = {
nickName()
firstName()
company()
}
}
I'm having an issue when using a find by on a domain class:
The error I'm getting is:
ERROR util.JDBCExceptionReporter - No value specified for parameter 2
The domain classes are:
class AppDetail {
String name
String url
Boolean active
static hasMany = [authString:AppIdentifier]
static constraints = {
name(unique:true,blank:false)
active(nullable:true)
}
class AppIdentifier {
Date added
String authString
static belongsTo = [authString:AppDetail]
static constraints = {
added()
authString(blank:false)
}
The find by is:
def identifier = AppIdentifer.findByAuthString('test')
def appDetails = AppDetail.findByAuthString(identifier)
Can anyone provide any insight into the meaning of this error?
Thanks in advance!
You have too many fields named "authString". Here's another way to redo the same classes:
class AppDetail {
String name
String url
Boolean active
static hasMany = [identifiers:AppIdentifier]
static constraints = {
name(unique:true,blank:false)
active(nullable:true)
}
class AppIdentifier {
Date added
String authString
static belongsTo = [appDetail:AppDetail]
static constraints = {
added()
authString(blank:false)
}
def identifier = AppIdentifer.findByAuthString('test')
def appDetails = identifier.appDetail
You defined authString both as String AND AppDetail:
String authString
static belongsTo = [authString:AppDetail]
Either remove String authString or change it to AppDetail authString
BTW From the point of view of GORM the property naming doesn't matter. But if you define a property with String in it's name, but of another class, you gonna get maintenance problems in the future.
I use hasOne for one-to-one relation in Grails:
class MyParent {
static hasOne = [child: MyChild]
}
class MyChild {
static belongsTo = [parent: MyParent]
static mapping = {
table: 'MyChild'
}
}
I have table in the DB named "MyChild" and thus I get the next error:
Invalid object name 'my_child'
How can I specify relation's table name in Parent class to be "MyChild" and not "my_child"?
Try it without ':'.
static mapping = { table "mychild"}
or use name label
static mapping = { table name:"mychild" }
Hope this helps