cross join issue in hibernate - java

I have been using Hibernate 3.2 intailly for my J2EE application with Spring 2.5.Recently I wanted a feature of hibernate 3.5(BigInt Identity support).So I have upgraded my hibernate and now I facing a different issue with my queries.
HQL Query:-
select table from tableVO table where tableVO.subTableVO.id=:tableVO.id
SQL Query:-
select table_1_ID from table cross join subTable where subTable.id =table.id
I see that cross join is being done by hibernate which is not accepted by Sybase ASE. How can I fix this?

Check the dialect you have set in hibernate configuration. I'm going to assume you're running on Sybase ASE 15.x. As you found out, Sybase does not (yet) support CROSS JOIN, which is what the SybaseDialect attempts to use. Instead, use SybaseASE157Dialect or SybaseASE15Dialect. It will generate syntax that should look like:
select table_1_ID from table, subTable where subTable.id =table.id

You can change hibernate dialect,
in hibernate.cfg
<property name="hibernate.dialect">com.YourProject.YourDialect</property>
in your dialect class you should enter the syntax you want executed.
example dialect change for DB2
public class DB2390Dialect extends DB2Dialect
{
public String getIdentitySelectString() {
return "select identity_val_local() from sysibm.sysdummy1";
}...
}
Hope this helps

This is a bug with implicit joins in Hibernate. You can fix it by aliasing your joins:
select table from tableVO table
join tableVO.subtableVO subtable
where subtable.id=:tableVO.id

Related

How to implment hibernate IN clause with Criteria?

I'm working with hibernate to serach element into database.
I have yhis sql query to implement with hibernate.
select * from PRESence presence0
inner JOIN AgentMJ agentmj1_ on presence0.FK_AGENTMJ_ID=agentmj1_.PK_AGENTMJ_ID
where agentmj1_.PK_AGENTMJ_ID=18
and (presence0.typeContactLibre like 'Absence')
and agentmj1_.PK_AGENTMJ_ID
in (
select agentmj1_.PK_AGENTMJ_ID from AgentMJ agentmj1_
inner JOIN Absence absences2_ on agentmj1_.PK_AGENTMJ_ID=absences2_.FK_AGENT_MJ_ID
inner join TypeAbsence typeabsenc3_ on
absences2_.FK_TYPE_ABSENCE_ID=typeabsenc3_.PK_TYPE_ABSENCE_ID
where agentmj1_.PK_AGENTMJ_ID=18 and typeabsenc3_.code =1);
My problem is I can't implement the IN clause using hibernate.
How to do that? and there is an example to see it?Thanks in advance.

JPA generated SQL with "fetch first ? rows only" fails with ORA-00933 in Oracle 11g

In a Spring Boot 2.0 project, I have a JpaRepository object with query methods. One query method returns a paginated resultset.
Page<Model> findByPartContainingIgnoreCaseAndModelContainingIgnoreCaseAndNeedsUpdateContainingIgnoreCase(String part, String model, String needsUpdate, Pageable pageReguest);
This JPA query method runs when querying a Oracle 12c database, with the following SQL:
select * from (SQL) where rownum <= 100
The same JPA query method fails with (ORA-00933) when querying a Oracle 11g database, with the following SQL generated:
select SQL **fetch first 100 rows only**
Why does the same code base generate different SQLs for the different Oracle databases? How can we fix this?
The project uses the ojdbc6 (version - 11.2.0.4) Oracle Driver.
The issue was because of the dialect. I had used an older dialect at first - "org.hibernate.dialect.Oracle9iDialect"
The problem was resolved by setting the following dialect in application.properties.
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect

Can hibernate autogenerate an unlogged table?

#Entity
class MyEntity {
}
hibernate.hbm2ddl.auto=update
Is it possible that hibernate will generate a CREATE unlogged TABLE statement instead of CREATE TABLE when the table for MyEntity does not exist?
I think you can do this by subclassing the relevant Postgres dialect and overriding the getCreateTableString() method as below.
public class CustomPostgresSQLDialect extends PostgresSQLxxDialect{
#Override
public String getCreateTableString() {
return "create unlogged table";
}
}
And obviously set this as your dialect:
<property name="hibernate.dialect" value="org.foo.CustomPostgresSQLDialect "/>
I think there's no param in #Entity or #Table to pass that kind of options to the DBMS. Found this feature request in hibernate's jira though, it covers your use case, maybe you want to vote for it.
Anyway you can try and add an auxiliary database object in the hibernate configuration to run ALTER TABLE MyEntity SET UNLOGGED, it's the easiest way i can think of doing that DDL modification.
You should use update rather than create
this is helpful:
Hibernate hbm2ddl.auto default value

JPA Query MONTH/YEAR functions

How can I write a JPA query using MONTH function just like sql query?
#NamedQuery(name="querybymonth", query="select t from table1 t where MONTH(c_Date) = 5")
When I use the above pattern for query, I get an error: unexpected token - MONTH.
If you are using EclipseLink (2.1) you can use the FUNC() function to call any database function that is not defined in the JPA JPQL spec.
i.e.
FUNC('MONTH', c_Date)
In JPA 2.1 (EclipseLink 2.5) the FUNCTION syntax becomes part of the specification (and replaces the EclipseLink-specific FUNC).
If you are using TopLink Essentials, you cannot do this in JPQL, but you can define a TopLink Expression query for it (similar to JPA 2.0 criteria), or use native SQL.
Also if you are using any JPA 2.0 provider and using a Criteria query there is a function() API that can be used to define this.
I want to query YEAR(itemDate) but the function doesn't exit, then i saw the SUBSTRING() function so what i did was
Select q from table where SUBSTRING(itemDate, 1, 4)='2011'
and it works for me! hope it helps!
if you need you a dynamic variable, you can do that too. here :poDate is the year which is deifned in the setParameter();
#NamedQuery(name = "PurchaseOrders.findByYear", query = "SELECT p FROM PurchaseOrders p WHERE SUBSTRING(p.poDate, 1, 4) = :poDate")
Query q = em.createNamedQuery("PurchaseOrders.findByYear");
q.setParameter("poDate", s_year+"");
but if your okay with your solutions, that'll be fine. i just find JPA faster to execute.
The MONTH() function exists in Hibernate HQL but is not a standard JPA function. Maybe your JPA provider has some proprietary equivalent but you didn't mention it. If it doesn't, fall back on native SQL.
I am using Toplink Essentials for the same. Please help, if any function exists in Toplink. Thanks.
To my knowledge, TopLink doesn't have a direct equivalent. So either use a native SQL query or maybe a TopLink Expression query (not sure about this, and not sure this is available in TopLink Essentials).
Following worked for me with hibernate (4.3.9.Final) & JPA 2.1.
#NamedQuery(name = "PartyEntity.findByAID", query = "select distinct psc.party from PartyShortCode psc where (psc.shortCode = :aidNumber or FUNCTION('REPLACE',psc.accountReference,' ','') = :aidNumber) and psc.sourceSystem in :sourceSystem")
if your class holds a date type variable, you can use a query like this:
#Query("select m from Movement m where m.id_movement_type.id=1 and SubString(cast(m.date as text),1,4) = :year")
List<Movement> buysForYear(#Param("year") String year);

Spring hibernate query help

currently I am using the following to pull rows from a table called Table:
return getHibernateTemplate().find("from Table");
How do I use hibernate to pull only the first n rows from the table (i.e. like a mySql limit would do)?
Thanks.
Update: so is this the proper way to do it?
getHibernateTemplate().setMaxResults(35);
return getHibernateTemplate().find("from Table");
Use HIbernateTemplate setMaxResults to limit the results.
I ended up using a Hibernate Criteria query to do this and it works properly. I made use of the setFirstResult() and setLastResults() methods.

Categories