multiple domain class with belongsTo relationship - grails

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

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

Grails many to many relationship error

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

Grails Query with parent

If I have four domain classes like this:
class Branch {
String name
static hasMany = [users:Users]
static mappedBy = [users:'branch']
static mapping = {
id column: 'f_branch_id'
name column: 'f_name'
}
}
class Users {
String name
static hasMany = [account:Account]
static mappedBy = [account:'user']
static belongsTo= [branch:Branch, title:Title]
static mapping = {
id column: 'f_user_id',
name column: 'f_name',
branch column: 'k_branch_id'
}
}
class Account {
String username
static belongsTo = [user:Users]
static mapping = {
id column: 'f_account_id'
user column: 'f_user_id'
username column: 'f_username'
}
}
class JoinTable implements Serializable {
Account account
Role role
static mapping = {
id composite : ['role', 'account']
role column :'k_role_id'
account column :'k_account_id'
version false
}
}
How can i get branch from JoinTable using criteria query
i try this process but fail for alias problem
def criteria = JoinTable.createCriteria()
def list = criteria.list {
account {
user{
branch{
eq("id", "2")
}
}
}
}
Domains
class Branch {
String name
static hasMany = [users:Users]
static mapping = {
id column: 'f_branch_id'
name column: 'f_name'
}
}
class Title {
...
}
class Users {
String name
static hasMany = [account:Account]
static belongsTo= [branch:Branch, title:Title]
static mapping = {...}
}
class Account {
String username
static belongsTo = [user:Users]
static mapping = {...}
}
class Role {
...
}
class JoinTable implements Serializable {
Account account
Role role
static mapping = {
id composite : ['role', 'account']
role column :'k_role_id'
account column :'k_account_id'
version false
}
}
Test
#TestMixin(GrailsUnitTestMixin)
#Mock([Act, Branch, Title, Users, Account, Role, JoinTable])
class EaseTests {
void testCriteria(){
10.times{
def b = new Branch().save(validate:false, flush:true)
10.times{
def u = new Users(branch:b).save(validate:false, flush:true)
10.times{
def a = new Account(user:u).save(validate:false, flush:true)
def joinTableRow = new JoinTable(account: a).save(validate:false, flush:true)
}
}
}
def c = JoinTable.createCriteria()
def results = c{
account{
user {
branch{
idEq(2l)
}
}
}
}
assert results
}
}

Restrict the rows retrieved from database for the relationship between the domain classes

I have two domain classes:
class Entity {
static hasMany = [
titles: Title
]
}
class Title {
Boolean isActive
static belongsTo = [entity:Entity]
static mapping = {
isActive type: 'yes_no'
}
}
Now when I am calling Entity.get(0) I would like to take from the database the Entity with id=0, but only with active Titles (where isActive = true). Is it possible in grails? I've tried to add where clause in static mapping of Title domain class:
static mapping = {
isActive type: 'yes_no'
where 'isActive = Y'
}
or
static mapping = {
isActive type: 'yes_no'
where 'isActive = true'
}
but it doesn't work. I am using Grails in version 2.2.1
Could You help me? Thank You in advance.
In this case you can use criteria to do that:
Entity.createCriteria().get {
eq('id', 0)
projections {
titles {
eq('isActive', true)
}
}
}
I don't think it's possible to set a default where to be applied in all your database calls to that Domain Class.
You can also wrap your logic in a service:
class EntityService {
def get(Long id) {
return Entity.createCriteria().get {
eq('id', id)
projections {
titles {
eq('isActive', true)
}
}
}
}
}

Override getter and setter in grails domain class for relation

How to override getter and setter for field being a relation one-to-many in grails domain class? I know how to override getters and setters for fields being an single Object, but I have problem with Collections. Here is my case:
I have Entity domain class, which has many titles. Now I would like to override getter for titles to get only titles with flag isActive equals true. I've tried something like that but it's not working:
class Entity {
static hasMany = [
titles: Title
]
public Set<Title> getTitles() {
if(titles == null)
return null
return titles.findAll { r -> r.isActive == true }
}
public void setTitles(Set<Title> s) {
titles = s
}
}
class Title {
Boolean isActive
static belongsTo = [entity:Entity]
static mapping = {
isActive column: 'is_active'
isActive type: 'yes_no'
}
}
Thank You for your help.
Need the reference Set<Title> titles.
class Entity {
Set<Title> titles
static hasMany = [
titles: Title
]
public Set<Title> getTitles() {
if(titles == null)
return null;
return titles.findAll { r -> r.isActive == true }
}
public void setTitles(Set<Title> s) {
titles = s
}
}

Resources