Grails many to many relationship error - grails

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]
}

Related

Proper Criteria for Shiro plugin in Grails

I have Shiro domain classes as below:
class ShiroUser {
String email
String password
static hasMany = [ roles: ShiroRole, permissions: String ]
static constraints = {
email(nullable: false, blank: false, unique: true)
}
}
class ShiroRole {
String name
static hasMany = [ users: ShiroUser, permissions: String ]
static belongsTo = ShiroUser
static constraints = {
name(nullable: false, blank: false, unique: true)
}
}
I received ShiroUser's email from params.email. And I want to find out the permission that belongsTo ShiroUser using criteria().
I tried the below code, but couldn't succeed.
def criteria= permissions.createCriteria().listDistinct {
ShiroRole{
ShiroUser{
eq("email", params.email)
}
}
}
Your criteria is wrongly built. I'd keep it simple and put like:
def permissions = ShiroUser.findByEmail( params.email )?.roles*.permissions.flatten() as Set
If you want to stick with criteria:
def permissions = ShiroRole.createCriteria().listDistinct {
projections{
property 'permissions'
}
users{
eq "email", params.email
}
}

[B cannot be cast to java.sql.Blob

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'
}

grails multiple table criteria

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.

multiple domain class with belongsTo relationship

I have 3 classes as per below...
Class A
{
E objE;
}
Class B
{
E objE;
}
Class E
{
E objE;
belongsTo:[
a : A,
b : B
]
static constraints = {
a nullable: true
b nullable: true
c nullable: true
}
}
When I am trying to save object of A class it through exception for null.
Try to remove the hasOne on Class Incidents, Problems, Requests and replace it with
E eObj
static constraints = {eObj: unique: true, nullable:true}
static mapping = {
eObj cascade: "delete"
}

While Generating controller and view getting error : Can not set int field lms.Book.bookId to java.lang.Class

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).

Resources