Can you get metrics and statistics from Hibernate OGM? - java

I'm trying to get performance data from Hibernate OGM. It doesn't seem to have the Hibernate statistics classes like Hibernate ORM. Is there something equivalent?

The reason there are not specific classes is that you should be able to use the existing one in ORM.
For example:
org.hibernate.stat.Statistics statistics = getSessionFactory().getStatistics();
statistics.setStatisticsEnabled( true );
final Session session = openSession();
...
statistics.logSummary();
session.close();
If you think that something is missing or not working, you can signal it on the Hibernate OGM Jira or forum.
Davide

Related

JPA [Eclipselink] - How to refresh metadata of Dynamic entities created?

I got to know about possibility of Dynamic entity creation in eclipselink from here. And I'm trying to create Dynamic entities and map them to static entities which are already present in the same persistence unit as described in the examples given here.
I'm using refreshMetadata(with empty map of properties) of EntityManagerFactoryImpl to refresh metadata.
But the the dynamic entities are not getting listed in the metamodel of entitymanager factory.
Can somebody let me know where am I going wrong?
I expect they won't, as the Dynamic entity api adds mappings to the native EclipseLink session, while the JPA metamodel is build from JPA mappings. refreshMetadata is used to rebuild the native EclipseLink session using any new JPA metadata (orm.xml etc), but does not go the other way.
I was able to refresh the metamodel by adding a new metamodel with the current session by the following code snippet:
Metamodel metamodel = new MetamodelImpl((AbstractSession) dynamicHelper.getSession());
((EntityManagerFactoryImpl) emf).setMetamodel(metamodel);
Though this didn't solved my main problem, it solved the problem I've asked here.

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?

Spring MVC - no DAO service layer, how to get current session for criteria query

I have no DAO service layer in a spring MVC project. IN my controller, I wish to create a criteria query. For this I need a session object to call the "createCriteria(myClass.class)".
How do I get the session object ?
I saw some people were using the HibernateUtil class like "HibernateUtil.currentSession()". I tried this but the import cannot be resolved. I posted some of the relvant code to address another issue here Hibernate criteria queries - Query Conditions
Can someone please offer some form of guidance in this regard, Thanks.
HibernateUtil is a class you are supposed to create according to your own needs. Here's the corresponding docs section.
I would take a look at the dispatcher-servlet.xml. Sessions (Hibernate), tx managers are set up in the context for later access. old 2.5 example .. http://static.springsource.org/spring/docs/2.5.x/reference/orm.html
HibernateUtil is a class from the Hibernate tutorial. It's not for real use. Don't use it for anything but the tutorial. If you're already using Spring, it has excellent Hibernate integration. Just do it the right way from the start.
Ok, the problem was solved by using the entity manager exposed in the parent entity class. I have a class called person and in there is placed a transient method as follows
#Transient
public static Collection<?> searchResults(JsonJqgridSearchModel jsonJqgridSearchModel){
HibernateEntityManager hem = Person.entityManager().unwrap(HibernateEntityManager.class);
Session session = hem.getSession();
Criteria criteria = session.createCriteria(Person.class);
Iterator<JqgridSearchCriteria> iterator = jsonJqgridSearchModel.rules.iterator();
while(iterator.hasNext()){
criteria.add(iterator.next().getRestriction());
}
return criteria.list();
}
The main thing is how the HibernateEntityManager and the Session was obtained. Hope this helps someone out there.

Force update in Hibernate

How can I force Hibernate to update an entity instance even if the entity is not dirty? I'm using Hibernate 3.3.2 GA, Hibernate Annotations and Hibernate EntityManager btw. I really want Hibernate to execute the generic UPDATE statement even if no property on the entity has changed.
I need this because some event listeners need to get invoked to do some additional work when the application runs for the first time.
Thanks!
ok - found it myself. This does the trick:
Session session = (Session)entityManager.getDelegate();
session.evict(entity);
session.update(entity);
For transients, you can check
if(session.contains(entity)) {
session.evict(entity);
}
session.update(entity);
session.evict(entity);
session.update(entity);
Good trick, but watch out for transient objects before putting this into some automation code. For transients I have then StaleStateObjectException
Try em.flush() which is used for EJB 3.0 entities, which also uses JPA similar to Hibernate 3.2.2 GA. If it doesn't work normally, use flush in transactions.

JPA and PostgreSQL

I'm on a project that uses the EclipseLink implementation of JPA to talk to a PostgreSQL database. I have a task for which PostgreSQL NOTIFY/LISTEN seems like a perfect fit. Unfortunately, I'm a JPA newb, and am struggling to figure out how to make it work. So I guess I really have two questions; answering either one will make me happy.
1) What's the best way for me to get a hold of the direct JDBC connection to the database? (Which I sincerely hope will prove to be of type org.postgresql.PGConnection.)
OR
2) What's the best way for me to emulate/access org.postgresql.PGConnection.getNotifications() via EclipseLink JPA?
Thank you very much for your help.
Edit: Two working solutions! I love this site. If anybody has anything to say about hidden gotchas/benefits that would make either Pascal's or Balus's solution better than the other before I hand out the checkmark, I'd like to hear it.
Getting a JDBC connection from an EntityManager in EclipseLink is answered in the EclipseLink wiki.
The way differs per JPA API version. Here's an extract from the wiki:
JPA 2.0
entityManager.getTransaction().begin();
java.sql.Connection connection = entityManager.unwrap(java.sql.Connection.class);
...
entityManager.getTransaction().commit();
JPA 1.0
entityManager.getTransaction().begin();
UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl)((JpaEntityManager)entityManager.getDelegate()).getActiveSession();
unitOfWork.beginEarlyTransaction();
java.sql.Connection connection = unitOfWork.getAccessor().getConnection();
...
entityManager.getTransaction().commit();
You should be able to get it from org.eclipse.persistence.internal.jpa.EntityManagerImpl that is returned by EntityManager.getDelegate():
java.sql.Connection conn = ((EntityManagerImpl)(em.getDelegate())).getServerSession().getAccessor().getConnection();

Categories