EclipseLink: Not appending TENANT_ID to WHERE clause of query for entityManager.merge(entity) operation - eclipselink

I am using EclipseLink and Spring Data JPA for single table multi-tenancy requirement.
My problem is TENANT_ID is not appended for UPDATE operation.
Spring Data JPA internally delegate this call to entityManager.merge(entity) but the problem is this call is not appending TENANT_ID to WHERE clause.
EXAMPLE:
What I want : UPDATE USER SET firstName = ?, lastName = ? WHERE userName = ? and TENANT_ID =?
What I get : UPDATE USER SET firstName = ?, lastName = ? WHERE userName = ?.
So, this is a problem as there may be user with same userName for more than one tenant in a single table.
Why TENANT_ID is not being appended fore em.merge(entity) operation?
The same case for em.remove()
If it is not than , please suggest any solution for this.

Related

SQL expression for not changing the field in UPDATE?

It must be a classic scenario, but I don't see any question for it...
I receive a lot of arguments in HTTP request for updating a row in the database.
Some other parameters are not set.
I am using some C++ framework for the SQL queries.
And I have a query like:
auto update(R"__(
UPDATE
table
SET
field1 = ?,
field2 = ?
WHERE
id = ?
)__");
exec_update(update, {field1.value_or(<?>), field2.value_or(<?>), id})
Because there can be around 30 fields to update, I don't want to create the query dynamically, but I would rather use some keyword to tell the Postgresql not to change the fields that I don't have new value for, how to achieve that?
Simply, what to put instead of the <?>?
I only found a similar question: How do I update selective fields in SQL (leaving some unchanged)?, which is a special case for this, I want to be able to set even NULL or whatever, I just want to not update some fields from the query without changing the query, I want to just have the values dynamic, not the query.
EDIT: I couldn't find anything better than having the dynamic query like:
auto update(fmt::format(
R"__(
UPDATE
table
SET
field1 = {},
field2 = {},
WHERE
id = {}
)__",
object.get_field1().has_value() ? fmt::format("'{}'", object.get_field1().value())
: "field1",
object.get_field2().has_value() ? fmt::format("'{}'", object.get_field2().value())
: "field2",
id));
, but I am not much satisfied with that.

update records with special conditions nhibernate

I have a table(InquiryTable) and first of all I select some records from it, after I extract these records from database, I update them into database. but I need to know how I can do these two commands simultaneously(merge them in one) by nhibernate.
inquiry = Session.Query<InquiryTable>().Where((c => c.ID == ID)).ToList();
inquiry.FirstOrDefault().Time= sendTime;
Session.Update(inquiry);
I want to merge Session.Query and Session.Update in one command. It means that I need an update with where in same query.
What would fit to this concept is NHiberante DML
13.3. DML-style operations
As already discussed, automatic and transparent object/relational
mapping is concerned with the management of object state. This implies
that the object state is available in memory, hence manipulating
(using the SQL Data Manipulation Language (DML) statements: INSERT,
UPDATE, DELETE) data directly in the database will not affect
in-memory state. However, NHibernate provides methods for bulk
SQL-style DML statement execution which are performed through the
Hibernate Query Language (HQL).
This feature set is not built on top of ICriteria or QueryOver, but it does use HQL.
An example from the doc, doing the UPDATE on filtered data in one shot:
string hqlUpdate = "update Customer c set c.name = :newName where c.name = :oldName";
// or string hqlUpdate = "update Customer set name = :newName where name = :oldName";
int updatedEntities = s.CreateQuery( hqlUpdate )
.SetString( "newName", newName )
.SetString( "oldName", oldName )
.ExecuteUpdate();
See also:
Update Top n using NHibernate
Do I have to refresh entities after bulk updates / deletes with HQL?

coldfusion sql query to check user

Trying to match received data from the form with one in the db. So if I dump form variable and the table I can see there is a match but coldfusion gives me this...
Column 'Kirill' is either not in any table in
the FROM list or appears within a join specification and is outside
the scope of the join specification or appears in a HAVING clause and
is not in the GROUP BY list. If this is a CREATE or ALTER TABLE
statement then 'Kirill' is not a column in the target table.
The query:
SELECT FIRST_NAME, PASSWORD
from APP.USERS_TASK
where FIRST_NAME = "#form.username#"
and PASSWORD = "#form.password#"
Also the same thing works just fine with id which been passed via url. With the different table though.
SQL requires strings to be in single quotes, not double.
SELECT FIRST_NAME, PASSWORD
FROM APP.USERS_TASK
WHERE FIRST_NAME = '#form.username#'
AND PASSWORD = '#form.password#'
But really, you should be using cfqueryparam to sanitize your user inputs and prevent SQL injection. This would also take care of any data typing and required quotes for you.
SELECT FIRST_NAME, PASSWORD
FROM APP.USERS_TASK
WHERE FIRST_NAME = <cfqueryparam value="#form.username#" cfsqltype="cf_sql_varchar">
AND PASSWORD = <cfqueryparam value="#form.password#" cfsqltype="cf_sql_varchar">

LinQ to SQL : Update on table data not working

I have a LinQ query which is intended to Update the table concerned.
The code is as follows:
LINQHelperDataContext PersonalDetails = new LINQHelperDataContext();
var PerDetails1 = (from details in PersonalDetails.W_Details_Ts
where details.UserId == userId
select details).First();
PerDetails1.Side = "Bridge";
PerDetails1.TotalBudget = 4000000;
PersonalDetails.SubmitChanges();
However, this change/update does not get reflected in the DB. Also,this does not throw any exception.Please suggest.
Make sure W_Details_Ts has one (or more) member properties marked as primary key. L2S can't generate update or delete statements if it does not know the underlying table's PK member(s).

How to get auto-increment after executing SQLQuery.executeUpdate() insert statement

Is it possible to retrieve autoinc field after I execute my insert:
String queryString = "insert into " + String.format("table%04d", id) +
"(sectionid, topicid, dc) values (" + entry.getSectionId() +
", " + entry.getTopicId() + ", " + entry.getDc() + ")";
SQLQuery query = session.createSQLQuery(queryString);
query.executeUpdate();
The table has four fields
eid - autoinc
sectionid - int
topicid - int
dc - int
Please help me
Unfortunately, you are bypassing much of Hibernate's database agnostic benefits, by using SQL queries instead of HQL. If you absolutely cannot use HQL, you will have to implement your own database specfic implementations. To do that, you first need to get the JDBC driver class from Hibernate:
How to get database metadata from entity manager
Once you have you driver class, you can do a database specific query based on that class. Since you can't modify the driver class to implement an interface with a specific get autoinc query, I recommend Nico's suggestion in the following link:
switch instanceof?
Once you implement a switch statement on the driver class, you can choose whether to implement a MYSQL, MSSQL, or other autoinc query.