Ebean request giving 2 identic rows - postgresql

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.

Related

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;

Jpql query across tables

Let'say i have the following tables:
Table A: id b-id
Table B: id property
Can I filter elements of table A like the following in JPQL language?
SELECT a FROM A a JOIN a.b-id targetId WHERE targetId.property = : someValue
I'd like to get table A's elements in which the referenced B element has property = someValue
And if I introduce a third table
Table A: id b-id
Table B: id c-id
Table C: id property
How can I get A's elements where c.property=someValue ?
I'm starting to get a sense of the power of ORM but some concepts are still vague to me.
Thank you for your answer
JPQL queries operate to entities, not to database tables. I assume names of entities and persistent attributes match to the names of tables and database columns given in question.
Because all relations in question are single valued, one-to-one or many-to-one (each A is connected only to one B (or possibly not to any), each B is connected to one C), specifying joins in query is not needed at all.
SELECT a FROM A a WHERE a.b.c.property = someValue
There is no need to worry about null values in path, because as said in JPA 2.0 specification:
Path expression navigability is composed using “inner join” semantics.
That is, if the value of a non-terminal field in the path expression is null,
the path is considered to have no value, and does not participate in the
determination of the result.
Same does not work for collection valued attributes (one-to-many, many-to-many), because it is not possible to navigate to their attributes via path expressions.

Insert into table with Identity and foreign key columns

I was trying to insert values from one table to another from two different databases.
My issue is I have two tables with a relation and the first table is having an identity column also.
eg table first(id, Name) - table second(id, address)
So now both the table exist with values in a db and i am trying to copy values from this db to another db.
So when I insert values from first db to second db the the first table will insert values for the Id column by itself so now I have to link that id to the second table.
How can I do that?
UPDATE using MSSQL server 2000
You can use #scope_identity immediately after your insert in SQL server 2000 which will give you the last id within the current scope but I'm not sure how that would work with bulk inserting of data
http://msdn.microsoft.com/en-us/library/ms190315.aspx
If this were SQL Server 2005 or later I would suggest using the output clause in your insert statement to retrieve the ids just inserted, but that was not available in SQL Server 2000.
If your data contains some column or series of columns which is unique other than the identity column, then you can query your first table based on that series of columns to get the ids and use that to populate your second table.
If the target tables were empty you could use SET IDENTITY_INSERT ON - this would allow to insert original values to identity columns, and you will not have to update referenced IDs. Of course if there is any existing ids that can overlap inserted ids - that is not the solution.
If names in first tables are unique, you could boild mapping between new and old ids and perform update something like this:
UPDATE S
SET S.id = F.id
FROM second S
INNER JOIN first_original FO ON FO.id = S.id
INNER JOIN first F ON F.name = FO.name
If names are not unique, then original ids should be saved in "first" in order to provide mapping between old and new ids. It can be temporary new column that can be deleted after ids in "second" will be updated.
Or as Rich Andrews said you could use #scope_identity, but in this case you will have to perform insert one by one - declare a cursor on source table, insert each record, get its new id and insert it into "second" table.

Replace Text of a field from a different table in SQL

I have two data buckets using a cryptic naming convention.
Am I able to update the data in the fields on the main table where the record entry is equal to the primary key on the other table?
Something like Table1 has 5 columns, t1A t1B t1C t1D t1E
and Table2 has 2 columns description, and Table1code.
Am I able to switch the data in Table1 with the description field in Table2?
I have tried doing a sql update/case statement but kept getting non-boolean errors when I would run it.
Any help would be appreciated.
You need to do an update with a join. Have a read of this http://www.bennadel.com/blog/938-Using-A-SQL-JOIN-In-A-SQL-UPDATE-Statement-Thanks-John-Eric-.htm

From SQL with Left Outer Join to JPQL or Native SQL

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.