Quantcast

Does createAlias behaves consistently in all situations

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Does createAlias behaves consistently in all situations

Ravi Pratap
I wrote a very simple criteria query on UserRole Domain Object(created by spring-security-core) and am baffled with sql output

def userRole=UserRole.withCriteria{

createAlias ('user', 'usr')

eq('role',Role.findByAuthority('ROLE_ADMIN'))

eq("usr.username","admin1")

}

The generate Sql  doesn't create a join with user table and gives error as Unknown column 'usr1_.username' in 'where clause'.

SQL Log:  "select this_.role_id as role1_0_0_, this_.user_id as user2_0_0_ from user_role this_ where this_.role_id=? and usr1_.username=?"

Am I missing something??? 

PS : UserRole the default generated Serializable  Domain Class generated by spring-security-core. It is a mapping class Between User and Role Domain Class and has user and role as 2 fields.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Does createAlias behaves consistently in all situations

michael astreiko
Re: [grails-user] Does createAlias behaves consistently in all situations

Hello Ravi,


Do not understand the reason as well but maybe try this:


def userRole=UserRole.withCriteria{

  eq('role',Role.findByAuthority('ROLE_ADMIN'))

  user {

      eq("username","admin1")

  }

}


Thursday, March 1, 2012, 1:49:24 PM, you wrote:



I wrote a very simple criteria query on UserRole Domain Object(created by spring-security-core) and am baffled with sql output

def userRole=UserRole.withCriteria{

createAlias ('user', 'usr')

eq('role',Role.findByAuthority('ROLE_ADMIN'))

eq("usr.username","admin1")

}

The generate Sql  doesn't create a join with user table and gives error as Unknown column 'usr1_.username' in 'where clause'.

SQL Log:  "select this_.role_id as role1_0_0_, this_.user_id as user2_0_0_ from user_role this_ where this_.role_id=? and usr1_.username=?"

Am I missing something??? 

PS : UserRole the default generated Serializable  Domain Class generated by spring-security-core. It is a mapping class Between User and Role Domain Class and has user and role as 2 fields.




-- 

Best regards,

Michael Astreiko

--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Does createAlias behaves consistently in all situations

Ravi Pratap
Michael, Thanks!  I tried your solution but got exactly the same error. Between, when i started with grails (1.3.7), this looked as the most intuitive syntax to traverse the object hierarchy but it did not use to work. That is when i figured that this syntax is only valid for associations and createAlias is the way one can traverse the object hierarchy.

The SQL log for your suggestion: "select this_.role_id as role1_0_0_, this_.user_id as user2_0_0_ from user_role this_ where this_.role_id=? and usr1_.username=?"


On Thu, Mar 1, 2012 at 5:19 PM, michael astreiko [via Grails] <[hidden email]> wrote:

Hello Ravi,


Do not understand the reason as well but maybe try this:


def userRole=UserRole.withCriteria{

  eq('role',Role.findByAuthority('ROLE_ADMIN'))

  user {

      eq("username","admin1")

  }

}


Thursday, March 1, 2012, 1:49:24 PM, you wrote:



I wrote a very simple criteria query on UserRole Domain Object(created by spring-security-core) and am baffled with sql output

def userRole=UserRole.withCriteria{

createAlias ('user', 'usr')

eq('role',Role.findByAuthority('ROLE_ADMIN'))

eq("usr.username","admin1")

}

The generate Sql  doesn't create a join with user table and gives error as Unknown column 'usr1_.username' in 'where clause'.

SQL Log:  "select this_.role_id as role1_0_0_, this_.user_id as user2_0_0_ from user_role this_ where this_.role_id=? and usr1_.username=?"

Am I missing something??? 

PS : UserRole the default generated Serializable  Domain Class generated by spring-security-core. It is a mapping class Between User and Role Domain Class and has user and role as 2 fields.




-- 

Best regards,

Michael Astreiko

--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email


If you reply to this email, your message will be added to the discussion below:
http://grails.1312388.n4.nabble.com/Does-createAlias-behaves-consistently-in-all-situations-tp4434645p4434744.html
To unsubscribe from Grails, click here.
NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Does createAlias behaves consistently in all situations

Ravi Pratap
In reply to this post by michael astreiko
The cause for this  behavior is UserRole has a compositeId and is serializable (id composite: ['role', 'user']). If I remove this, createAlias works fine. 

It will be enlightening to know why? or if it is a bug, I can raise a JIRA.


On Thu, Mar 1, 2012 at 6:06 PM, Ravi Pratap <[hidden email]> wrote:
Michael, Thanks!  I tried your solution but got exactly the same error. Between, when i started with grails (1.3.7), this looked as the most intuitive syntax to traverse the object hierarchy but it did not use to work. That is when i figured that this syntax is only valid for associations and createAlias is the way one can traverse the object hierarchy.

The SQL log for your suggestion: "select this_.role_id as role1_0_0_, this_.user_id as user2_0_0_ from user_role this_ where this_.role_id=? and usr1_.username=?"


On Thu, Mar 1, 2012 at 5:19 PM, michael astreiko [via Grails] <[hidden email]> wrote:

Hello Ravi,


Do not understand the reason as well but maybe try this:


def userRole=UserRole.withCriteria{

  eq('role',Role.findByAuthority('ROLE_ADMIN'))

  user {

      eq("username","admin1")

  }

}


Thursday, March 1, 2012, 1:49:24 PM, you wrote:



I wrote a very simple criteria query on UserRole Domain Object(created by spring-security-core) and am baffled with sql output

def userRole=UserRole.withCriteria{

createAlias ('user', 'usr')

eq('role',Role.findByAuthority('ROLE_ADMIN'))

eq("usr.username","admin1")

}

The generate Sql  doesn't create a join with user table and gives error as Unknown column 'usr1_.username' in 'where clause'.

SQL Log:  "select this_.role_id as role1_0_0_, this_.user_id as user2_0_0_ from user_role this_ where this_.role_id=? and usr1_.username=?"

Am I missing something??? 

PS : UserRole the default generated Serializable  Domain Class generated by spring-security-core. It is a mapping class Between User and Role Domain Class and has user and role as 2 fields.




-- 

Best regards,

Michael Astreiko

--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email


If you reply to this email, your message will be added to the discussion below:
http://grails.1312388.n4.nabble.com/Does-createAlias-behaves-consistently-in-all-situations-tp4434645p4434744.html
To unsubscribe from Grails, click here.
NAML


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Does createAlias behaves consistently in all situations

michael astreiko
In reply to this post by Ravi Pratap
Re: [grails-user] Re: Does createAlias behaves consistently in all situations

Hello Ravi,


And what is content of yours UserRole domain?


Thursday, March 1, 2012, 3:36:29 PM, you wrote:



Michael, Thanks! В I tried your solution but got exactly the same error. Between, when i started with grails (1.3.7), this looked as the most intuitive syntax to traverse the object hierarchy but it did not use to work. That is when i figured that this syntax is only valid for associations and createAlias is the way one can traverse the objectВ hierarchy.


The SQL log for your suggestion: "select this_.role_id as role1_0_0_, this_.user_id as user2_0_0_ from user_role this_ where this_.role_id=? and usr1_.username=?"




On Thu, Mar 1, 2012 at 5:19 PM, michael astreiko [via Grails] <[hidden email]> wrote:


Hello Ravi,


Do not understand the reason as well but maybe try this:


def userRole=UserRole.withCriteria{

В  eq('role',Role.findByAuthority('ROLE_ADMIN'))

В  user {

В  В  В  eq("username","admin1")

В  }

}


Thursday, March 1, 2012, 1:49:24 PM, you wrote:



I wrote a very simple criteria query on UserRole Domain Object(created by spring-security-core) and am baffled with sql output

def userRole=UserRole.withCriteria{

createAlias ('user', 'usr')

eq('role',Role.findByAuthority('ROLE_ADMIN'))

eq("usr.username","admin1")

}

The generate Sql В doesn't create a join with user table and givesВ error asВ Unknown column 'usr1_.username' in 'where clause'.

SQL Log: В "select this_.role_id as role1_0_0_, this_.user_id as user2_0_0_ from user_role this_ where this_.role_id=? and usr1_.username=?"

Am I missing something???В 

PS : UserRole the default generated Serializable В Domain Class generated by spring-security-core. It is a mapping class Between User and Role Domain Class and has user and role as 2 fields.




--В 

Best regards,

Michael Astreiko

--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email 




If you reply to this email, your message will be added to the discussion below:

http://grails.1312388.n4.nabble.com/Does-createAlias-behaves-consistently-in-all-situations-tp4434645p4434744.html 

To unsubscribe from Grails, click here.

NAML 




View this message in context: Re: Does createAlias behaves consistently in all situations

Sent from the Grails - user mailing list archive at Nabble.com.




-- 

Best regards,

Michael Astreiko

--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Does createAlias behaves consistently in all situations

Ravi Pratap
it is the default generated by spring-security-core

class UserRole implements Serializable {


User user

Role role


boolean equals(other) {

if (!(other instanceof UserRole)) {

return false

}


other.user?.id == user?.id &&

other.role?.id == role?.id

}


int hashCode() {

def builder = new HashCodeBuilder()

if (user) builder.append(user.id)

if (role) builder.append(role.id)

builder.toHashCode()

}


static UserRole get(long userId, long roleId) {

find 'from UserRole where user.id=:userId and role.id=:roleId',

[userId: userId, roleId: roleId]

}


static UserRole create(User user, Role role, boolean flush = false) {

new UserRole(user: user, role: role).save(flush: flush, insert: true)

}


static boolean remove(User user, Role role, boolean flush = false) {

UserRole instance = UserRole.findByUserAndRole(user, role)

if (!instance) {

return false

}


instance.delete(flush: flush)

true

}


static void removeAll(User user) {

executeUpdate 'DELETE FROM UserRole WHERE user=:user', [user: user]

}


static void removeAll(Role role) {

executeUpdate 'DELETE FROM UserRole WHERE role=:role', [role: role]

}


static mapping = {

id composite: ['role', 'user']

version false

}

}


On Thu, Mar 1, 2012 at 6:37 PM, michael astreiko [via Grails] <[hidden email]> wrote:

Hello Ravi,


And what is content of yours UserRole domain?


Thursday, March 1, 2012, 3:36:29 PM, you wrote:



Michael, Thanks! В I tried your solution but got exactly the same error. Between, when i started with grails (1.3.7), this looked as the most intuitive syntax to traverse the object hierarchy but it did not use to work. That is when i figured that this syntax is only valid for associations and createAlias is the way one can traverse the objectВ hierarchy.


The SQL log for your suggestion: "select this_.role_id as role1_0_0_, this_.user_id as user2_0_0_ from user_role this_ where this_.role_id=? and usr1_.username=?"




On Thu, Mar 1, 2012 at 5:19 PM, michael astreiko [via Grails] <[hidden email]> wrote:


Hello Ravi,


Do not understand the reason as well but maybe try this:


def userRole=UserRole.withCriteria{

В  eq('role',Role.findByAuthority('ROLE_ADMIN'))

В  user {

В  В  В  eq("username","admin1")

В  }

}


Thursday, March 1, 2012, 1:49:24 PM, you wrote:



I wrote a very simple criteria query on UserRole Domain Object(created by spring-security-core) and am baffled with sql output

def userRole=UserRole.withCriteria{

createAlias ('user', 'usr')

eq('role',Role.findByAuthority('ROLE_ADMIN'))

eq("usr.username","admin1")

}

The generate Sql В doesn't create a join with user table and givesВ error asВ Unknown column 'usr1_.username' in 'where clause'.

SQL Log: В "select this_.role_id as role1_0_0_, this_.user_id as user2_0_0_ from user_role this_ where this_.role_id=? and usr1_.username=?"

Am I missing something???В 

PS : UserRole the default generated Serializable В Domain Class generated by spring-security-core. It is a mapping class Between User and Role Domain Class and has user and role as 2 fields.




--В 

Best regards,

Michael Astreiko

--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email 




If you reply to this email, your message will be added to the discussion below:

http://grails.1312388.n4.nabble.com/Does-createAlias-behaves-consistently-in-all-situations-tp4434645p4434744.html 

To unsubscribe from Grails, click here.

NAML 




View this message in context: Re: Does createAlias behaves consistently in all situations

Sent from the Grails - user mailing list archive at Nabble.com.




-- 

Best regards,

Michael Astreiko

--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email


If you reply to this email, your message will be added to the discussion below:
http://grails.1312388.n4.nabble.com/Does-createAlias-behaves-consistently-in-all-situations-tp4434645p4434906.html
To unsubscribe from Grails, click here.
NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Does createAlias behaves consistently in all situations

Jonathan Andrew Ong
Ravi,

Have you solved this issue? I also need to filter by role, and I'm encountering the same issue.

Regards,
Jonathan

On Fri, Mar 2, 2012 at 12:04 PM, Ravi Pratap <[hidden email]> wrote:
it is the default generated by spring-security-core

class UserRole implements Serializable {


User user

Role role


boolean equals(other) {

if (!(other instanceof UserRole)) {

return false

}


other.user?.id == user?.id &&

other.role?.id == role?.id

}


int hashCode() {

def builder = new HashCodeBuilder()

if (user) builder.append(user.id)

if (role) builder.append(role.id)

builder.toHashCode()

}


static UserRole get(long userId, long roleId) {

find 'from UserRole where user.id=:userId and role.id=:roleId',

[userId: userId, roleId: roleId]

}


static UserRole create(User user, Role role, boolean flush = false) {

new UserRole(user: user, role: role).save(flush: flush, insert: true)

}


static boolean remove(User user, Role role, boolean flush = false) {

UserRole instance = UserRole.findByUserAndRole(user, role)

if (!instance) {

return false

}


instance.delete(flush: flush)

true

}


static void removeAll(User user) {

executeUpdate 'DELETE FROM UserRole WHERE user=:user', [user: user]

}


static void removeAll(Role role) {

executeUpdate 'DELETE FROM UserRole WHERE role=:role', [role: role]

}


static mapping = {

id composite: ['role', 'user']

version false

}

}


On Thu, Mar 1, 2012 at 6:37 PM, michael astreiko [via Grails] <[hidden email]> wrote:

Hello Ravi,


And what is content of yours UserRole domain?


Thursday, March 1, 2012, 3:36:29 PM, you wrote:



Michael, Thanks! В I tried your solution but got exactly the same error. Between, when i started with grails (1.3.7), this looked as the most intuitive syntax to traverse the object hierarchy but it did not use to work. That is when i figured that this syntax is only valid for associations and createAlias is the way one can traverse the objectВ hierarchy.


The SQL log for your suggestion: "select this_.role_id as role1_0_0_, this_.user_id as user2_0_0_ from user_role this_ where this_.role_id=? and usr1_.username=?"




On Thu, Mar 1, 2012 at 5:19 PM, michael astreiko [via Grails] <[hidden email]> wrote:


Hello Ravi,


Do not understand the reason as well but maybe try this:


def userRole=UserRole.withCriteria{

В  eq('role',Role.findByAuthority('ROLE_ADMIN'))

В  user {

В  В  В  eq("username","admin1")

В  }

}


Thursday, March 1, 2012, 1:49:24 PM, you wrote:



I wrote a very simple criteria query on UserRole Domain Object(created by spring-security-core) and am baffled with sql output

def userRole=UserRole.withCriteria{

createAlias ('user', 'usr')

eq('role',Role.findByAuthority('ROLE_ADMIN'))

eq("usr.username","admin1")

}

The generate Sql В doesn't create a join with user table and givesВ error asВ Unknown column 'usr1_.username' in 'where clause'.

SQL Log: В "select this_.role_id as role1_0_0_, this_.user_id as user2_0_0_ from user_role this_ where this_.role_id=? and usr1_.username=?"

Am I missing something???В 

PS : UserRole the default generated Serializable В Domain Class generated by spring-security-core. It is a mapping class Between User and Role Domain Class and has user and role as 2 fields.




--В 

Best regards,

Michael Astreiko

--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email 




If you reply to this email, your message will be added to the discussion below:

http://grails.1312388.n4.nabble.com/Does-createAlias-behaves-consistently-in-all-situations-tp4434645p4434744.html 

To unsubscribe from Grails, click here.

NAML 




View this message in context: Re: Does createAlias behaves consistently in all situations

Sent from the Grails - user mailing list archive at Nabble.com.




-- 

Best regards,

Michael Astreiko

--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email


If you reply to this email, your message will be added to the discussion below:
http://grails.1312388.n4.nabble.com/Does-createAlias-behaves-consistently-in-all-situations-tp4434645p4434906.html
To unsubscribe from Grails, click here.
NAML


Loading...