Fetch data from more than two tables using grails domain - grails

I have 3 table like v_ims_circuits , v_ims_productcodes and v_ims_domainmain and i want to fetch data from this table using grails domain.
Internally query should be build like below query using grails domain.
select cir.circuitname, cir.status, cir.oldname, cir.speed, null "Count of Subs",cir.productcode, cir.ordernr,
cir.createuser, cir.createdate, cir.acquisitiondate,dom.domainname
from v_ims_circuits cir, v_ims_productcodes pc, v_ims_domainmain dom
where cir.productcode = pc.product
and pc.domainid = dom.id
and cir.circuitname = ?
Can any one help me on this.

GORM (and Hibernate) require a different mindset than SQL. The first step is to create domain classes for your three tables; circuits, productcodes, and domainmain. Chapter 7 of the Grails reference guide Object Relational Mapping (GORM) is a good place to start.
Once your domain objects are built section 7.4 Querying with GORM describes several ways you can fetch your data. I also find the Domain Classes Quick Reference to be helpful.

Related

Mapping Queries result set to domain objects in SDN 3.3.1.RELEASE

I have created a POC with SDN 3.3.1 in which I have deployed a plugin inside the Neo4j server. The plugin contains domain objects, repositories and controllers.
From my application, I'm making rest calls to the controllers to execute repositories methods and return the response.
The issue is that in my queries, I'm returning multiple nodes and relationships. So, to map the response, I created wrapper classes using #QueryResult, #ResultColumn containing references to domain objects for each query. This is because each query has a different result set.
Since, my application has around 150 such queries I will have to create similar number of intermediate wrapper classes.
This is quite tedious and number of wrapper classes will only increase in future as more and more queries are added.
Is there any smarter way to do this?
I tried to have all of my domain objects as references in a single wrapper class. So that I can use it for any of my queries. But it gives exception if any of the fields in the wrapper class is not present in the query result.
Another issue is that, some of my queries are written to return all different nodes connected to a particular node, e.g,
Match (a)-[rel]->(b)-[tempRel]->(tempNodes) Return b,tempRel,tempNodes
I'm not sure how to map this result set to a wrapper class.
Is there any way to achieve it without refactoring the queries to match indvidual paths?
Regards,
Rahul
Good idea to have it ignore unknown classes and fields in both directions perhaps as an additional annotation on the class can you raise a Spring JIRA issue?
something like this e.g.
#QueryResult(requireFields=false, require=false)
class MyResult {
String name;
int age;
}
but you still have them focused on one area or use-case and not 1 for all 150 use-cases but perhaps 15-30 different ones.

grails neo4j how to define relationship and their properties

In grails-neo4j plug-in,how can i define relationship among nodes and properties of relationship.
I can define my nodes as domain classes but what if i want to connect nodes i.e. i want to map relationship of nodes in some form.
I found spring-neo4j framework which works well for this case and i can map relationship to java class but didn't find any grails compatibility layer for this.
the plugin doesn't map the domain classes directly to nodes or relations in neo4j. It uses its own structure, like:
root -> domainClass.name -> relation -> domain class instance
I'd recommend to define your domain classes as nodes, and the plugin should handle them properly. Another way: abandon the plugin >)
Mapping domain classes as relationships is not yet supported in the Neo4j Grails plugin. I have this on my list of stuff to add.
You're right, Spring Data Neo4j already is capable of doing this. Be aware that Spring Data Neo4j and the Grails Neo4j plugin have nothing in common (aside that they use Neo4j to persist). The persistence layer is a completely separate code base.
It is now much later than when this question was originally asked - 4 years later. The plugin now supports Neo4j relationships being created as Grails domain classes and the relationship can have attributes. However, constraints on these attributes do not work yet. Below is the full code for a working, one-way relationship.
package neo4j.authentication.demo
import grails.neo4j.Relationship
import groovy.transform.CompileStatic
#CompileStatic
class BelongsTo implements Relationship<Car, Owner> {
String sinceWhen
String toString() {
sinceWhen
}
}

Does a hasMany relationship in GORM require an indexColumn?

I'm dealing with legacy tables right now. Our Grails app will simply display data. All data entry and updates take place using a different tool. It would be very difficult to add columns to the legacy database. Are indexColumns absolutely, positively required? Or is there some way I can simply not use indexColumns at all in this case?
http://grails.org/doc/latest/ref/Database%20Mapping/indexColumn.html
Not if it is a Set, which is the default type for hasMany relationships.
By default when mapping an indexed collection such as a Map or List the index is stored in a column called association_name_idx
Grails documentation

Reference data from views in breeze

We have a table (AttendanceType) in our database which have many fields with multiple options. this multiple options are defined in single table. So, instead of creating separate Option table for each option we have single table (Option_Data) with key to identify each option type (Record).
Example : AttendanceType table has following fields
ID
Description
Category (Payroll / Accrual)
Type (Hours / Days)
Mode (Work hours / Overtime / ExtraHours)
Operation (Add / Minus)
These fields have options (data as shown above in brackets) which comes from Option_data table. We have created separate views from this Option_data table example: vwOption_Attendance_Mode, vwOption_Attendance_Operation etc.
Now, how we can link this view in breeze so the reference data come automatically.
We are using EF6, SqlServer, Asp.Net WebApi. When the table relationship is defined in SQL Server Breeze works perfectly and manages the relational data. In this case we cannot define relationship in SQL Server between Table and Views.
How we can code it so Breeze can manage the relational / reference data for us? If you require further clarification please let me know.
Edit # Jay Traband : Let say a single table (ie: AttendanceType) has fields which get reference/lookup data for its field from Views. How in breeze we can relate them (table with views), as in SQL Server we cannot.
My reference points is when Tables are related breeze does excellent job. I want to achieve same with table and views.
Breeze gets its metadata (including the relationships between entities) from EF. You'll need to tell EF about the relationship between the tables and views, even if there is no such relationship defined in SQL Server. This SO post provides some clues, and this blog post gives some related information about creating relationships in the designer.

Custom Join table in grails

I have a following domains
User (in database called usermanagement) and
Account (in another database xyz)
Its an old system so i cannot really change the architecture of the system. I have been assigned task to implement a system that a certain users can only access certain accounts. This is a classic case of many-to-many relationship but the problem lies in the fact that these two domains are in two different databases. I googled if that was possible but i realized that it was not possible. So I now am thinking of creating a custom join table to store info on which user are allowed access to which accounts. The table can have two columns 'accountId' and 'userId'. So for this, do i have to create a new domain in grails or is there any cleaver way of doing this ?
Thanks in advance.
If you create joinTable in which DB you are going to create it and how you are going handle updates in main (Account,User) tables and ceep your join table up2date ?
I think (for this case) you don't need join table, you need handle business logic in application level.
You cane have 2 Domain classes each one pointed to different dataSource(DataBase).
http://grails.org/doc/latest/guide/conf.html#multipleDatasources
As I searched for solution of this, I did not find any sustainable solutions. I eventually narrowed down the probable solutions to two:
1. Create a domain table (only) using sql, some sort of patch and use hard-coded queries in grails to write and access data to and from the table.
2. Create a domain class like AccountUser having properties clientId and userId
I choose the 2nd option, I wrote some additional methods and created a service to return user and client instance and I am done ! Anyways, thanks guys.
If the databases are "visible" to each other (on the same server or there is a db link between them), you should be able to map the domain classes using the fully qualified table names ('schema.tablename') in the mapping closure.
psuedocode:
class User {
static mapping = {
table "usermanagement.user"
}
static hasMany = [Account:accounts]
}
class Account {
static mapping = {
table "xyz.account"
}
}
http://grails.org/doc/latest/guide/GORM.html#tableAndColumnNames

Resources