From SQL with Left Outer Join to JPQL or Native SQL - jpa

Hiya all, my problem is related to converting one of the Sql with left join into jpa and entity structure, but i am stucked with it here is my sql
SELECT
*
FROM X_TABLE LEFT OUTER JOIN Y_TABLE
ON
TO_CHAR (X_TABLE.TIME1, 'HH24:MI') =
TO_CHAR (Y_TABLE.TIME1, 'HH24:MI')
AND
TO_DATE(Y_TABLE.DATE1) = TO_DATE('10/10/2010','dd/MM/yyyy')
AND
Y_TABLE.Z_TABLE_ID =X_TABLE.Z_TABLE_ID,
Z_TABLE
Where X_TABLE.Z_TABLE_ID =Z_TABLE.ID
Y_TABLE has foreign key of X_table
but when i query with this sql , X_TABLE which has no foreign key of Y_TABLE ,
where will my columns of Y_TABLE be stored in?
the relationship between Y_TABLE and X_TABLE is ManyToOne ,
so in the Y_TABLE entity i have #ManyToOne X_TABLE field,
when i map inversely this relationship in X_TABLE via mapped
i am having a Y_TABLE list
when i query this sql i am getting just a row with two tables columns?
but in entity meaning i am having a list of Y_TABLE?
how i can get the all returning columns in one entity, what should i do for that?
thanx all
system we have been working like below
eclipselink 2.0.1,Glassfish 3.0.1,JSF 2

Your query seems very complex. Perhaps start by explaining your object model, and what you are trying to do, and what you want back from your query.
Your function usage seems odd, why do you want to compare your times as chars and your chars as dates?
If your database is very cryptic, you may consider defining a view and mapping to it instead.

Related

OneToOne Join On Non-Primary Column Spring Data JPA Hibernate

I am using Spring data JPA(Hibernate).
I am trying to join my tables (Table A & Table B) but on Non-Primary Columns. Is it possible to actually do that? I am trying to use referenceColumnName, but it seems to not working, giving error :
Cannot set int to Integer.
When I am removing referenceColumnName, then it is working but obviously it is joining with Primary Key. Also in case of One-to-one Bidirectional, where should I place mappedBy & JoinColumn?
The annotation #JoinColumn indicates that this entity is the owner of the relationship (that is: the corresponding table has a column with a foreign key to the referenced table), whereas the attribute mappedBy indicates that the entity in this side is the inverse of the relationship, and the owner resides in the "other" entity.
Regarding the other question of using joining tables on Non-Primary columns, there are plenty of threads why don't you go through. for example
Does the JPA specification allow references to non-primary key columns?

How to Carry over a Table into a Column PostgreSQL

This May be a dumb question as I am a beginner in postgreSQL but what I'm trying to do is
I have a Table called Products and inside products there is 3 columns Name, Price, Expiry Date. Now I have a second table called orders with 4 columns. Product, purchasePrice, Amount, and CountryRecieved.
All I want is to reference the Product column to the product table so it has all the Information of the product table?
Is this do able?
The key concepts you need to read up on are:
"normalisation": the process of breaking down data into multiple related entities
"foreign keys": pointers from one database table to another
"joins": the query construct used to follow that pointer and get the data back together
In your case:
You have correctly determined that the information from Products should not just be copied manually into each row of the Orders table. This is one of the most basic aspects of normalisation: each piece of data is in one place, so updates cannot make it inconsistent.
You have deduced that the Orders table needs some kind of Product column; this is your foreign key. The most common way to represent this is to give the Products table an ID column that uniquely identifies each row, and then have a ProductID column in the Orders table. You could also use the product's name as the key, but this means you can never rename a product, as other entities in the database might reference it; integer keys will generally be more efficient in storage and speed, as well.
To use that foreign key relationship, you use a JOIN in your SQL queries. For example, to get the name and quantity of products ordered, you could write:
SELECT
P.Name,
O.Amount
FROM
Products as P
INNER JOIN
Orders as O
-- This "ON" clause tells the database how to look up the foreign key
On O.ProductId = P.ProductId
ORDER BY
P.Name
Here I've used an "inner join"; there are also "left outer join" and "right outer join", which can be used when only some rows on one side will meet the condition. I recommend you find a tutorial that explains them better than I can in a single paragraph.
Assuming the name column is key in Products table and product column in Orders table refers to it, you can join the two table on related column(s) and get all the information:
select
o.*, p.*
from orders o
join products p on o.product = p.name;

How can I join two tables by a foreign key table in Entity Framework?

I am trying to join two tables by a foreign key table in linq.
The two tables are
Items and Classes, and the foreign key table is ItemClasses (ItemId, ClassId).
My context does not have a DBSet for the foreign key table, and when I try to add it I get a model creation error.
When I saw the error, I noticed that this code referred to the foreign key table
modelBuilder.Entity<Classes>()
.HasMany(e => e.Items)
.WithMany(e => e.Classes)
.Map(m => m.ToTable("ItemClasses").MapLeftKey("ClassId").MapRightKey("ItemId"));
So it looks like I should be able to refer to the items through the class, but how do I join the tables?
I'd like to do something like this
query = from ig in myCtx.Items.AsNoTracking()
//join di in myCtx.ItemClasses.AsNoTracking() on ig.Id equals di.ClassId
join c in myCtx.Classes.AsNoTracking() on di.ClassId equals c.Id
join dd in myCtx.SalesAssociates.AsNoTracking() on dd.Id equals ig.SalesAssociateId
How do I do joins when the fk table is not in my context, but is referred to in one of the tables?
First, you have a configuration error. With HasMany / WithMany you are actually configuring the automatic "link" table, so
m.ToTable("Classes")
should be
m.ToTable("ItemClasses")
or how exactly you want that table to be named in the database.
Second, when working with EF, it's always preferred to use navigation properties rather than joins. In case of many-to-many relationship with auto link table, using navigation properties is mandatory. EF will produce the necessary joins for you.
So instead of join your would use something like this:
query = from ig in myCtx.Items.AsNoTracking()
from c in ig.Classes
...

Ebean request giving 2 identic rows

I have one row in my PostgreSQL table with the name I look for, but Ebean query gives me two identic results (same primary key). Each row has a unique name so I must be able to findUnique()
finder.where().eq("name", name).findUnique()
Handmade sql query gives me only one
String sql = "select id, name from totem where name ilike :name";
Any idea to get findUnique() working ?
Is it an Ebean bug ?
I have a OnetoOne relationship with table B.
So Ebean generates an inner join with this table.
Table B has two entry corresponding the object I'm fetching on table A.
So the request returns 2 rows.

Django-ORM Left join with all columns of both tables

i have two tables A and B, i need all the columns of both tables using django ORM(left join).
i am new bee to django and programming please help.
One way is to use the .values() callable on your query (though what you are asking is not very clear). This returns a querydict, rather than a queryset, but behaves more like a left join done SQL directly into the database - i.e returns the rows with null entries from table B.
Assuming table A has a foreign key to table B in the models file.
TableA.object.filter(your filters here).values(tableA__field1, tableA__field2 , ... \
tableB__field1, tableB__field2, etc).
https://docs.djangoproject.com/en/1.3/topics/db/aggregation/#values