How to get First 5 elements Hibernate - java

I have a very long object, so to make the system faster, I decided to show only the top 5 on "attendance" attribute (insted to show all the elements) when retrieve by the "get" service.
I have a attendances list, but there is a way to say only get the top 5 (on the list get) and when I got the results from the "detail" service, I will have all the itens.
#ManyToMany
private List<Attendance> attendance;

Write a custom repostitory, inject the entitymanager and use a query like that:
entityManager.createQuery("Select a from Enity e join e.attendence a")
.setMaxResults(5)
.getResultList();

Even you can do by Hibernate Criteria:
public List findByLimit(Class persistentClass, String orderproperty, int limit)
{
Session session = getSession(); // Get Session here
try
{
Criteria criteria = session.createCriteria(persistentClass);
criteria.addOrder(Order.desc(orderproperty));
criteria.setMaxResults(limit);
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
return criteria.list();
}
catch (RuntimeException re)
{
//Catch exception
}
finally
{
//close the session
}
}
Now call the above function :
findByLimit(Example.class,id,5); // here id is your order property

Related

How to handle EntityNotFoundException during Hibernate query?

I'm working on an application where we need to query for a collection of entities using hibernate and then perform some indexing on the results. Unfortunately, the database the application is querying from does not enforce foreign key constraints on all associations, so the application needs to handle potentially broken references.
Currently, the code responsible for doing the query looks like this:
private List<? extends Entity> findAll(Class entity, Integer startIndex, Integer pageSize)
throws EntityIndexingServiceException {
try {
Query query = entityManager
.createQuery("select entity from " + entity.getName() + " entity order by entity.id desc");
if (startIndex != null && pageSize != null) {
return query.setFirstResult(startIndex).setMaxResults(pageSize).getResultList();
} else {
return query.getResultList();
}
}
catch (Throwable e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
log.warn(sw.toString());
return Collections.EMPTY_LIST;
}
}
The problem with this code is that bad data in any of the results will result in the whole page of data being skipped. So if I'm using this method to get a list of objects to index, none of the query results will be included in the indexing even though only one or two were invalid.
I know Hibernate has a #NotFound annotation, but that comes with it's own side-effects like forced eager loading that I would rather avoid. If possible, I want to exclude invalid entities from the results, not load them in a broken state.
So the question then is how can I handle this query in such a way that invalid results (those that cause an EntiyNotFoundException to be thrown) are excluded from the return values without also throwing out the 'good' data?

Writing distinct in hibernate criteria

I want to write the below query using criteria .
I need to find the distinct rows and also use the current date in where clause .How can I achieve this in Criteria.
SELECT DISTINCT *
FROM EMAIL_PROGRAM
WHERE CURRENT_DATE >=PGM_START_DT
AND CURRENT_DATE <= PGM_END_DT
AND EMAIL_PGM_FLG ='Y'
AND EMAIL_PGM_DESC IS NOT NULL
and RGN_CD = 'US';
Below is my code in which I need to apply .
SessionFactory factory = null;
Session session = null;
try {
factory = getSessionFactory();
session = factory.openSession();
final Criteria criteria = session
.createCriteria(EmailDataBean.class);
returnList = criteria.list();
} catch (Exception e) {
logger.error(e.getMessage());
throw new DAOException(e);
} finally {
DBUtil.close(factory, session);
}
if (logger.isInfoEnabled()) {
logger.info(LOG_METHOD_EXIT);
}
return returnList;
}
you can use below on your criteria object.
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
You should do something like
//first add conditions in the where clause (you should use property names as defined in your Hbernate entities , not column names in your DB)
criteria.add(Restrictions.ge("PGM_START_DT", startDt));
criteria.add(Restrictions.le("PGM_END_DT", endDt));
criteria.add(Restrictions.eq("EMAIL_PGM_FLG", "Y"));
criteria.add(Restrictions.isNotNull("EMAIL_PGM_DESC"));
criteria.add(Restrictions.eq("RGN_CD", "US"));
Now, add every column (i.e. a Hibernate entity property/field) to a Projection list (this is needed to support distinct in the query)
ProjectionList pList = Projections.projectionList();
pList.add(Projections.property("PGM_START_DT"), "PGM_START_DT");
pList.add(Projections.property("PGM_END_DT"), "PGM_END_DT");
// add all the other properties (columns) and then have the Projections.distinct method act on the entire row (all the columns)
criteria.setProjection(Projections.distinct(pList));
By default, using Projections does return the result as a List<Object[]> rather than List<EmailDataBean>, which is usually not convenient. To remedy that, you should set a ResultTransformer
crit.setResultTransformer(Transformers.aliasToBean(EmailDataBean.class));
Alternatively, instead of using Projections, you can use
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
but this will not fetch distinct rows from the database but rather have Hibernate filter the results (removing the duplicates).

Left join using hibernate criteria

I have two entity: Issue and Issue_Tracker. I am using Hibernate 3.6.
SELECT `issues`.`issue_id`,
`issues`.`issue_raised_date`,
`issues`.`issue_description`,
`issue_tracker`.`tracker_status`
FROM `issues`
LEFT JOIN `issue_tracker` ON `issues`.`issue_id` = `issue_tracker`.`issue_id`
WHERE `issues`.`status`="Escalate To"
How to achieve this using Hibernate Criteria, and most Important, I have to use it for pagination.
and My Dao is as follows to show the list of Issues in jqgrid
public List showHelpDeskIssues(DetachedCriteria dc, int from,
int size) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
try
{
Criteria criteria = dc.getExecutableCriteria(session);
criteria.setFirstResult(from);
criteria.setMaxResults(size);
criteria.add(Restrictions.eq("status","Escalate To"));
return criteria.list();
}
catch (HibernateException e)
{
e.printStackTrace();
throw e;
} }
For brief explanation please refer this question how to show two tables data in jqgrid using struts2 - jqgrid plugin and hibernate
any help would be great.
you can try the following
Criteria criteria = session.createCriteria(Issues.class);
criteria.setFirstResult(from);
criteria.setMaxResults(size);
criteria.setFetchMode('parent.child', FetchMode.JOIN);
criteria.add(Restrictions.eq("status", "Escalate To"));
List<Issues> list= criteria.list();
here parent is the property name in Issues.java and child is the property in IssueTracker.java.
Well,
follow one sample...
Criteria crit = session.createCriteria(Issues.class);
crit.createAlias("otherClass", "otherClass");
crit.add(Restrictions.eq("otherClass.status", "Escalate To"));
List result = crit.list();
I think so this can to help!!
Try this out because this worked for me
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Issues.class, "issues");
criteria.setFetchMode("issues.issuetracker", FetchMode.JOIN);
criteria.createAlias("issues.issuetracker", "issuetracker");
criteria.add(Restrictions.eq("status","Escalate To"));
List list = criteria.list();
return list;
Note: In Hibernate criteria, inner outer join and inner join are one and the same. Do not get confused.
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
try{
Criteria criteria = session.createCriteria(Issues.class, "issues");
criteria.setFirstResult(from);
criteria.setMaxResults(size);
criteria.createAlias("issues.issuestracker", "issuestracker", JoinType.LEFT_OUTER_JOIN);
criteria.add(Restrictions.eq("issues.status", "Escalate To"));
return criteria.list();
}catch(HibernateException e){
e.printStackTrace();
throw e;
}
This should solve your issue. Let me know if you face any trouble.

How do I stop hibernate returning multiple instances when using "join" fetch mode?

Given the following I am trying to force the child collection (countryData) to be loaded when I perform the query, this works however I end up with duplicates of the Bin records loaded.
public Collection<Bin> getBinsByPromotion(String season, String promotion) {
final Session session = sessionFactory.getCurrentSession();
try {
session.beginTransaction();
return (List<Bin>) session.createCriteria(Bin.class).
setFetchMode("countryData", FetchMode.JOIN).
add(Restrictions.eq("key.seasonCode", season)).
add(Restrictions.eq("key.promotionCode", promotion)).
add(Restrictions.ne("status", "closed")).
list();
} finally {
session.getTransaction().commit();
}
}
I don't want the default (lazy) behavior as the query will return ~8k records thus sending 16k additional queries off to get the child records.
If nothing else I'd prefer.
select ... from bins b where b.seasonCode = ?
and b.promotionCode = ?
and b.status <> 'Closed';
select ... from binCountry bc where bc.seasonCode = ?
and bc.promotionCode = ?;
you can use CriteriaSpecification.DISTINCT_ROOT_ENTITY;
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

Hibernate is not deleting my objects. Why?

I have just set up a test that checks that I am able to insert entries into my database using Hibernate. The thing that drives me crazy is that Hibernate does not actually delete the entries, although it reports that they are gone!
The test below runs successfully, but when I check my DB afterwards the entries that were inserted are still there! I even try to check it using assert (yes I have -ea as vm parameter). Does anyone have a clue why the entries are not deleted?
public class HibernateExportStatisticDaoIntegrationTest {
HibernateExportStatisticDao dao;
Transaction transaction;
#Before
public void setUp(){
assert numberOfStatisticRowsInDB() == 0;
dao = new HibernateExportStatisticDao(HibernateUtil.getSessionFactory());
}
#After
public void deleteAllEntries(){
assert numberOfStatisticRowsInDB() != 0;
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
for(PersistableStatisticItem item:allStatisticItemsInDB()) {
session.delete(item);
}
session.flush();
assert numberOfStatisticRowsInDB() == 0;
}
#Test public void exportAllSavesEntriesToDatabase(){
int expectedNumberOfStatistics = 20;
dao.exportAll(StatisticItemFactory.createTestStatistics(expectedNumberOfStatistics));
assertEquals(expectedNumberOfStatistics, numberOfStatisticRowsInDB());
}
private int numberOfStatisticRowsInDB() {
return allStatisticItemsInDB().size();
}
#SuppressWarnings("unchecked")
private List<PersistableStatisticItem> allStatisticItemsInDB(){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();
Query q = session.createQuery("FROM PersistableStatisticItem item");
return q.list();
}
}
The console is filled with
Hibernate: delete from UPTIME_STATISTICS where logDate=? and serviceId=?
but nothing has been deleted when I check it.
I guess it's related to inconsistent use of transactions (note that beginTransaction() in allStatisticItemsInDB() is called several times without corresponding commits).
Try to manage transactions in proper way, for example, like this:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
for(PersistableStatisticItem item:
session.createQuery("FROM PersistableStatisticItem item").list()) {
session.delete(item);
}
session.flush();
assert session.createQuery("FROM PersistableStatisticItem item").list().size() == 0;
tx.commit();
See also:
13.2. Database transaction demarcation
I have the same problem. Although I was not using transaction at all. I was using namedQuery like this :
Query query = session.getNamedQuery(EmployeeNQ.DELETE_EMPLOYEES);
int rows = query.executeUpdate();
session.close();
It was returning 2 rows but the database still had all the records. Then I wrap up the above code with this :
Transaction transaction = session.beginTransaction();
Query query = session.getNamedQuery(EmployeeNQ.DELETE_EMPLOYEES);
int rows = query.executeUpdate();
transaction.commit();
session.close();
Then it started working fine. I was using SQL server. But I think if we use h2, above code (without transaction) will also work fine.
One more observation : To insert and get records usage of transaction is not mandatory but for deletion of records we will have to use transaction. (only tested in SQL server)
Can you post your DB schema and HBM or Fluent maps? One thing that got me a while back was I had a ReadOnly() in my Fluent map. It never threw an error and I too saw the "delete from blah where blahblah=..." in the logs.

Categories

Resources