I'm testing JDBC OrientDB driver for one of our projects. It seems to be working fine I just have one question. How can I fetch relationship data, is there equivalent for dot notation? Lets consider standard OrientDB demo data with Person and City classes. How can I fetch Person data and get related city name information
e.g.
select * from Person and how to get 'city.name' ?
Result of query for city value is: City#11:0{name:Rome,country:Italy} v1
It's easy:
select city.name from Person
Related
Has anyone got an idea why JPA Provider - Eclipselink (using version 2.6.0) generates query like:
SELECT ID FROM OPERATION WHERE (ID = ?);
or
SELECT ID FROM SUBSCRIPTION WHERE (ID = ?);
Why it needs to get ID providing an ID...
Maybe 1st or 2nd level Cache synchronization?
Maybe my queries are inaccurate...
In my queries I never ask directly this to execute - I use JPQL and never ask for ID giving some ID.
I have quite complex model and queries so I see no point in providing full code (or only if U really insist but I dont think it will help a lot).
Using Oracle 12c DB.
We have encountered the same issue with our Java application using EclipseLink 2.5.2. Specifically, we saw it whenever we inserted a new entity that had a M:1 relationship with another entity. A simplified use case would be:
A a = new A(); // the new entity
B b = lookupB(); // get existing entity from database
a.setB(b); // set M:1 relationship
em.persist(a); // save
In this case, we would always see a query for B (i.e., SELECT ID FROM B WHERE ID = #). After some research, we traced it down to the existence checking that EclipseLink performs before merging records.
We found that annotating the entity class for B with #ExistenceChecking(ExistenceType.ASSUME_EXISTENCE) prevented EclipseLink from running the query.
For a further discussion of this, see this related post.
I don't know whether I should be drawing parallels, but unfortunately, that's the only way I can express my issue.
In SQL, suppose we have two tables:
Employee with columns Employee ID, Employee Name, Dept. ID
Deptartment with columns Dept. ID, Dept Name
The Dept ID. in the Employee table is a foreign key with that in the Department table.
Now suppose I want to fetch the following columns:
Employee ID, Employee Name, Department Name
using a SQL such as:
SELECT A.EMPLOYEE_ID, A.EMPLOYEE_NAME, B.DEPT_NAME
FROM EMPLOYEE A, DEPARTMENT B
WHERE A.DEPT_ID = B.DEPT_ID
How would one do this using Core Data in Swift? I guess I'm getting confused by only seeing references to
NSFetchRequest(entityName: entityName)
where the entityName refers to a single entity (table in relational DB lingo)
I'd really appreciate any pointers or examples that can help me clear my doubts.
Thanks
It is certainly possible to create a fetch request that is equivalent to your SQL query. More complex queries can be difficult if not impossible to achieve with a single fetch request. But I recommend trying NOT to draw parallels between CoreData and SQL, at least until you have got to grips with how it works.
To take your example, in the CoreData view of the world, Employee would be an entity with a relationship to another entity, Department. A fetch request based on the Employee entity will return an array of Employee objects, and (assuming you create subclasses of NSManagedObject for each entity) you can access the attributes with simple dot notation:
let employeeName = myEmployeeObject.employeeName
But you can use the same notation to traverse relationships equally easily:
let departmentName = myEmployeeObject.department.departmentName
You don't need to worry about joins, etc; CoreData handles that for you.
Now, suppose you try to do it "the SQL way". You can construct a fetch request based on the Employee entity, but specify "properties to fetch" which likewise traverse the relationship:
let fetch = NSFetchRequest(entity:"Employee")
fetch.propertiesToFetch = ["employeeID", "employeeName", "department.departmentName"]
For this to work, you would need to specify the "resultType" for the fetch request to be DictionaryResultType:
fetch.resultType = .DictionaryResultType
The results from this query would be an array of dictionaries containing the relevant keys and values. But the link with the underlying objects is lost. If you subsequently want to access any details from the relevant Department (or even the Employee), you would have to run a new fetch to get the object itself.
Following are two tables:
Country >> CountryId, CountryName
City >> CityId, CityName, CountryId
I am using repository pattern to generate entity set from database. (example, repository.GetQuery<City>())
Now I want to write such a query that when I fetch City it also generates an populates for corresponding Country in the result set. So I can just access it like, city.Country.CountryName.
I know I can perform this by joining two entity sets, but that must be costly in performance. Probably, .Include<> may help me in achieving this. Any idea/ example to achieve this?
I'm going to assume, since you're using Entity Framework, that your City object has a navigational property of Country which points to the corresponding Country record.
As such, unless you specifically want to use lazy loading, you'll need to use Include. The good news is that you can do the includes inside of your repositories, and not have to write that code on the outside. An example of this can be found here.
Let me know if that works.
I have an web app which uses a DB for storage.
The code is C# there are two classes country and company.
The Company class contains a property with the relationship
List(Guid) CountryIds{get;private set}
The information is stored in the following manner in the db:
Company
Company_Country
Country
An example query is:
select c.* , cc.CountryId
from Company c
inner join Company_Country cc on c.id = cc.CompanyId
where (c.SomeColumn like 'somerestriction')
The question: What the most efficient way or even the current defacto standard to retrieve the relationship data from the database?
The options as I see it are:
use the above sql then in the c# code loop through the returned code adding the country id's to the correct company (very quick using a dictionary)
select the list of companies and then go back for each one with a separate sql query to get the list of country ids (simplest but big overhead to reconnect to the sql sever)
Use T-Sql to return the countryId in a xml format then use linq to extract the xml list from that comment (see sql below), note I would like to avoid delimited id strings.
Something like:
with cte(Id, CountryId) as
(
select CompanyId,
(SELECT CountryId as CountryId
from Company_Country p2
WHERE p2.CompanyId = p1.CompanyId
FOR XML PATH('')) AS CountryId
from Company_Country p1
group by CompanyId
)
select ss.*, cte.countryid
from StandardKitContent ss
inner join cte on cte.id = ss.id
order by ss.id
4 Other? (please comment)
Many thanks
Choco
If you're on a recent version of .NET, I recommend Entity Framework; it will slurp up your database tables, identify the relationships (if not automatically, then you can specify them in the designer or XML definition). Once that's done, it will generate business objects which will accurately reflect them, and you'll be able to do CRUD operations to your objects and have them persisted to your database.
Good luck!
Previously when I wanted obtain related data in an sql query I would join tables, however now in linq to entities I want to get data from a table that is related to the table through another table. I don't know how to perform this sort of query in linq to entities. If someone could help that would be good.
The example is a table named person which has a relationship to table users which is related to table roles. I want to be able to obtain a person that has a particular role. Since person is only related to user and indirectly through user to role, I'm not sure of the query. Also using navigation properties doesn't get me all the way there either.
Any information would be good. Here is an example of the database structure:
db structure http://img194.imageshack.us/img194/4540/persons.jpg
If you using the generator in VS (ie, drap drop the data table and diagram and keys are all set in db), then the thing you are asking could be already there automatically.
e.g.
from Person in Context.Persons
where Person.Name == "PETER PAN"
select Person.User.Role.RoleName;
Exact name need to refer to the code generator, but this is the idea. Linq to entities will help to map foreign keys and those for you.
Edit
Actually I haven't tried using include. But according to msdn:include method, the include should show the object hierarchy to work. So, for your query to work, try:
from c in db.Persons.Include("aspnet_Users").Include("aspnet_Roles")
where c.aspnet_Users.aspnet_Roles.RoleName == "Role" select c
And moreover, will you consider start from roles?
from r in db.aspnet_Roles
where r.RoleName == "ROLE"
select r.aspnet_Users.Persons
(from u in db.aspnet_Users.Include("Person")
from c in db.aspnet_Roles
where c.RoleName == "role"
select u.Persons);
Worked it out thanks for trying though.