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.
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'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()
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.
I have 3 domain class and I need to define their relationships but I find no luck in defining them correctly. Can somebody rectify my code? I have always get the error unknown column pointing to an id. Please help. Below are my codes. Thank you.
Todo.groovy class
package todoScaff
import userScaff.User
import categoryScaff.Category
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
import todoScaff.Todo
import userScaff.User
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
import todoScaff.Todo
import categoryScaff.Category
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"
}
}
Errors
I'm fairly certain your SQL tables are incorrect.
I created a small project with your 3 domain classes and used the grails command schema-export; after removing everything that doesn't relate to the domain relationship, the structure should look like this:
create table category (
id bigint,
user_id bigint,
);
create table todo (
id bigint,
category_id bigint,
owner_id bigint,
);
create table user (
id bigint
);
If your table structure doesn't resemble this, you should either change it to match or use the static mapping = {} closure in the domain class to indicate the proper column names.
Static Mapping
In Todo:
static mapping = {
owner column: 'owner_id' // change owner_id to match your owner column within todo
category column: 'category_id' // change category_id to match your category column within todo
}
In Category:
static mapping = {
owner user: 'user_id' // change user_id to match your user column within category
}
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)