How to select from an Oracle 11g table called User using HQL - java

In Oracle, if you name a table User, you must query the table by putting quotes around the word user.
This will not work
select * from User
This will work
select * from "User"
My question is, how do I run a hibernate HQL query on a table named User? I have tried putting "" around User, escaping quotes, single quotes, nothing works. HQL doesn't like those characters and errors out. I have googled and searched for a solution and found nothing.

You need to escape the table name, in your entity mapping:
#Entity
#Table(name="`User`")
public class User {
...
}
The when you write an HQL query like this:
from User
Hibernate will generate an SQL query like this:
select * from "User"

Related

Distinct and List of other column in postgresql

I am using postgres DB and i have table with two column name and sal .
name Sal
Raunak 10000
Raunak 5000
Rahul 500
Raunak 300
And i want
Raunak 10000,5000,300
Rahul 500
i am using JPA is there any way to get in JPA data
You can use string_agg function to build a comma separated list of values:
select name, string_agg(sal::text, ',')
from t
group by name
You might want to consider json_agg instead of csv if your application can consume json data.
If you want to preserve the data type of the sal column, you can use array_agg() that returns an array of values. Not sure if JPA will let you access that properly though
select name, array_agg(sal) as sals
from the_table
group by name;
If I understand your question correctly, you want to get the result via below SQL statement:
SELECT
name,
string_agg (sal::varchar(22), ', ') as sals
FROM
test
GROUP BY
name;
Since it's postgresql related SQL, we can't or hard to express it via common object query. You can construct the above SQL via native query mode in your JPA code.
#Query(value = "<the above query>", nativeQuery = true)
List<Object[]> query();

Sybase DataContext : The column prefix does not match with a table name or alias name

We are dealing with sybase database in core java code. We are using org.eobjects.metamodel.DataContext to parse query.
String sCol[]=table.getColumnNames();
Query query=dataContext.query().from(table.getName()).select(sCol).toQuery();
return new QueryIterator(dataContext.executeQuery(query).iterator());
But it executing query. Same code working fine with Oracle database to parse and execute query.
Some of query example generated are :
select City.CityName from ownername.City
select City.CityName from City
select CityName from ownername.City
select CityName from City
select ownername.City.CityName from ownername.City
SELECT "City"."CityName" FROM ownername."City"
select * from ownername.City
No any of above query executed. We are getting these errors :
Not capable of parsing FROM token: "ownername"."City"
Could not execute query: The column prefix '"City"' does not match with a table name or alias name used in the query. Either the table is not specified in the FROM clause or it has a correlation name which must be used instead.
Not capable of parsing SELECT token: ownername.City.CityName
How can we execute query using metamodel wih SYBASE database OR is there any other way to execute sybase queries?
Oracle (and Microsoft) use a schema logical construct that Sybase ASE does not. In SAP/Sybase ASE, all tables and columns in a database are in the same schema. It is possible to users to have their own objects in the database though, so there is the possibility of imitating some of the schema behavior using user ownership, but it would require an extra level of effort.
For Sybase the proper query syntax would be:
SELECT [ Col1, Col2 | * ]
FROM [dbName].[ownerName.]TABLE
In your case
SELECT CityName
FROM dbName.ownername.City
In Sybase ASE, it's typically best practice to have all objects owned by 'dbo', so in that case you can omit the owner from the query:
SELECT CityName
FROM dbName..City
Full query syntax and information can be found in the Query Section of the Transact SQL Users Guide in the documentation.
The error messages you're getting are coming from MetaModel's query parser layer. It is searching for matching column and table names in the metadata of your database before it's even firing the query.
I notice that you're using namespace "org.eobjects.metamodel". You should upgrade to Apache MetaModel ("org.apache.metamodel") if possible since a lot has been improved in MetaModel since it's introduction into Apache. Including a lot of query parser improvements.

Force ebean to not include an ID in a generated query

I'm building a select that has to get me all distinct values from a table.
The sql I would normally write would look like this: "SELECT DISTINCT ARTIST FROM MUSICLIB"
However, ebean is generating the following: "SELECT DISTINCT ID, ARTIST FROM MUSICLIB"
The finder is as such:
find.select("artist").setDistinct(true).findList();
I've found that ebean is generating this ID on every single query, no matter what options I set.
How do I accomplish what I'm looking for?
You can't do that, Ebean for objects mapping requires ID field, and if you won't include it you'll get some mysterious exceptions.
Instead you can query DB without mapping and then write your SQL statement yourself:
SqlQuery sqlQuery = Ebean.createSqlQuery("SELECT DISTINCT artist FROM musiclib");
List<SqlRow> rows = sqlQuery.findList();
for (SqlRow row : rows) {
debug("I got one: " + row.getString("artist"));
}
Of course if artist is a relation, you need to perform additional query using list of found IDs with in(...) expression.

Intellij IDEA jpa console issue select from tables with specific columns

We have a project includes Eclipselink and MySQL.
For example if we use simple query:
SELECT a from ExampleTable a
it is transformed to something like this:
SELECT `id`, `code`, `high`, `key`, `name`, `regionalCode` FROM `ExampleTable`
and if I am using JPA Console I am getting something like that:
SELECT id, code, high, key, name, regionalCode FROM ExampleTable
and error message
[42000][1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key, name, regionalCode FROM ExampleTable' at line 1
The problem is "key" column but I have no chance to rename it.
I tried to find options to fix that but have no results.
I think if there will be no answer - the good way is to create bug report.
How we get enclosed names of columns?
We are using EntityManager
EntityManager em;
...
TypedQuery = em.createQuery("SELECT et FROM ExampleTable et");
And if we run it we see (in debug mode) that all names in generated query are enclosed with ` symbol.
key is reserved keyword of MySQL database (see MySQL: Reserved Words).
so you should escape reserved keyword in your mapping.
You can do it in the following way:
#Table
#Entity
public class ExampleTable implement Serializable
{
// ...
#Column(name = "'key'") // or #Column(name = "\"key\"")
private String key;
//...
}

Hibernate like query for escape character

I'm using Mysql database. I got stored data for certain columns with value of a\"a. I used following query to select but it failed:
select * from table_name where coloum_name like "%a\"%";
I spend some hours to find a query to select and the following one is working:
select * from table_name where coloum_name like "%a\\\\\\\\""%";
I'm using hibernate in my application, so I used:
criteria.add(Restrictions.ilike("coloum_name","%a\\\\\\\\""%"));
but it's not working. Any possible way to select via criteria??
Check this one. Hope this will help for you.
http://levanhuy.wordpress.com/2009/02/19/providing-an-escape-sequence-for-criteria-queries/

Categories

Resources