This is my domain class
class TimesheetSubmission {
Date submissionDate=new Date()
String foreman
String shift
String jobId
Date date
byte[] xmlSubmission
String xmlResponse
static constraints = {
submissionDate nullable: false
foreman nullable: false
shift nullable: false
jobId nullable: false
date nullable: false
xmlSubmission nullable: true
xmlResponse nullable: false
}
static mapping = {
xmlSubmission (type: "blob")
}
}
And following is my code to persist data in database.
TimesheetSubmission timesheetSubmission=new TimesheetSubmission()
timesheetSubmission.foreman=Party.findById(foremanId)
timesheetSubmission.shift=shift
timesheetSubmission.jobId=jobId
timesheetSubmission.date=Date.parse("yyyy-MM-dd", date)
timesheetSubmission.xmlSubmission=sTimesheet.getBytes();
timesheetSubmission.xmlResponse="response"
timesheetSubmission.save(flush: true,failOnError: true)
I am getting following error when applying save on domain .
[B cannot be cast to java.sql.Blob
static mapping = {
xmlSubmission sqlType: 'blob'
}
Related
LoginController.groovy
package nvts.sample
import grails.converters.*
class LoginController {
static LoginService loginService
def index() {
render "Sample"
}
def getall() {
def user = User.list()
render user as JSON
}
}
The first method called index() is rendering string value but getall() method not rendering JSON value.
Red error line in def user = User.list()
URI
/sample/login/index
Class
groovy.lang.MissingPropertyException
Message
No such property: User for class: nvts.sample.LoginController
User is my domain class which is created by reverse engineer:
User.groovy
package sample
class User {
String emailAddress
String userPasswd
String payrollId
String posLoginId
String firstName
String lastName
String designation
String dept
String loginName
String nric
Date dateofbirth
String phyAddr1
String phyAddr2
String phyAddr3
String phyCity
String phyState
String phyCountryCode
String phyPostalCode
String postalAddr1
String postalAddr2
String postalAddr3
String postalCity
String postalState
String postalCountryCode
String postalPostalCode
String telephone
String handphone
String altEmail
String createdBy
Date dateCreated
String updatedBy
Date dateUpdated
String userMenu
String emailAddress_1
String altEmail_1
String createdBy_1
Date dateCreated_1
Date dateUpdated_1
String firstName_1
String lastName_1
String loginName_1
String payrollId_1
String phyAddr1_1
String phyAddr2_1
String phyAddr3_1
String phyCity_1
String phyCountryCode_1
String phyPostalCode_1
String phyState_1
String posLoginId_1
String postalAddr1_1
String postalAddr2_1
String postalAddr3_1
String postalCity_1
String postalCountryCode_1
String postalPostalCode_1
String postalState_1
String updatedBy_1
String userMenu_1
String userPasswd_1
static mapping = {
id name: "emailAddress", generator: "assigned"
version false
}
static constraints = {
emailAddress maxSize: 45
userPasswd maxSize: 45
payrollId nullable: true, maxSize: 20
posLoginId nullable: true, maxSize: 10
firstName maxSize: 45
lastName nullable: true, maxSize: 45
designation nullable: true, maxSize: 50
dept nullable: true, maxSize: 50
loginName nullable: true, maxSize: 45
nric nullable: true, maxSize: 10
dateofbirth nullable: true
phyAddr1 nullable: true, maxSize: 50
phyAddr2 nullable: true, maxSize: 50
phyAddr3 nullable: true, maxSize: 50
phyCity nullable: true, maxSize: 50
phyState nullable: true, maxSize: 50
phyCountryCode nullable: true, maxSize: 10
phyPostalCode nullable: true, maxSize: 10
postalAddr1 nullable: true, maxSize: 50
postalAddr2 nullable: true, maxSize: 50
postalAddr3 nullable: true, maxSize: 50
postalCity nullable: true, maxSize: 50
postalState nullable: true, maxSize: 50
postalCountryCode nullable: true, maxSize: 10
postalPostalCode nullable: true, maxSize: 10
telephone nullable: true, maxSize: 20
handphone nullable: true, maxSize: 20
altEmail nullable: true, maxSize: 50
createdBy nullable: true, maxSize: 45
dateCreated nullable: true
updatedBy nullable: true, maxSize: 45
dateUpdated nullable: true
userMenu nullable: true
emailAddress_1 maxSize: 45
altEmail_1 nullable: true, maxSize: 50
createdBy_1 nullable: true, maxSize: 45
dateCreated_1 nullable: true
dateUpdated_1 nullable: true
firstName_1 maxSize: 45
lastName_1 nullable: true, maxSize: 45
loginName_1 nullable: true, maxSize: 45
payrollId_1 nullable: true, maxSize: 20
phyAddr1_1 nullable: true, maxSize: 50
phyAddr2_1 nullable: true, maxSize: 50
phyAddr3_1 nullable: true, maxSize: 50
phyCity_1 nullable: true, maxSize: 50
phyCountryCode_1 nullable: true, maxSize: 10
phyPostalCode_1 nullable: true, maxSize: 10
phyState_1 nullable: true, maxSize: 50
posLoginId_1 nullable: true, maxSize: 10
postalAddr1_1 nullable: true, maxSize: 50
postalAddr2_1 nullable: true, maxSize: 50
postalAddr3_1 nullable: true, maxSize: 50
postalCity_1 nullable: true, maxSize: 50
postalCountryCode_1 nullable: true, maxSize: 10
postalPostalCode_1 nullable: true, maxSize: 10
postalState_1 nullable: true, maxSize: 50
updatedBy_1 nullable: true, maxSize: 45
userMenu_1 nullable: true
userPasswd_1 maxSize: 45
}
}
I think you are missing an import of the User.groovy class in your LoginController.groovy class?
import sample.User
My application works for everything except the call to display members of different boards. I can get the correct output in the db with an SQL query but having issues trying it in Grails using createCriteria.
Have to use Oracle 11g as my DB.
Grails 2.3.3
both the DB and Grails are local.
Here are my domains
class Trustee {
String salutation
String firstName
String middleName
String lastName
static hasMany = [board:Boards, membership:TrusteeMembership]
static constraints = {
salutation nullable: true
firstName nullable: true
middleName nullable: true
lastName nullable: true
}
//map to the existing DB table
static mapping = {
table 'BOT_TRUSTEE'
id column:'TRUSTEE_ID'
salutation column: 'SALUTATION'
firstName column: 'FIRST_NAME'
middleName column: 'MIDDLE_INITIAL'
lastName column: 'LAST_NAME'
version false
}
}
class Boards {
String boardName
static belongsTo = [trustee:Trustee, hospital:Hospitals]
static constraints = {
boardName nullable:true
}
static mapping = {
table name:"BOT_BOARD"
id column:'BOARD_ID'
trustee column:'TRUSTEE_ID'
hospital column:'HOSPITAL_ID'
boardName column:'BOARD'
version false
}
}
class Hospitals {
String hospitalName
static hasMany = [committees:Committees, board:Boards]
static constraints = {
hospitalName nullable:true
}
static mapping = {
table 'BOT_HOSPITAL'
id column:'HOSPITAL_ID'
hospitalName column:'HOSPITAL'
version false
}
}
class Committees {
String committeeName
String description
static belongsTo = [hospital: Hospitals]
static hasMany = [membership:TrusteeMembership]
static constraints = {
committeeName nullable:true
description nullable:true
}
static mapping = {
table 'BOT_COMMITTEE'
id column:'COMMITTEE_ID'
hospital column:'HOSPITAL_ID'
committeeName column:'COMMITTEE'
description column:'DESCRIPTION'
version false
}
}
class TrusteeMembership implements Serializable{
String position
String type
static belongsTo = [trustee:Trustee, committees:Committees]//
static constraints = {
position nullable:true
type nullable:true
}
static mapping = {
table 'BOT_TRUSTEE_COMMITTEES'
version false
id composite: ['trustee','committees']
trustee column:'TRUSTEE_ID'
committees column: 'COMMITTEE_ID'
position column:'POSITION'
type column:'TYPE'
}
Here is my controller
def members(){
def letter = params.letter
def commId = params.committee
params.max = Math.min(params.max ? params.int('max'): 15, 100)
def indexSearch = Trustee.createCriteria().list(params){
//search by First letter of lastName
if(letter != null){
ilike("lastName", "${letter}%")
}
//search by lastName
if(params.lastName){
ilike("lastName", "%${params.lastName}%")
}
//search by firstName
if(params.firstName){
ilike("firstName", "%${params.firstName}%")
}
//search by boardName
if(params.boardId){
//display only members within a board id
board{
eq("id", "%${params.boardId}%")
}
}
order("lastName", "asc")
}
respond Hospitals.list(params), model:[hospitalsInstanceCount: Hospitals.count(),
trusteeInstanceList : indexSearch]
}
//search by boardName
if(params.boardId){
//display only members with the boardName
board{
eq("id", params.long('boardId'))
}
}
I ended up with this for the correct result.
I get a problem due to my poor knowledge on GORM and modeling domain object in Grails.
Here is my issue :
| Error Error loading plugin manager:
No owner defined between domain classes [class com.myproject.model.Project] and
[class com.crowdfun.Sector] in a many-to-many relationship.
Example: static belongsTo = com.myproject.model.Sector
(Use --stacktrace to see the full trace)
I can't say what is wrong, because I follow the tutorial of official grails documentations : http://grails.org/doc/latest/guide/GORM.html#manyToMany
My classes :
Project.groovy :
class Project {
String name
Integer nbInvestors
Region region
Integer nbDays
Boolean success
String equity
String currency
Double target
Double raisedAmount
String url
Double valuation
boolean extended = false
static belongsTo = [
site: Site,
sector: Sector
]
static hasMany = [
sectors: Sector
]
static hasOne = [
valuationRange: ValuationRange,
targetRange: TargetRange
]
static constraints = {
name nullable: true
nbInvestors nullable: true
region nullable: true
nbDays nullable: true
success nullable: true
equity nullable: true
currency nullable: true
target nullable: true
raisedAmount nullable: true
url nullable: true, unique: true
valuation nullable: true
}
}
Sector.groovy :
class Sector {
String name
static hasMany = [
projects: Project
]
static constraints = {
name unique: true
}
#Override
public String toString() {
return name
}
def getNbProjects() {
projects.size()
}
}
Site.groovy
class Site {
String name
static hasMany = [
projects: Project
]
static constraints = {
name unique: true
}
#Override
public String toString() {
return name
}
}
Change the class like so:
class Project {
...
Site site
Sector sector
static belongsTo = [Site, Sector]
}
While generating controller and view for a Domain class as:
class Book {
static constraints = {
bookId blank:false
bookTitle blank:false
}
private int bookId
private String bookTitle
private String author
private double price
private Date edition
private String publisher
}
Giving Error saying :
Can not set int field lms.Book.bookId to java.lang.Class
I think if u add 'private' to field declaration, u have to write getter and setter for this field:
class Book {
static constraints = {
bookId blank:false
bookTitle blank:false
}
private Integer bookId
...
Integer getBookId() { this.bookId }
void setBookId(Integer bookId) { this.bookId = bookId }
....
}
Change "int" to "Integer" (and "double" to "Double" too), e.g.
class Book {
static constraints = {
bookId blank:false
bookTitle blank:false
}
private Integer bookId
private String bookTitle
private String author
private Double price
private Date edition
private String publisher
}
Also, I doubt whether you can have a "blank" constraint on an Integer, change it to:
bookId nullable: false
assuming that is what you want (or remove it altogether, as the nullable: false constraint is implicit).
I have this:
class Usuario {
String username
String password
String passwordDos
String nombre
String apellidoPaterno
String apellidoMaterno
Date fechaDeNacimiento
String sexo
String correo
static constraints = {
username blank: false, unique: true, validator: { val, obj ->
obj.password != val
return ['usuario.userPassError']
}
password blank: false, validator: { val, obj ->
obj.passwordDos == val
return ['usuario.passDiferentes']
}
passwordDos blank: false
nombre blank: false, maxSize: 64
apellidoPaterno blank: false, maxSize: 64
apellidoMaterno blank: true, maxSize: 64
sexo inList: ["Femenino", "Masculino"]
correo blank: false, maxSize: 128, email:true
}
}
I want to return in the error message, but I'm not doing wrong, I could explain alguein please?
I'd expect there to be some sort of conditional return in the validator closures. As it stands, it looks like they'll always fail, returning the error code.
Try writing your custom validators like:
// username validator
validator: { val, obj ->
obj.password == val ? 'userPassError' : true
}
// password validator
validator: { val, obj ->
obj.passwordDos != val ? 'passDiferentes' : true
}
Note the different message codes that are being returned, too.
Then, make sure you have the following in your appropriate grails-app/i18n/messages* file(s):
usuario.username.userPassError = Username and password cannot be the same
usuario.password.passDiferentes = Password does not match password confirmation