1
In all the Hibernate documentation, and web-based examples have this code example, or a very similar one, to use Hibernate.
List<Object> list = session.createCriteria(Object.class);
Since Session is an implementation of Hibernate’s Interface Session, but how do I get it, I’ve tried injecting it with @Autowired
spring(Fremework MVC I’m using), but the java.lang.NullPointerException
.
The question is, how do I get the right instance of Ibernate. Session?
EDIT 1
What used to be done was like in the code snippet below:
Session session = (Session) this.entityManager.unwrap(Object.class);
or
Session session = this.entityManager.unwrap(Object.class);
But, it seems to me, this was taken from Hibernate 4.2 or 5.*, I don’t know for sure, what happens is that I’m not getting this instance of Ssion anymore, always gives null Pointer.
EDIT 2
I tried what @Javeson Yehudi suggested to me, which is the following :
private static final SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
session = sessionFactory.openSession();
However, it still doesn’t work, the eclipse itself didn’t let me use it, because the class AnnotationConfiguration()
, not present in my libraries, but, as stated in the commentary, only Configuration()
worked, only that changed the error, now he says that I do not have the xml Hibernate.cfg., because I am doing this configuration via code, with Spring... follows the error.
GRAVE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dashboardController': Unsatisfied dependency expressed through field 'iService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'infoService' defined in file [H:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Administrator\WEB-INF\classes\br\com\finger\service\InfoService.class]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4725)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1404)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1394)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'infoService' defined in file [H:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Administrator\WEB-INF\classes\br\com\finger\service\InfoService.class]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1110)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1055)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1128)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
... 24 more
Caused by: java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1103)
... 35 more
Caused by: org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
at org.hibernate.cfg.Configuration.configure(Configuration.java:259)
at org.hibernate.cfg.Configuration.configure(Configuration.java:245)
at br.com.finger.service.InfoService.<clinit>(InfoService.java:40)
... 42 more
"Solution"
After reading the Hibernate documentation, I realized that the approach was wrong, and that in fact, it is not possible to inject this Hibernate.Sessionfactory dependency anymore, it was deprecated to encourage the use of the JPA specification, which makes things easier to maintain later, although it adds a bit of complexity, but as a good practice it is better to opt for specifics. Follow the trex and the link that talks about it.
Hibernate offers an Older, legacy org.hibernate.Criteria API which should be considered deprecated. No Feature Development will target those Apis. Eventually, Hibernate-specific criteria Features will be Ported as Extensions to the JPA javax.persistence.criteria.CriteriaQuery. For Details on the org.hibernate.Criteria API, see Legacy Hibernate Criteria Queries.
https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#criteria
I wrote about this years ago, maybe the post will be useful to you: http://www.simpledev.com.br/spring-hibernate.html
– Juliano Alves
Pretty cool your post... and exactly what I’m doing, only programmatically, because I don’t like xml
– Renato Vieira Dantas
This post was written almost 4 years ago :)
– Juliano Alves
Yes... that’s what I’m implementing now.
– Renato Vieira Dantas