Created query is not supported by my DB - java

I created an application using seam-gen. The created operation to search the DB ends with and exception (syntax error). The query has a where clause like this:
lower(barcode0_.barcode_ean) like lower((?||'%')) limit ?
Does hibnerate or seam create the where clause which my DB can't understand?
Or is there a workaround for SQL statement related issues in seam?

I'm not an expert of Seam but this looks like the problem mentioned in this thread (see also JBSEAM-3297). The suggested solution is use lower(concat instead of concat(lower in seam-gen/src/EntityList.java.ftl.

Related

Prevent Hibernate from returning a proxy in my criteria

Here is my criteria:
public Commercial findManager(Entity entity) {
DetachedCriteria criteria2 = DetachedCriteria.forClass(Role.class);
criteria2.createAlias("workStation", "workStation");
criteria2.createAlias("workStation.entity", "entity");
criteria2.add(Restrictions.eq("entity.id", entity.getId()));
criteria2.createAlias("commercial", "commercial", CriteriaSpecification.LEFT_JOIN);
criteria2.setFetchMode("commercial", FetchMode.JOIN);
criteria2.createAlias("commercial.function", "function");
criteria2.add(Restrictions.eq("function.name", "MANAGER"));
criteria2.setProjection(Projections.property("commercial"));
Commercial commercial = (Commercial) getHibernateTemplate().findByCriteria(criteria2).get(0);
return commercial;
}
This criteria works as intended except that it returns a proxy. Since I am going to query a lot of properties on that proxy I want to force hibernate to load that object in the same query, but even with fetchmode in JOIN mode it does not seem to work. What is wrong?
I use hibernate 3.3.2 and spring 2.5
Thanks to Dragan, I figured out that the proxy was actually initialized. It was actually a bad interpretation of what I was seeing in the JDBC log and in the debugger.
I was seeing this:
But when looking deeper into it, I figured out that the data was there, and making logs into the console for the data did not trigger new calls to the database:
After some more investigations I figured out that the FetchMode.JOIN and CriteriaSpecification.LEFT_JOIN in the original request are actually not needed to get this result. But the Adress field, which is a complex object, was not initialized, and querying on its attributes generated new requests to the database. Then I tried to add the following line and now it works like a charm (like in the previous screenshot where we can see that the adress field is a proxy)
criteria2.setFetchMode("commercial.adresse", FetchMode.JOIN);

Is there a HQL version of Hibernate's Restrictions.sqlRestriction()?

I have a Java application using Hibernate 4.3.6. We use two different databases (one for regular deploy, other for unit/integration tests). There's a common db-function we'd like to use, but it's called different in each db dialect and Hibernate has no support for it. We've fixed this by simply creating subclasses for each Dialect and using:
this.registerFunction("normalizedFunctionName",
new SQLFunctionTemplate(StandardBasicTypes.INTEGER, "arbitraryFunctionName(?1, ?2)"));
I can now use normalizedFunctionName(?, ?) in HQL. However I'd like to use it when using the Criteria API, something like:
sessionFactory.getCurrentSession()
.createCriteria(SomeClass.class)
.add(
Restrictions.lt("normalizedFunctionName(value, 'bla')", 3)
);
But this doesn't work. Now I've discovered there's:
Restrictions.sqlRestriction("arbitraryFunctionName(value, 'bla') < 3");
But since that's native SQL and not HQL, I can't use it.
So, my questions are:
Is there an HQL version of the Restrictions.sqlRestriction() feature?
Or is there another alternative to accomplish what I'm trying to do?

How to tell Hibernate annotation #Column to be case-sensitive?

I'm trying to do a simple SELECT query in a table named ECM (in uppercase) on a Sybase db with Hibernate. I've annotated my DBO this way :
#Entity
#Table(name="ECM")
public class RelationshipDbo {
...
}
However, I'm facing a "table not found" error : the generated SQL has the table name in lowercase. I cannot change the database configuration to tell it to be case-insensitive.
I've also tried putting quotes like this :
#Table(name="`ECM`")
and this :
#Table(name="'ECM'")
Result : the quotes are added in the query, but the table name is still converted from uppercase to lowercase.
Technical information :
Hibernate 4.3
JPA 1.2
org.hibernate.dialect.SybaseDialect
Have you guys any idea?
EDIT: Also tried this Hibernate changes #Table(name) to lowercase
Then my columns names and table name are automatically quoted, but the names still get lowercased.
I think I have your answer:
Basically, you need to change the naming strategy for you JPA provider. How you do this will depend on how you setup your project.
In my case, using spring boot data I set a property in my application.properties to
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.EJB3NamingStrategy
Without more details from you I can't give more specifics on how to do this.
My goal is a little different since was trying to create tables upper case and hibernate created them in lower case. Also i was using MySQL not Sybase.
But for me quoting the names like this worked:
#Entity
#Table(name="\"ECM\"")
public class RelationshipDbo {
...
}
Then tables were created upper case. Maybe that helps also for the queries.
What is your Sybase db version ?
SybaseDialect has been deprecated in Hibernate 3.5 and then refactored since Hibernate 4.1 with a bunch of subclasses matching different versions of Sybase. Have you tried one of the subclasses to see if it makes any difference?
org.hibernate.dialect.Sybase11Dialect
org.hibernate.dialect.SybaseAnywhereDialect
org.hibernate.dialect.SybaseASE15Dialect
Try this:
Use backticks as in #Table(name="`ECM`")?
This must work from Hibernate point. If not then problem should be in DB (if i'm not wrong)

how to get query string from a javax.persistence.Query?

Maybe I am missing something, but I simply want to (in my java program) get the query string from a javax.persistence.Query object?
The Query object itself doesn't seem to have a method to do that. Also I know that our manager doesn't want us to use Spring framework stuff (for example using their QueryUtils class).
Is there not a way to simply get the query string from javax.persistence.Query object (Again, in a java program) ?!
no problem. hibernate:
query.unwrap(org.hibernate.Query.class).getQueryString()
or eclipselink
query.unwrap(JpaQuery.class).getDatabaseQuery().getJPQLString(); // doesn't work for CriteriaQuery
query.unwrap(JpaQuery.class).getDatabaseQuery().getSQLString();
or open JPA
query.unwrap(org.apache.openjpa.persistence.QueryImpl.class).getQueryString()
...you get the idea....
There is no JPA-standard way, but some implementations have their own methods. For example DataNucleus JPA allows you to do
query.toString();
Look at the docs of your implementation for how they do it. See also this blog entry
http://antoniogoncalves.org/2012/05/24/how-to-get-the-jpqlsql-string-from-a-criteriaquery-in-jpa/
There is no standard way in JPA,
If you are using EclipseLink see,
http://wiki.eclipse.org/EclipseLink/FAQ/JPA#How_to_get_the_SQL_for_a_Query.3F
Edit your persistence properties file if JPA is provided by hibernate. Add the below section -
<property name="hibernate.show_sql" value="true"/>
Using OpenJPA I was able to do this:
OpenJPAQuery<MyEntity> q = (OpenJPAQuery<MyEntity>) query;
String mySQL = q.getQueryString();
The cast to OpenJPAQuery is important because the Query interface does not have the method getQueryString() on it.
The stored parameters are not filled in unfortunately but I'm working on figuring out how to do that myself.

Generated queries contain schema and catalog name

I've the same problem as described here
In the generated SQL Informix expects catalog:schema.table but what's actually generated is
catalog.schema.table
which leads to a syntax error.
Setting:
hibernate.default_catalog=
hibernate.default_schema=
had no effect.
I even removed schema and catalog from the table annotation, this caused a different issues : the query looked like that ..table same for setting catalog and schema to an empty string.
Versions
seam 2.1.2
Hibernate Annotations 3.3.1.GA.CP01
Hibernate 3.2.4.sp1.cp08
Hibernate EntityManager 3.3.2.GAhibernate
Jboss 4.3 (similar to 4.2.3)
Note that there is a new (as of 2010-04-26) web page, http://www.iiug.org/opensource, that has information about using Informix software with various open source packages, including Hibernate. In particular, there is downloadable code that improves the interaction of Hibernate and Informix.
I ended up with the one of the worst hacks I evever did:
The colon is the offending char wich should be '.' catalog:schema.table.
Informix allows comments in SQL statements select {comment} * from sometable
So I set
hibernate.default_catalog={
hibernate.default_schema=}schemaname
the resulting code looks like
select * from {.}schemaname.tablename
which is accepted by informix query parser.
Delete the schema and catalog attributes from *.hbm.xml.

Categories