Do native queries use naming strategy to resolve the table name in the "from" clause of the given SQL query? - jpa

I have a custom naming strategy where I add a prefix to the table names. My question is: when I create a native query (using EntityManager.createNativeQuery) should I use the prefixed name of my tables in the FROM clause of my queries or should I use the non-prefixed name (as in JPQL queries) ??

A Native query is an SQL query, so you input what would execute in your datastore if you put it directly through JDBC. It is nothing to do with the Entities

Related

JPQL Foreign Key as condition in NamedQuery()?

I want to realize two tables, table one has OnToMany and Table two is ManyToOne (so we have a 1 : N relation). Solution: Avoid reserved words.
Looks like your JPA provider does not automatically quote SQL reserved words for you (ORDER) and so the RDBMS is objecting to the SQL thrown at it.
If that is the case (easily checked, can you do a simple query with no WHERE clause?) then you'll have to set the table name as 'ORDER', or change its name to a non-keyword, or use a JPA provider that does auto-quote such things for you (e.g DataNucleus JPA).

Conditional onDuplicateKeyUpdate in Jooq

I tried to use the jooq dsl for an insert/update query.
We have a unique (MemberId, GroupId) combination in our table, and a group membership Enum.
On sending a subscription request with (MemberId, GroupId, RoleEnum)
The query should insert a new entry to the db, but if the MemberId, GroupId combination already exists - then only update the entry if the new RoleEnum is larger than the existing one.
I failed to do this using one query with the jooq Dsl, so instead I had to use two queries (get, then insert or update accordingly) but then I lose the atomicity of the operation...
Is there a way to do this with one query with the jooqDsl?
jOOQ emulates PostgreSQL 9.5's support for the ON CONFLICT clause via:
insertInto(...).values(...).onDuplicateKeyUpdate()... of the MySQL syntax. This is possible only if your table is generated with explicit primary key information on it. (https://github.com/jOOQ/jOOQ/issues/5093, since jOOQ 3.8)
mergeInto(...).key(...).values(...) of the H2 syntax. With this syntax, you can providean explicit key column list specification, so the code generator is not needed to provide constraint meta information. (https://github.com/jOOQ/jOOQ/issues/4464, since jOOQ 3.7).
Unfortunately, there is not yet any native ON CONFLICT support in jOOQ.

How to convert this sql query to JPA criteria

I would like to do a string search on all the columns in my table or view in my database.
Below is my SQL query.
SELECT *
FROM MY_FAVORITE_MOVIES as t
where t::text like '%srk%'
The above query would do a search of the string in all the columns of the table.
How to do the same with JPA criteriaBuilder API?
I tried using the like operation but it seems like I have to do a string search for each and every column. Is there a way to do a search string in all columns at once.
No, this is not possible with JPA. You must query all fields separately. Or you may just trigger your SQL query as a native query from JPA.

Crate DB exception, no viable alternative at input 'VARCHAR'

I'm using Elastic search to store large amount of data to make it searchable, but for configuration items I'm still using HSQL DB.
Is it possible to eliminate HSQL DB completely and use my existing Elastic search in combination with Crate DB?
Things I have tried:
tried connecting to my existing Elastic search using Crate driver and Crate client but I got an exception No handler found for action "crate_sql". Does that mean I cannot use my existing ES and have to use inbuilt ES in crateDB??
After connecting to crateDB elastic search (and not my existing ES). I was able to get connection using CrateDriver and run SQL queries. But in one of module I'm creating table using below command:
create table some_table_name
(
id VARCHAR(256),
userName VARCHAR(256),
fieldName VARCHAR(256),
primary key (id),
unique (userName, fieldName)
);
...but then I got an exception:
io.crate.action.sql.SQLActionException: line 1:28: no viable alternative at input 'VARCHAR'
Does that mean I cannot write create table query using SQL syntax and SQL data types?
I know it will work if I use string data type instead of varchar, but I don't want to change all those queries now.
1)
No you cannot use existing ES nodes together with Crate. The whole SQL analyzer/planner/execution layer is done server side, not client side. In fact the crate clients are rather dumb.
2)
You'll have to change the types and also remove / change anything that isn't supported by crate. For example defaults or unique constraints aren't supported (up to 0.39 - in the future support might be added)
In your case the varchar type isn't valid in Crate, instead you'll have to use "string".
See Data Types Documentation for a list of supported data types.

DB2 Character Datatype and JPA Not Working

I am working with DB2 Universal database having lots of tables with columns of datatype CHARACTER. Length of these columns is variable (greater than 1 e.g. 18). As i execute a Native query using JPA it selects only first character of the column. Seems CHARACTER datatype is mapped to Java Character.
How can i get full contents in DB column. I can not change the database bening on Vendor side. Please note i need to do it both ways, i.e. :
Using JPQL ( is attribute columnDefinition can work in this case)
Using native DB query (no pojo is used in this case and i have no control over datatypes)
i am using Hibernate implementation of JPA provided by spring.
If these columns are actually common in your database, you can customize a dialect used by Hibernate. See comments to HHH-2304.
I was able to cast the column to VARCHAR to produce padded String results from createNativeQuery:
select VARCHAR(char_col) from schema.tablename where id=:id