I am new to grails framework how to query 3 tables having association between them
Class A{
static hasMany = [b:B]
}
enter code here
class B{
long aId // Id of table A
}
class c{
B b //B reference
}
SQL query: select * from C where b_id in (select id from B where a_id='10'_)
Any help will be appreciated.
Straight-forward
def list = C.withCriteria{
b{
eq 'aId', '10'
}
}
Related
I have the following domain classes:
class A {
hasMany = [bs : B]
}
class B { }
Note that B has no backward relation to a. GORM creates a join table in my MYSQL database a_b. This table has two columns the id of a and the id of b.
How can I get a dateCreatedin the join table?
Create a model of the join table yourself and add whatever properties you want on it. Simple, and done.
For example:
class A {
static hasMany = [bs: JoinB]
}
class JoinB {
static belongsTo = [a: A]
B b
Date dateCreated
static mapping = {
autoTimestamp true // default, but I like to be explicit about it.
}
}
class B {
String whatever
}
(Careful of typos etc. I just did that off the top of my head)
I have a question about GORM and "multiple" hasmany relationships, and I didn't find an answer in my previous searches.
Let's say we have three domains:
class A {
...
static hasMany = [Bs: B]
}
and
class B {
...
static belongsTo = A
static hasMany = [Cs: C]
}
and
class C {
static belongsTo = B
String name
dateCreated date
}
I want to know if it is possible to get a list of objects of the class C, sorted by dateCreated, using an object of the class A (something like C.findAll(...., a: a.id) ) or if I have to use a more complex query ?
Best regards,
Its a little more difficult because you aren't storing a back reference to the parent objects
Something like this - I didn't test it myself
A.executeQuery("select distinct c from A a join a.bs as b join b.cs as c where a = :a", [a: a])
If B has:
static belongsTo = [a:A]
then you can do:
C.withCriteria {
a{
eq('id', <a's id here>)
}
order('dateCreated', 'desc')
}
I have a one to many relationship between a Course and Categories
class Course {
String code
static hasMany = [categories:CourseCategory]
}
Class CourseCategory {
String name
}
I need to query courses based on a list of categories. I have tried the query
courseInstanceList = Course.findAll("from Course c inner join c.categories cts where cts.id in :categoryIds",[categoryIds:categoryIds])
But this query returns both Courses and CourseCategories - just wondering how to create a query to just return courses?
You can use the createCriteria method:
def c = Course.createCriteria()
println (c.listDistinct {
categories {
'in' 'id', [1L, 2L, 3L]
}
})
Almost three years past ... )
Anyway here's the answer:
courseInstanceList = Course.findAll("SELECT distinct c from Course c inner join c.categories cts where cts.id in :categoryIds",[categoryIds:categoryIds])
to get just categories:
courseInstanceList = Course.findAll("SELECT cts from Course c inner join c.categories cts where cts.id in :categoryIds",[categoryIds:categoryIds])
I have two classes in Grails application ,Employee has hasMany(one To Many relations) for another class called Vote
class Employee {
String employeeName
static hasMany = [votes:Vote]
}
and vote class has voteRank integer property, and all employees vote on scale of 1-10 and every employee contains list of these votes given by other employees
class Vote {
Integer voteRank
static belongsTo =[employee:Employee]
}
How do I get the avg of all votes given per employee. This can be solved in SQL by using group by clause. But i am looking for grails domain mapping and gorm oriented answer for this problem.
This HQL query will work:
def averages = Employee.executeQuery(
'select e, avg(vote.voteRank) ' +
'from Employee e join e.votes vote ' +
'group by e')
This will return a List of Object[] arrays where the first element is the Employee and the second is the average. If you don't want to return the whole Employee instances you can select one or more properties:
def averages = Employee.executeQuery(
'select e.employeeName, avg(vote.voteRank) ' +
'from Employee e join e.votes vote ' +
'group by e')
You can accomplish this with a simple method on the Employee domain class.
class Employee {
String employeeName
static hasMany = [votes: Vote]
def sumVotes() {
votes.voteRank.sum() / votes.size()
}
}
I have domain objects for each table specified in the below query. I'm having trouble creating the withCriteria closure representing the below SQL query. Any thoughts?
Thanks!
Steve
SQL Query:
select A_NAME from A
where A_XID =
(select A_XID from B
where B_XID =
(select distinct B_XID from C
where D_XID = '${d.dXid}')
Domain Objects:
class A {
String aName
BigDecimal aXid <-- unique identifier
}
class B {
A a
BigDecimal bXid <-- unique identifier
}
class C {
D d
B b
}
I'm not sure how to do this with a criteria query, but in HQL it'd be
String aName = A.executeQuery(
'select c.b.a.aName from C c where c.d = :d',
[d: d])[0]
but you've left out a lot of information, so this is based on the assumption that you have these domain classes (you omitted the D class and mappings):
class A {
String aName
BigDecimal aXid
}
class B {
A a
BigDecimal bXid
static mapping = {
a column: 'A_XID'
}
}
class C {
D d
B b
static mapping = {
b column: 'B_XID'
d column: 'D_XID'
}
}
class D {
String someProperty
}