Spring hibernate query help - java

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.

Related

fetching single column values in JPA

I want to get all the values from a particular column in JPA and store all values into a list. currently, I am using the below approach but I am getting records in something else format.can someone please help me out
Query q1 = factory.createNativeQuery("select * from booking_attendee where booking_id="+id);
List<String> em1=q1.getResultList();
return em1;
query otput
em=[[Ljava.lang.Object;#68606667, [Ljava.lang.Object;#2cd7f99a, [Ljava.lang.Object;#137a5a5, [Ljava.lang.Object;#a45cc1c, [Ljava.lang.Object;#61fdc06d, [Ljava.lang.Object;#72f5eee1, [Ljava.lang.Object;#4e536797]
If you want to create a native query for this, it is more about how to solve this in SQL. You do not say SELECT * which means all columns. You would have to say SELECT your_column_name to select only a specific column.
Query q1 = factory.createNativeQuery("SELECT your_column FROM booking_attendee");
List<String> em1 = q1.getResultList();
The WHERE clause could and should be defined with the parameter binding of JPA. There are several advantages concerning performance and SQL injection.
Named parameter binding is special to the persistence provider (e.g. Hibernate). The common way for JPA is using ? to let your code be portable to other providers.
Query q1 = factory.createNativeQuery("SELECT your_column FROM booking_attendee b WHERE b.booking_id = ?");
q1.setParameter(1, id);
List<String> em1 = q1.getResultList();
Native queries offer the possibilities to use original SQL. Like this, some features which are specific for your database could be used with this. Nevertheless, if you do not have very specific SQL code, you should also have a look in JPQL, the specific query language of JPA, and the JPA Criteria API which offers advantages when you want to refactor your code, shows errors during compile time and makes the dynamic creation of queries easier.

How to replace exsisting SQL queries with Hibernate in exsisting jsf application?

I have multiple dao classes, I want to replace them with Hibernate ORM. I have generated all the POJOs and mapping for each table. Now, I don't know how to move ahead. Can someone help ? Thanks in advance.
You don't have to replace the SQL queries, just setup the hibernate configuration & use the createSQLQuery method
String sql = "SELECT * FROM EMPLOYEE";
SQLQuery query = session.createSQLQuery(sql);

Add SQL condition to CriteriaQuery

I need add something like Restrictions.sqlRestriction to CriteriaQuery. How to add SQL condition to CriteriaQuery?
My code:
CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<Installation> criteriaQuery = criteriaBuilder.createQuery(Test.class);
Root<Installation> root = criteriaQuery.from(Test.class);
criteriaQuery.select(root);
JPA Criteria allows function() to be used, or other clauses that have JPQL equivalents. Since you don't say what is the "SQL condition" then that's all that can be said. You certainly can't dump some random SQL into a JPQL filter for example
I am not sure that it can be done. I suggest you these 3 options:
Implement whatever you need to do in your Hibernate entity, but I guess you have already tried and are here looking for some other solution
Rework your entire query as SQL
A more creative one: define a new Hibernate entity on a database view doing the 'delicate' part of your query, and leave the rest to be managed via Criteria.
Solution for my case (call Oracle function 'bitand')
criteriaQuery.where(criteriaBuilder.notEqual(criteriaBuilder.function("bitand", Long.class, root.get(Test_.status).get(Status_.id), criteriaBuilder.parameter(Long.class, "status")),0));
TypedQuery<Installation> query = em.createQuery(criteriaQuery.select(root));
query.setParameter("status", 1L);

how to get the table2 information using hibernate concept

I have 2 tables in my database
table1--> id, name, address
table2--> id, tb1id
How can I get the table2 information using hibernate framework?
you can use ResultsetMetaData in spring/hibernate.
Check this answer..
Use annotations for getting ResultsetMetaData.
Thanks..

cross join issue in hibernate

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

Categories