Grails/GORM simple many-to-many join query not working? - grails

OK, I'm new to Grails, as well as Hibernate. I'm prototying something simple, and am stuck on querying the simplest many-to-many relationship via a join.
My model objects are:
class User {
static hasMany = [roles:Role]
String firstName
String lastName
String username
String password
// ... constraints and hooks omitted ...
}
class Role {
static hasMany = [users:User]
static belongsTo = User
String name;
// ... constraints and hooks omitted ...
}
After loading some data, I can see:
groovy:000> User.list().each { user-> println "$user.username : ${user.roles.collect {it.name}}"}
smendola : [Admin, Reviewer]
jripper : []
jbauer : []
groovy:000> Role.list().each { role-> println "$role.name: ${role.users?.collect {it.username}}"}
Admin: [smendola]
Guest: null
Reviewer: [smendola]
So, user smendola has two roles; other users have no roles; and the relationship is working from both directions. Good.
Now the question:
I want to query for user with some role. Of course I could use the return value from either of the above two queries and search it in Groovy, but I want the db to do this work.
I have futzed for HOURS trying to to construct a query that will give me the desired result, to no avail. I believe I have followed online examples to a tee, and yet I cannot get this query to work.
One version of the query I've tried:
groovy:000> User.where { roles.name == 'Admin' }.list()
===> []
Or this variant:
groovy:000> User.where { roles {name == 'Admin'}}.list()
===> []
I've tried many, many other variations, including using .id, or role=someRoleInstance, etc. Nothing works. I'm out of ideas. Any help out there?
The database is h2, by the way.
Grails version 2.0.0
Thanks!
ADDED:
Two variants that were suggested, but also did not work:
groovy:000> User.createCriteria().list{ roles { eq('name', 'Admin') } }
===> []
groovy:000>
groovy:000> roleName = 'Admin'
===> Admin
groovy:000> def users = User.withCriteria {
groovy:001> roles {
groovy:002> eq('name', roleName)
groovy:003> }
groovy:004> }
===> []
groovy:000>

User.createCriteria().list{
roles{
eq('name', 'Admin')
}
}
Try using criteria

This should work if you're willing to use a criteria query instead:
String roleName = 'Admin'
def users = User.withCriteria {
roles {
eq('name', roleName)
}
}

Related

Grails 2.4.4 removing all items from a hasmany String relationship

I have a User class that hasMany organizations, and organizations are String UIDs.
class User implements Serializable {
...
List organizations
static hasMany = [organizations: String]
...
}
To update an User instance, I need to remove all the Strings from "organizations" before adding what the updated uids, but it doesn't work. The same organizations are resaved into the collection after I delete them and save the User instance.
I tried many ways:
// 1
user.organizations.clear()
// 2
def orgsToDelete = [] // this is to avoid concurrent modification exception
orgsToDelete += user.organizations
orgsToDelete.each { orguid ->
user.removeFromOrganizations(orguid)
}
After any of 1 or 2, the user.organizations is empty, but when I do:
user.save(flush:true)
I get the same organizations that where before the clear/removeFromOrganizations
I can't do user.organizations.each { it.delete() } because the items are not domain classes but Strings.
Another weird thing is I have a custom validator to check if the collection has any items, and it seems it doesn't gets any errors, event the organizations is empty, this is my validator:
organizations validator: { val, obj ->
if (obj.organizations.size() == 0) return false
return true
}
When I do this on the controller update action, it says hasErrors() == false
if (userInstance.hasErrors())
{
println "has errors"
respond userInstance.errors, view:'edit'
return
}
Any ideas?
Here is the controller: https://github.com/ppazos/cabolabs-ehrserver/blob/master/grails-app/controllers/com/cabolabs/security/UserController.groovy#L199-L237
Use your own domain class instead of String as collection's elements, like:
class Organization {
String name
static belongsTo = [ User ]
}
then you should be able to properly remove the children by
user.organisations.clear()
user.save()
The reason for the behaviour you a facing is, that the String instances are mapped to a table which has no back-refs to the user. That leads to the situation, that the records in that string table are not actually removed and are loaded upon the next database call.
I have read your project code and you have the DataSource bad set. You are using a H2 memory BBDD, but using the MySQL driver instead (in the development environment). It doesnt look good. Set it properly, and try again.
I have test your model, and used:
user.organizations.clear()
without any problems, even using your custom validator.
In other hand you should use a Service instead for all the business logic, not a controller. Because a service is transactional by default.
Here are the changes I did, and works:
// List organizations = []
static hasMany = [organizations: String]
static mapping = {
password column: '`password`'
// organizations lazy: false
}
Removing List organizations. Using the H2 datasource with this config at DataSource development environment:
dbCreate = "create-drop"
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
username = "sa"
password = ""
And this code works like a charm:
def clearOrganizationFromUser(){
//creating user with timestamp
User user = new User(username: 'username' + new Date(), password: 'password', email: 'email#email.es', accountExpired: false, accountLocked: false,
passwordExpired: false)
List<String> organizations = []
10.times{
organizations << "organization $it"
}
organizations.each{ String organization ->
user.addToOrganizations(organization)
}
user.save()
println "Organizations after saving: ${user.organizations}"
//Its not neccesary but I did it for showing that organization are persisted in DDBB
User newUserFromDDBB = User.get(user.id)
//deleting organization
newUserFromDDBB.organizations.clear()
newUserFromDDBB.save()
println "---> Organizations after deleting: ${newUserFromDDBB.organizations}"
}
Hope It works. If do, please mark as solved.

Grails Criteria - list contents

These are my domain objects
User{
hasMany = {roles: UserRole}
}
UserRole{
User user
Role role
}
Role{
String authority
}
I need to find users based on their Role. For that I am trying to use the following criteria:
def role_admin = Role.findByAuthority('ROLE_ADMIN')
def criteria = new DetachedCriteria(User).build {
roles{
role{
idEq(role_admin.id)
}
}
}
result.users = criteria.list(params)
result.total = criteria.count()
The above will always return one result, even though I have verified by looking at the database directly that there should be more results. The params passed to list are correct, but I tried removing them just to be sure. I can't see what is wrong with the above, any suggestions ?
I also tried this
roles{
role{
eq("authority","ROLE_ADMIN")
}
}
But it is throwing exception:
Unknown column 'role_alias2_.authority' in 'where clause'
this works for me:
def criteriaUsers = UserRole.createCriteria()
def users = criteriaUsers.listDistinct{
eq("role", role_admin)
}.user
Side note: from the nomenclature of your classes it looks like you are using spring-security-core plugin. In my humble opinion the hasMany = [roles: UserRole] is redundant as the association User - Role is already being modeled in the UserRole class.

Grails findBy belongsTo gives JdbcSQLException

Say, for a system, I had Student and User objects, like so:
User {
}
Student {
User user //not nullable
static belongsTo = [User]
}
Teacher {
static belongsTo = [user:User]
}
I have a lot of code written against these models, and plenty of dynamic finders in it
def u1 = new User().save( flush: true)
new Student( user: u1 ).save( flush: true)
...
def s = Student.findByUser( springSecurityService.currentUser)
//s can be null
def t = Teacher.findByUser( springSecurityService.currentUser)
//t can be null
if( s != null)
//do something with 's'
if( t != null)
//do something with 't'
...
And this all worked fine. But I wanted to update my domain model to add cascading access from User to Student. So I did:
User {
Student student //nullable
}
And I would save a user like this:
new User( student: new Student() ).save( flush: true)
But the problem is, when I try to access 's' like I did above.
If the Student.findByUser can find one such student, then it works.
But if it should return null, it gives an error instead.
def s = Student.findByUser( springSecurityService.currentUser)
will result in:
org.h2.jdbc.JdbcSQLException
Parameter #2 is not set; SQL statement:
select this_.id as id18_0_, this_.version as version18_0_, from student this_ where this_.id=? limit ? [90012-173]
I know I can access the student object through the user object like user.student, but I would like to be able to keep the existing code as it is.
I've seen this thread, and it had a similar problem, but no resolution grails email list
Is this a bug? What can I do to get around it? Am I not setting up my domain models properly?
Thanks
As an alternative, you could try something like this using hasOne instead of belongsTo.
class User {
static hasOne = [student:Student, teacher:Teacher]
static constraints = {
student unique:true, nullable: true
teacher unique:true, nullable: true
}
}
class Student {
User user
}
class Teacher {
User user
}
It still cascades and appears to handle null searches fine.

Spring Data Neo4j : find all nodes with a list of propertyvalues

I have a neo4j social network db and have a usecase to look through a bunch of user ids and check how many user ids are present in the graph. The User looks like this :
#NodeEntity
public class User {
#GraphId
Long nodeId;
#Indexed(indexName = "uid",unique = true)
Long uid;
}
my check would look somrthing like this :
for(Long uid : allUserIds){
User friend = userRepo.findByPropertyValue("uid", uid);
if(friend!=null){
//Create a relationship
}else{
//continue
}
}
Is there a way I can get rid of the findByPropertyValue for every single userId ? Is there a faster way thru which I can get all existing Users given a bunch of uids in one request ?
Thanks..
You could try with a Cypher query:
Map<String, Object> params = new HashMap<String, Object>();
String query = "start user=node:__types__(className=\"<package>.User\") where ID(user)>=0 and ID(user) in {uids} return user"; // substitute <package> with the full package name
params.put("uids", allUserIds); // allUserIds should be a Collection<Long>
Collection<User> users = neo4jOperations.query(query.toString(), params).to(User.class).as(Collection.class);
for (User user: users) {
...
}
You're already doing it right.
There is also findByQuery afaik, that allows you to pass in a lucene query which would be "uid: value1 value2 value3"

Grails Web flow with Multiple domain

I am new to Grails development. I started doing the web flow for the user registration. It has two forms that in first form i get the basic details and get the bank details in second one. And both of them going to save in different domains.
class Customer {
String Name
Integer Age
Date DateOfBirth
String FatherOrHusbandName
String IdProof
String IdProofNumber
Boolean IsDeleted
static hasOne = [bankdetail:BankDetails]
static hasMany = [property:Property]
static constraints = {
}}
Bank details
class BankDetails {
String bank_name
String account_number
String account_holder_name
String branch_name
String IFSC_code
String account_type
Customer customer
static constraints = {
customer unique:true
}
static mapping = {
customer insertable: false
customer updateable: false
}}
These two domain classes are used for the customer. I got the nice tutorial for web flow implementation from http://ridingthetiger.wikia.com/wiki/Creating_the_Example_Grails_WebFlow_Project. But in this tutorial only one domain is used. I want the grails webflow example with two or more domain classes. Please suggest if a have any or give one simple example....
Thanks in advance
Basically, what I do is create two command objects and pass them around through next and previous. One for each step. But in this case the domain objects are simple you can just pass them around in the flow object. e.g
def createCustomerFlow = {
basicInfo{
on('next'){Customer cust ->
flow.customer = cust
if(!cust.validate()){
flow.errorbean = cust
return error()
}else{
return success()
}
}.to 'bankInfo'
on("cancel").to 'cancel'
}
bankInfo{
on('finish'){BankDetails bank ->
flow.bank = bank
if(!bank.validate()){
flow.errorbean = bank
return error()
}else{
try{
//create customer. Implement this method yourself
def cust = createCustomer(flow.customer, flow.bank)
flow.customer = cust
}catch(ValidationException e){
log.error("Validation exception", e)
flow.errorbean = e
return error()
}
return success()
}
}.to 'summary'
on("previous"){BankDetails bank ->
flow.bank = bank
}.to 'basicInfo'
on("cancel").to 'cancel'
}
}

Resources