-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: How to make the CriteriaBuilder Autowired by Spring?
PostPosted: Tue Mar 14, 2017 2:07 am 
Newbie

Joined: Wed Jul 13, 2016 10:40 pm
Posts: 17
When needing CriteriaBuilder, it always has to call entityManager.getCriteriaBuilder().
Thus I want to configure the bean in the spring apllicationContext and add the Annotation @Autowired, but my configuration doesn't work.

Code:
   <bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="packagesToScan" value="cn.tata.t2s.ssm.entity" />
      <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
      <property name="persistenceUnitName" value="t2sPersistanceUnit" />
      <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
      <property name="jpaProperties">
         <props>
            <prop key="hibernate.physical_naming_strategy">cn.tata.t2s.ssm.util.AcmeCorpPhysicalNamingStrategy</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hiberante.format_sql">true</prop>
         </props>
      </property>
   </bean>

    <bean id="criteriaBuilder" class="org.hibernate.query.criteria.internal.CriteriaBuilderImpl">
       <property name="sessionFactory" ref="entityManagerFactory"/>
    </bean>


No qualifying bean of type [org.hibernate.internal.SessionFactoryImpl] found for dependency [org.hibernate.internal.SessionFactoryImpl]: expected at least 1 bean which qualifies as autowire candidate for this dependency.


Top
 Profile  
 
 Post subject: Re: How to make the CriteriaBuilder Autowired by Spring?
PostPosted: Tue Mar 14, 2017 3:08 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I checked the CriteriaBuilder and it looks like the class is thread-safe since the only state it has is the sessionFactory which is thread-safe.

So, you need to use the constructor-arg instead of property and you need to unwrap the entityManagerFactory to a SessionFactory:

Code:
<bean id="criteriaBuilder" class="org.hibernate.query.criteria.internal.CriteriaBuilderImpl">
       <constructor-arg ref="#{ entityManagerFactory.unwrap(Session.class) }"/>
</bean>


Top
 Profile  
 
 Post subject: Re: How to make the CriteriaBuilder Autowired by Spring?
PostPosted: Tue Mar 14, 2017 3:47 am 
Newbie

Joined: Wed Jul 13, 2016 10:40 pm
Posts: 17
vlad wrote:
I checked the CriteriaBuilder and it looks like the class is thread-safe since the only state it has is the sessionFactory which is thread-safe.

So, you need to use the constructor-arg instead of property and you need to unwrap the entityManagerFactory to a SessionFactory:

Code:
<bean id="criteriaBuilder" class="org.hibernate.query.criteria.internal.CriteriaBuilderImpl">
       <constructor-arg ref="#{ entityManagerFactory.unwrap(Session.class) }"/>
</bean>


get Another Exception:
org.springframework.expression.spel.SpelEvaluationException:
EL1008E:(pos 21): Property or field 'Session' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?

By the way, i have set a filter: OpenEntityManagerInViewFilter in my web.xml.
Would the "#{ entityManagerFactory.unwrap(Session.class) }" be incompatible with it?


Top
 Profile  
 
 Post subject: Re: How to make the CriteriaBuilder Autowired by Spring?
PostPosted: Tue Mar 14, 2017 3:53 am 
Newbie

Joined: Wed Jul 13, 2016 10:40 pm
Posts: 17
Code:
<bean id="criteriaBuilder" class="org.hibernate.query.criteria.internal.CriteriaBuilderImpl">
          <constructor-arg value="#{ entityManagerFactory.getNativeEntityManagerFactory() }"/>
</bean>


I find that this code works, but I am not sure that it's safe or not.


Top
 Profile  
 
 Post subject: Re: How to make the CriteriaBuilder Autowired by Spring?
PostPosted: Tue Mar 14, 2017 2:51 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
It's fine with getNativeEntityManagerFactory.

I wondered if that would have worked:

Code:
<bean id="criteriaBuilder" class="org.hibernate.query.criteria.internal.CriteriaBuilderImpl">
       <constructor-arg ref="#{ entityManagerFactory.unwrap( T(org.hibernate.Session).class ) }"/>
</bean>


Top
 Profile  
 
 Post subject: Re: How to make the CriteriaBuilder Autowired by Spring?
PostPosted: Wed Mar 15, 2017 12:14 am 
Newbie

Joined: Wed Jul 13, 2016 10:40 pm
Posts: 17
vlad wrote:
It's fine with getNativeEntityManagerFactory.

I wondered if that would have worked:

Code:
<bean id="criteriaBuilder" class="org.hibernate.query.criteria.internal.CriteriaBuilderImpl">
       <constructor-arg ref="#{ entityManagerFactory.unwrap( T(org.hibernate.Session).class ) }"/>
</bean>


Code:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'criteriaBuilder' defined in class path resource [spring/spring-dao.xml]: Cannot resolve reference to bean '#{ entityManagerFactory.unwrap( T(org.hibernate.Session).class ) }' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is javax.persistence.PersistenceException: Hibernate cannot unwrap EntityManagerFactory as 'java.lang.Class'
   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
   at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:648)
   at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:145)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
   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:197)
   at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:187)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1213)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1053)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018)
   at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
   ... 43 more
Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is javax.persistence.PersistenceException: Hibernate cannot unwrap EntityManagerFactory as 'java.lang.Class'
   at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:164)
   at org.springframework.beans.factory.support.AbstractBeanFactory.evaluateBeanDefinitionString(AbstractBeanFactory.java:1418)
   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.doEvaluate(BeanDefinitionValueResolver.java:255)
   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:340)
   ... 59 more
Caused by: javax.persistence.PersistenceException: Hibernate cannot unwrap EntityManagerFactory as 'java.lang.Class'
   at org.hibernate.internal.SessionFactoryImpl.unwrap(SessionFactoryImpl.java:891)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:462)
   at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:633)
   at com.sun.proxy.$Proxy38.unwrap(Unknown Source)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.springframework.expression.spel.support.ReflectiveMethodExecutor.execute(ReflectiveMethodExecutor.java:113)
   at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:129)
   at org.springframework.expression.spel.ast.MethodReference.access$000(MethodReference.java:49)
   at org.springframework.expression.spel.ast.MethodReference$MethodValueRef.getValue(MethodReference.java:347)
   at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88)
   at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
   at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:242)
   at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:161)
   ... 62 more


Exception message.


Top
 Profile  
 
 Post subject: Re: How to make the CriteriaBuilder Autowired by Spring?
PostPosted: Wed Mar 15, 2017 2:01 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
SPEL is very tricky indeed. Use the method that you found, and it's perfectly fine.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.