Im currently researching how to port the Data Access Layer of an existing .NET 4.0 MVC 3 web application over to an entity framework. There are many reasons, but the primary one being due to thousands of stored procedures, adding just 1 field to a table results in 30 - 50 sproc edits!!
We are using MS SQL Server 2008 R2 and, ideally, we would like to use NHibernate and Fluent for mapping.
I have simplified the problem Im having into a simple example:
Imagine the following 2 tables:
'Products' Table
ID (INT)
DefaultName (NVARCHAR(128))
'Product Names' Table
ProductID (INT)
Name (NVARCHAR(128))
Culture (VARCHAR(10))
The Products table will contain a list of products, each of them will have a default, English, name. The Product Names table will contain the ID of the Product and many translations.
Currently, using stored procedures, we have the following:
SELECT Products.ID,
ISNULL(ProductNames.Name, Products.DefaultName) AS Name
FROM Products
LEFT JOIN ProductNames ON ProductNames.ProductID = Products.ID AND ProductNames.Culture = #Culture;
Note: #Culture is passed into the procedure
This always ensures a single Product with either a localised name or default (English) name is returned.
My question is: Is this possible to do at the Mapping level of Fluent NHibernate? I have been searching for days on 'How to join on 2 columns', but cant find a solution which works. It would seem odd if this is not possible in such a mature framework?
As an example of what I have been experimenting with:
public class ProductMap : ClassMap<Product> {
public ProductMap() {
Id(p => p.Id);
Join("ProductNames", pn => {
pn.Optional()
.KeyColumn("ProductID")
.Map(p => p.Name);
});
}
}
However, this results in the following exception:
More than one row with the given identifier was found: 109, for class: Product
This is because product 109 has 5 translations and thus all 5 cannot be mapped to a single string.
I have managed to use the 'HasMany<>' method to map all translations into a List within a Product. However, this is not what I need.
if the name is readonly then
public class ProductMap : ClassMap<Product> {
public ProductMap() {
Id(p => p.Id);
Map(p => p.Name).Formula("Select ISNULL(pn.Name, DefaultName) FROM ProductNames pn WHERE pn.ProductID = ID AND pn.Culture = '" + GetCUltureFromSomewhere() + "'");
}
}
Related
I am using System.Linq.Dynamic.Core v 1.2.24 to create a dynamic select statement when querying SQL Server via Entity Framework. This is sitting behind a GraphQL API and the intention is to make the database query as efficient as possible - only query the database for the requested columns.
I have the projection working for the type being enumerated but that type has a child property which also needs projection.
I have created a simple Fiddle to demonstrate this .
https://dotnetfiddle.net/nvU7DB
In the example I am querying a Person collection and Person has a property Address. I am successfully projecting the Person properties but can't figure out how to project the Address properties. I have not been able to find any examples of this.
This dynamic linq string will bring back all properties of Address:
"new Person(Id, FirstName, Address)"
but I want to do a projection on Address to just populate Id and City for example.
I was thinking the string might look like this
"new Person(Id, FirstName, new Address(Id, City)"
but this results in "Unhandled exception. Type 'Address' not found (at index 37)"
Without the 'new':
"new Person(Id, FirstName, Address(Id, City))"
which results in "Unhandled exception. No property or field 'City' exists in type 'Person' (at index 38)".
To clarify what I'm trying to do, this is what the projection would look like in C# code:
var projectedPeople = people.Select(
e => new Person() {
Id = e.Id,
FirstName = e.FirstName,
Address = new Address(){
Id = e.Address.Id,
City = e.Address.City
}
});
Any help or insights would be appreciated.
For anyone picking this up later, the solution was as follows
string dynamicSelect = "new Person(Id, FirstName, new Address(it.Address.Id, it.Address.AddressLine1, it.Address.City) as Address)";
ParsingConfig parsingConfig = new ParsingConfig(){ResolveTypesBySimpleName = true};
IEnumerable result = (IEnumerable)peopleQueryable.Select(parsingConfig, dynamicSelect);
I have a many-to-many relationship so I have three tables (administrators, offices and the intermediate table):
I need to do a very simple innerjoin of the three tables to show lastnames and their offices with Active Record. I tried but I couldn't.
This is what I tried:
$admins = Admins::find()
->joinWith(Oficinas::tableName())
->all();
echo '<pre>';
print_r($admins);
echo '</pre>';
die();
Also I would like to know how to show the SQL query so it can help me to find a solution.
You need to specify the relation name for joinWith() rather than table names. Since there isn't any info on the relation names I will use simple innerJoin using table names as per your requirements to display the last name and the office name for the admins.
Admins::find()
->alias('a')
->select('a.lastnameadm, o.nombreofi')
->innerJoin('admin_oficinas ao','ao.idadm = a.idadm')
->innerJoin('oficinas o','o.idofi = ao.idofi')
->all();
Try this:
TABLE_NAME_1::find()
->join('inner join',
'table_name_2',
'table_name_2.column = table_name_1.column'
);
->join('inner join',
'table_name_3',
'table_name_3.column = table_name_2.column'
)->all();
but if you can also use Via like the following example:
public function getClassschedule()
{
return $this->hasOne(TABLE_NAME_2::className(), ['table_name_1.column' => 'table_name_2.column'])
->via('tableName3');
}
when excuded, the results should be obtained like the previous example. so there's no need to repeated relations. full documentation can be seen here :
https://www.yiiframework.com/doc/guide/2.0/en/db-active-record
I want to write a specific query for my Hyperledger Composer app. Below, I have 2 assets and a transaction. Asset1 has a field called contents, which is an array of type Asset2. The associated code is below:
namespace org.acme.biznet
asset Asset1 identified by Asset1Id {
o String Asset1Id
--> Asset2[] contents
}
asset Asset2 identified by Asset2Id {
o String Asset2Id
}
transaction Transact {
--> Asset1 asset1
}
I want to select all instances of Transact where the associated Asset1 has a specified Asset2 inside. The closest solution I came to the query below, which did not work.
query GetTransactionsThatHaveAsset2 {
description: ""
statement:
SELECT org.acme.biznet.Transact
WHERE (asset1.contents CONTAINS (Asset2Id == _$propId))
}
The thing is, I have also written the query below.
query GetAsset1sThatHaveAsset2 {
description: ""
statement:
SELECT org.acme.biznet.Asset1
WHERE (contents CONTAINS (Asset2Id == _$propId))
}
This query behaves as intended, but it's selecting over Asset1. I want to select over Transact. How would I write this query?
no, you can't presently nest the query like you propose, nested named queries are not currently implemented in Composer (CouchDB is not a relational DB, and hence Composer query language can't translate the nested query presently to be translated to CouchDB) 2) Transact is a transaction - it contains a relationship identifier(s) you defined in your model, not the nested data stored in the related asset. You would have to define a query that searches for all transactions that matches the asset1 identifier field you passed to the trxn - then in your code, you can check transact.asset1.contents contains the 'something' (passed in to the trxn too?) using a javascript match - quite straightforward). Alternatively, you could use the REST API filters (loopback filters as opposed to queries) where (form your app code) you can resolve the relationships of transaction (Transact) back to asset1 (and its contents) using the REST call with filter eg
{"where":{"asset1":"resource:org.acme.net.Asset1#1"}, "include":"resolve"} . Hope this helps, maybe its nesting you're looking for exclusively..
In my case, I have these assets
// Define assets
asset Product identified by productId {
o String productId
o String description
o String serialNumber
o String modelNumber
o String status // TRANSFERED, RECEIVED, RECLAMED
o DateTime joinTime
--> Trader previousOwner
--> Trader currentOwner
--> Trader newOwner
}
// Trade, moves product from to a new owner.
transaction Trade {
--> Product product
--> Trader newOwner
o String trade_type
}
Executing a trade transaction results in the record:
{
"$class": "org.sp.network.Trade",
"product": "resource:org.sp.network.Product#123",
"newOwner": "resource:org.sp.network.Trader#6694",
"trade_type": "Trade",
"transactionId": "e39a86ed4748a3ab73b5e9c023f6bb0ca025098af09b8b5b2dca8f5f7ef0db67",
"timestamp": "2019-06-13T12:04:20.180Z"
}
And to query all Trade transactions that contain a product is
query ProductPath{
description: "Selete all Trade transactions for a specific ProductId"
statement:
SELECT org.sp.network.Trade
WHERE (_$productId==product)
}
Using the rest server: the value of _$productId is resource:org.sp.network.Product#123
I have many to many entities relation ships
Trace - Car
I want to select select all Traces where Car.TypeId = 1
How may i do that?
var traces = (from s in repository.AsQueryable<Traces>(new List<string> { "Cars" })
where s.Cars.TypeId== 1//how can I put this condition on collection?
select s).FirstOrDefault();
Something like the following will do it
var traces = context.Traces.Where(t => t.Cars.Any(c => c.TypeId == 1));
This will give you a list of traces that contain a car with a TypeId of 1.
Is there a way to querying across 2 databases in grails ?
Example (I made a select on two databases - works and test) :
select
c.crf_name,
c.crf_id,
ig.group_id,
ig.group_name,
from
works.crfs c,
test.item_groups ig;
1) I would like to query against two databases, and attach results to a domain.
Or :
2) Is it possible to mixing one query part with data from database and other part with domain class ?
Edit : I need to do a single query mixing tables from 2 databases (one db is PostgreSQL and other db is Mysql). So, in grails is it possible to mix to dataSources beans in one query ?
Edit 2 : Here a better example :
select
igm.item_id,
igm.item_group_id as group_id,
igm.crf_version_id,
ig.name as group_name
from
works.item_group_metadata igm,
test.item_group ig
where
igm.item_group_id=ig.item_group_id
;
If you are planning to do your own sql (like it seems to be the case) over 2 datasources, I suggest that you define your 2 datasources as Spring beans in grails-app/conf/spring.
e.g. (drop your db drivers in /lib and replace the values to match your RDBMS drivers and connection string) :
import org.apache.commons.dbcp.BasicDataSource
import oracle.jdbc.driver.OracleDriver
beans = {
worksDataSource(BasicDataSource) {
driverClassName = "oracle.jdbc.driver.OracleDriver"
url = "jdbc:oracle:thin:#someserver:someport:works"
username = "works"
password = "workspassword"
}
testDataSource(BasicDataSource) {
driverClassName = "oracle.jdbc.driver.OracleDriver"
url = "jdbc:oracle:thin:#someserver:someport:test"
username = "test"
password = "testpassword"
}
}
Then create a service to handle your queries, like :
import groovy.sql.Sql
class SomeService {
def worksDataSource
def testDataSource
def query1 = """
SELECT crf_name, crf_id
FROM works.crfs
"""
def query2 = """
SELECT group_id, group_name
FROM test.item_groups
"""
def sqlWorks = Sql.newInstance(query1)
def sqlTest = Sql.newInstance(query2)
// Then do whatever you like with the results of the 2 queries
// e.g.
sqlWorks.eachRow{ row ->
def someDomainObject = new SomeDomainObject(prop1 : row.crf_name, prop2 : crf_id)
someDomainObject.otherProp = whateverYouLike()
someDomainObject.save()
}
}
Your query doesn't have a where clause so I don't know how you want to relate the data coming from your 2 tables...
If you want to do a single query mixing tables from 2 databases, (ask your DBA to) establish a DBLink between databases test and works and perform the query on the datasource containing the DBLink.
I hope this helps.
You should use a "UNION SELECT" which is supported by most databases. Make sure that both select statements have the same number of columns.
select
crf_name,
crf_id
from
ig
UNION SELECT
group_id,
group_name
from
id
A union select concatenates the results for 2 queries, for example:
select 1 union select 2
Have you looked at the Datasources plugin? It sounds like it will do what you require, but I think you'd need to use HQL or finders on the domain objects directly, rather than SQL, for it to work.
I haven't used the plugin myself but I'd be interested to hear how it goes it you try it.
HTH