Removing Data Source Connection Spring + JPA - java

Hy there folks, i'm facing a problem here where I work, there is a project when initialized works with 1 SQL Server Instance, 2 DB2 instances.
The problem is, this project was based on another project to keep the similar structure, but on this new project we don't use the SQL Server instance who is in reference on some xml files, I tried to comment these references, when I start the project on Tomcat, the project show me few errors.
As a new on Spring, how can I remove this connection properly??
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<jee:jndi-lookup id="IP_ICMS_RJ" jndi-name="java:comp/env/IP_ICMS_RJ" />
<jee:jndi-lookup id="USUARIO_ICMS_RJ" jndi-name="java:comp/env/USUARIO_ICMS_RJ" />
<jee:jndi-lookup id="SENHA_ICMS_RJ" jndi-name="java:comp/env/SENHA_ICMS_RJ" />
<jee:jndi-lookup id="IP_ICMS_SP" jndi-name="java:comp/env/IP_ICMS_SP" />
<jee:jndi-lookup id="USUARIO_ICMS_SP" jndi-name="java:comp/env/USUARIO_ICMS_SP" />
<jee:jndi-lookup id="SENHA_ICMS_SP" jndi-name="java:comp/env/SENHA_ICMS_SP" />
<jee:jndi-lookup id="PSACWSDocumentURL" jndi-name="java:comp/env/PSACWSDocumentURL" />
<jee:jndi-lookup id="PSACWSEndPoint" jndi-name="java:comp/env/PSACWSEndPoint" />
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<aop:aspectj-autoproxy/>
<bean id="FaleConoscoWSBean" class="br.com.embratel.faleconosco.ws.FaleConoscoWSBean">
</bean>
<bean id="RoboFaleConoscoWSBean" class="br.com.embratel.faleconosco.ws.RoboFaleConoscoWSBean">
</bean>
<!-- <bean id="faleConoscoDS" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/FaleConoscoDS" />
</bean> -->
<bean id="icmsRJDS" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/ICMS_RJ_DS" />
</bean>
<bean id="icmsSPDS" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/ICMS_SP_DS" />
</bean>
<bean id="icmsRJ" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="icmsRJDS" />
</bean>
<bean id="icmsSP" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="icmsSPDS" />
</bean>
<util:map id="icmsPropertiesMap">
<entry key="IP_RJ" value-ref="IP_ICMS_RJ" />
<entry key="USUARIO_RJ" value-ref="USUARIO_ICMS_RJ" />
<entry key="SENHA_RJ" value-ref="SENHA_ICMS_RJ" />
<entry key="IP_SP" value-ref="IP_ICMS_SP" />
<entry key="USUARIO_SP" value-ref="USUARIO_ICMS_SP" />
<entry key="SENHA_SP" value-ref="SENHA_ICMS_SP" />
</util:map>
<!-- Lista de processadores de OS -->
<util:list id="processors">
<bean class="br.com.embratel.faleconosco.ws.service.os.processor.ClienteProcessor" />
<bean class="br.com.embratel.faleconosco.ws.service.os.processor.ServicoEquipamentoProcessor" />
<bean class="br.com.embratel.faleconosco.ws.service.os.processor.SuspensaoProcessor" />
<bean class="br.com.embratel.faleconosco.ws.service.os.processor.SaldoProcessor" />
<bean class="br.com.embratel.faleconosco.ws.service.os.processor.DebitoAutomaticoProcessor" />
</util:list>
<context:annotation-config />
<context:component-scan base-package="br.com.embratel.faleconosco">
<context:include-filter type="annotation" expression="javax.jws.WebService" />
</context:component-scan>
<!-- <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="faleConoscoDS" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="SQL_SERVER" />
<property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="org.hibernate.type" value="true" />
<entry key="org.hibernate.transaction" value="true" />
</map>
</property>
</bean>
<bean id="jpaTemplate" class="br.com.embratel.faleconosco.ws.dao.JpaTemplateExt">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
enable the configuration of transactional behavior based on annotations
<tx:annotation-driven transaction-manager="transactionManager" /> -->
<bean id="ProtocoloWS" class="org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean" scope="prototype">
<property name="serviceFactoryClass">
<value>org.apache.axis.client.ServiceFactory</value>
</property>
<property name="wsdlDocumentUrl" ref="PSACWSDocumentURL" />
<property name="endpointAddress" ref="PSACWSEndPoint" />
<property name="serviceName">
<value>ProtocoloWSService</value>
</property>
<property name="namespaceUri">
<value>http://webservices.protocolounico.embratel.com.br/</value>
</property>
<property name="portName">
<value>ProtocoloWSPort</value>
</property>
<property name="portInterface">
<value>br.com.embratel.protocolounico.webservices.ProtocoloWS</value>
</property>
<property name="serviceInterface">
<value>br.com.embratel.protocolounico.webservices.ProtocoloWS</value>
</property>
<property name="customPropertyMap">
<map>
<entry>
<key><value type="java.lang.String">axis.connection.timeout</value></key>
<value type="java.lang.Integer">300000</value>
</entry>
</map>
</property>
</bean>
<bean id="LoggerAspect" class="br.com.embratel.faleconosco.ws.aop.LoggerAspect">
</bean>
<jaxws:endpoint xmlns:tns="http://ws.faleconosco.embratel.com.br/" id="faleconoscowsbean"
implementor="#FaleConoscoWSBean" wsdlLocation="wsdl/faleconoscowsbean.wsdl" endpointName="tns:FaleConoscoWSPort"
serviceName="tns:FaleConoscoWSService" address="/FaleConoscoWSPort">
<jaxws:features>
bean class="org.apache.cxf.feature.LoggingFeature" /
</jaxws:features>
</jaxws:endpoint>
<jaxws:endpoint xmlns:tns="http://ws.faleconosco.embratel.com.br/" id="robofaleconoscowsbean"
implementor="#RoboFaleConoscoWSBean" wsdlLocation="wsdl/robofaleconoscowsbean.wsdl"
endpointName="tns:RoboFaleConoscoWSPort" serviceName="tns:RoboFaleConoscoWSService" address="/RoboFaleConoscoWSPort">
<jaxws:features>
bean class="org.apache.cxf.feature.LoggingFeature" /
</jaxws:features>
</jaxws:endpoint>
</beans>
ERRORS
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'FaleConoscoWSBean': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private br.com.embratel.faleconosco.ws.service.usuario.UsuarioService br.com.embratel.faleconosco.ws.FaleConoscoWSBean.usuarioService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usuarioServiceImpl': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private br.com.embratel.faleconosco.ws.service.email.EnviarEmailService br.com.embratel.faleconosco.ws.service.usuario.UsuarioServiceImpl.enviarEmailService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'enviarEmailServiceImpl': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private br.com.embratel.faleconosco.ws.dao.JpaTemplateExt br.com.embratel.faleconosco.ws.service.BaseServiceImpl.jpaTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [br.com.embratel.faleconosco.ws.dao.JpaTemplateExt] is defined: Unsatisfied dependency of type [class br.com.embratel.faleconosco.ws.dao.JpaTemplateExt]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at

Well I Studied better Spring and re create the xml.
I removed this part of the code from the code above.
Creating the file from the begining.
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="SQL_SERVER" />
<property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
</bean>
</property>

Related

JPA Hibernate Spring MySql Tomcat - Connect to 2 databases

I've been searching, reading, trying code for 2 days and have not been successful.
I need to be able to connect to 2 different databases, not necessarily simultaneously, using the technologies listed in the header. I'm using Tomcat7, not a J2EE container.
Below is what I have for the application context. It works fine for one database. What do I need to do to configure it for two? How do I tell my DAOs which connection to use? Thanks in advance.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<tx:annotation-driven/>
<context:component-scan base-package="org.aaa.slds"/>
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSourceWCCC" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="username" value="${dbuser_wccc}"/>
<property name="password" value="${dbpassword_wccc}"/>
<property name="url" value="${dburl_wccc}"/>
<property name="driverClassName" value="${dbdriver_wccc}"/>
</bean>
<bean id="dataSourceDWS" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="username" value="${dbuser_dws}"/>
<property name="password" value="${dbpassword_dws}"/>
<property name="url" value="${dburl_dws}"/>
<property name="driverClassName" value="${dbdriver_dws}"/>
</bean>
<bean id="entityManagerFactoryWCCC" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSourceWCCC"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<map>
<entry key="hibernate.hbm2ddl.auto" value="create-drop"/>
<entry key="hibernate.show_sql" value="false"/>
</map>
</property>
<property name="packagesToScan" value="org.aaa.slds.core.models.entities.wccc"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>
As soon as I add a 2nd EntityManager I get a runtime errpr ///
INFO: HHH000204: Processing PersistenceUnitInfo [
name: punit2
...]
May 18, 2015 1:01:22 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
Here is my complete appcontext file ...
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<!-- Enable AspectJ style of Spring AOP -->
<aop:aspectj-autoproxy/>
<context:annotation-config/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<context:component-scan base-package="gov.wyo.slds"/>
<!-- Configure Aspect Beans, without this Aspects advices won't execute
<bean name="loggingAspect" class="LoggingAspect" /> -->
<bean id="dataSourceWCCC" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="${wccc.dbuser}"/>
<property name="password" value="${wccc.dbpassword}"/>
<property name="url" value="${wccc.dburl}"/>
<property name="driverClassName" value="${wccc.dbdriver}"/>
</bean>
<bean id="entityManagerFactoryWCCC" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="punit"/>
<property name="dataSource" ref="dataSourceWCCC"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false"/>
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<entry key="hibernate.hbm2ddl.auto" value="update"/>
<entry key="hibernate.format_sql" value="true"/>
</map>
</property>
<property name="packagesToScan" value="gov.wyo.slds.core.models.entities.wccc"/>
</bean>
<bean id="dataSourceDWS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="${dws.dbuser}"/>
<property name="password" value="${dws.dbpassword}"/>
<property name="url" value="${dws.dburl}"/>
<property name="driverClassName" value="${dws.dbdriver}"/>
</bean>
<bean id="entityManagerFactoryDWS" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="punit2"/>
<property name="dataSource" ref="dataSourceDWS"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false"/>
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<entry key="hibernate.hbm2ddl.auto" value="create"/>
<entry key="hibernate.format_sql" value="true"/>
</map>
</property>
<property name="packagesToScan" value="gov.wyo.slds.core.models.entities.dws"/>
</bean>
<bean id="transactionManagerWCCC" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryWCCC" />
<qualifier value="wccc"/>
</bean>
<bean id="transactionManagerDWS" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryDWS" />
<qualifier value="dws"/>
</bean>
<tx:annotation-driven/>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<!-- TODO: re-eanble when we have services -->
<!-- context:component-scan base-package="com.slds.core.services.impl"/> -->
And here is my persistence.xml
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.1">
<persistence-unit name="punit">
</persistence-unit>
<persistence-unit name="punit2">
</persistence-unit>
A more complete error is
Error creating bean with name 'DWSWageControllerImpl' defined in file [/Users/sja/Development/p20w-slds-poc/target/slds-1.0-SNAPSHOT/WEB-INF/classes/gov/wyo/slds/api/controllers/impl/DWSWageControllerImpl.class]
Unsatisfied dependency expressed through constructor argument with index 0 of type [gov.wyo.slds.core.services.DWSWageService]: :
Error creating bean with name 'DWSWageService':
Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private gov.wyo.slds.core.repositories.wccc.DWSWageRepository gov.wyo.slds.core.services.impl.DWSWageServiceImpl.dwsWageRepository;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dwsWageRepository':
Injection of persistence dependencies failed;
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined:
expected single matching bean but found 2:
entityManagerFactoryWCCC,entityManagerFactoryDWS;
Any help would be appreciated.
For that you need to have:
Two EntityManagerFactory:
<bean id="entityManagerFactoryWCCC" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSourceWCCC"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<map>
<entry key="hibernate.hbm2ddl.auto" value="create-drop"/>
<entry key="hibernate.show_sql" value="false"/>
</map>
</property>
<property name="packagesToScan" value="org.aaa.slds.core.models.entities.wccc"/>
</bean>
<bean id="entityManagerFactoryDWS" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSourceDWS"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<map>
<entry key="hibernate.hbm2ddl.auto" value="create-drop"/>
<entry key="hibernate.show_sql" value="false"/>
</map>
</property>
<property name="packagesToScan" value="org.aaa.slds.core.models.entities.dws"/>
</bean>
Two JPA transaction managers:
<bean id="transactionManagerWCCC" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryWCCC" />
<qualifier value="wccc"/>
</bean>
<bean id="transactionManagerDWS" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryDWS" />
<qualifier value="dws"/>
</bean>
And your Service methods are annotated for a specific transaction manager:
#Transactional("wccc")
or
#Transactional("dws")
If you want to enlist both data sources in a global transaction, you need to use JTA transactions and a JTA transaction manager.

Spring+Maven Property File not loading

I am trying to access a property file containing db configurations in a Maven + Spring project.
I get following error:
Cannot load JDBC driver class '${db_driver}'
My Property file is placed in src/resources folder.
Below is the tag to load property files:
<bean id="dbPropertyReader"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="locations">
<value>classpath:${appenv.deployment}.properties</value>
</property>
</bean>
Following tag uses properties loaded:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="url" value="${db_url}" />
<property name="driverClassName" value="${db_driver}" />
<property name="username" value="${db_username}" />
<property name="password" value="${db_password}" />
</bean>
Below are contents of properties file:
#JDBC Properties
db_driver=com.mysql.jdbc.Driver
db_url=jdbc\:mysql\://hostname\:3306/xxx_dbxxx?useUnicode\=true
db_username=abcdefgh
db_password=ijklmnopq
db_removeabadoned=true
db_initialsize=1
db_maxactive=2
${appenv.deployment} is a VMArgument set as follows:
-Dappenv.deployment=development
I have checked, this value is getting populated properly.
I am getting following line in logs:
Found key 'appenv.deployment' in [systemProperties] with type [String] and value 'development'
Then after this I am also getting following:
Loading properties file from class path resource [development.properties]
But some how, the values are not getting loaded.
Spring-Datasource.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dbPropertyReader"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="locations">
<value>classpath:${appenv.deployment}.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="url" value="${db_url}" />
<property name="driverClassName" value="${db_driver}" />
<property name="username" value="${db_username}" />
<property name="password" value="${db_password}" />
<property name="initialSize" value="${db_initialsize}" />
<property name="maxActive" value="${db_maxactive}" />
</bean>
<bean id="firstConfigDataFromDB" class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg type="javax.sql.DataSource" ref="dataSource" />
<constructor-arg index="1" value="tablename1" />
<constructor-arg index="2" value="propertyname2" />
<constructor-arg index="3" value="propertyvalue2" />
</bean>
<bean id="firstConfigDataFromDBFactory"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<constructor-arg ref="firstConfigDataFromDB" />
</bean>
<!-- DB Properties Initialization -->
<bean id="firstConfigurationPlaceHolder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2" />
<property name="ignoreUnresolvablePlaceholders" value="false"/>
<property name="properties" ref="firstConfigDataFromDBFactory" />
</bean>
<bean id="secondConfigurationFromDB"
class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg type="javax.sql.DataSource" ref="dataSource" />
<constructor-arg index="1" value="tablename2" />
<constructor-arg index="2" value="propertyname2" />
<constructor-arg index="3" value="propertyvalue2" />
</bean>
<bean id="secondConfigurationFromDBFactory"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<constructor-arg ref="secondConfigurationFromDB" />
</bean>
<!--
Error Map Initialization
Subtype of org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
-->
<bean id="secondConfigurationPlaceHolder"
class="com.application.SecondConfigurationPlaceHolder">
<property name="order" value="3" />
<property name="ignoreUnresolvablePlaceholders" value="false"/>
<property name="properties" ref="secondConfigurationFromDBFactory" />
</bean>
</beans>
Generic.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
>
<!-- Enable annotation scanning -->
<context:annotation-config/>
<!-- Initialise connection to Database -->
<import resource="Spring-Datasource.xml"/>
<!-- Initialize mail connection -->
<import resource="Spring-Mail.xml"/>
<!-- Inject database connection to DAO -->
<import resource="Spring-DAO.xml"/>
<!-- Other Beans Below -->
</beans>
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
>
<import resource="generic.xml" />
<bean id="applicationBean" class="com.application.ApplicationBean" scope="singleton" >
<property .. />
<property .. />
</bean>
</beans>
I am loading applicationContext.xml with following statement in code:
`appContext = new ClassPathXmlApplicationContext("applicationContext.xml");`
applicationContext.xml imports generic.xml.
generic.xml imports Spring-DataSource.xml.
have you added this to your application context file
<context:property-placeholder location="Path for .properties file"/>
add this line before the beans

Spring + GWT UnsatisfiedDependencyException

I looked at almost every topic about org.springframework.beans.factory.UnsatisfiedDependencyException but I can't find a solution for my problem. My service looks like this:
#Service
public class PacientService {
#Resource
private PacientDAO dao;
#Resource
private PacientModelTransformer transformer;
public PacientService() {
}
#Autowired
public PacientService(PacientDAO dao, PacientModelTransformer transformer) {
this.dao = dao;
this.transformer = transformer;
}
public PacientDTO getPacientById(Long pacientId) {
return transformer.toDTO(dao.readByPrimaryKey(pacientId));
}
}
<context:annotation-config />
<context:component-scan base-package="pl.arprojects.dietetyk" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/d2" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="database" value="MYSQL" />
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<hades:dao-config base-package="pl.arprojects.dietetyk.api.domian" />
This is my applicationContext.xml. I really dont know why I've got an exception like this:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pacientService' defined in file [D:\Dietetyk\Dietetyk\target\dietetyk-1.0.0-SNAPSHOT\WEB-INF\classes\pl\arprojects\dietetyk\server\service\PacientService.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [pl.arprojects.dietetyk.api.dao.PacientDAO]: : No matching bean of type [pl.arprojects.dietetyk.api.dao.PacientDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [pl.arprojects.dietetyk.api.dao.PacientDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
No, this DAO hasnt got any subclasses and looks exactly like this:
#Repository
public interface PacientDAO extends GenericDao<Pacient, Long> {
#Query("Select * from dietetyk_pacients where name = :name")
public Pacient getByName(#Param("name") String name);
#Query("")
public void deleteByPrimaryKey(#Param("id") long id);
}
Extends GenericDao from Hades Synyx
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:hades="http://schemas.synyx.org/hades"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://schemas.synyx.org/hades http://schemas.synyx.org/hades/hades.xsd">
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="database" value="MYSQL" />
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/d2" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<hades:dao-config base-package="pl.arprojects.dietetyk.api.domian" />
applicationContext.xml
Try #Repository or #Component annotation for all DAO classes including sub-classes of base DAO and no need to define a name in annotation.
Dependencies are resolved by type.

EntityManager and persist method not working properly

I'm in trouble here..
I'm new using Spring + Hibernate in a Java SE application...
I'm trying to instantiate the entityManager, but it's not working
I'm using the annotation #PersistenceUnit, like this:
#PersistenceUnit
public void setEmf(EntityManager emf) {
this.emf = emf;
}
And it works "fine", but it doesn't persist =/
When I change to
#PersistenceContext
public void setEmf(EntityManager emf) {
this.emf = emf;
}
It came out the following error:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'daoAbstract' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy10 implementing org.hibernate.ejb.HibernateEntityManagerFactory,org.springframework.orm.jpa.EntityManagerFactoryInfo' to required type 'javax.persistence.EntityManager' for property 'emf'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy10 implementing org.hibernate.ejb.HibernateEntityManagerFactory,org.springframework.orm.jpa.EntityManagerFactoryInfo] to required type [javax.persistence.EntityManager] for property 'emf': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:532)
Here's my applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="ConfiguradorDePropriedades"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<description>The service properties file</description>
<property name="location" value="file:AppConfig.properties" />
</bean>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- <property name="persistenceUnitName" value="ChatJpa" /> -->
<property name="persistenceXmlLocation" value="classpath:persistence.xml" />
<property name="dataSource" ref="dataSourceLocal" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="${database}" />
<property name="showSql" value="false" />
<property name="generateDdl" value="true" />
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSourceLocal"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pass}" />
</bean>
<bean id="daoAbstract" class="com.planner.pvc.dao.DaoAbstract">
<property name="emf" ref="entityManagerFactory" />
</bean>
<bean id="clienteDao" class="com.planner.pvc.dao.ClienteDaoImpl">
</bean>
<bean id="pvcMainController" class="com.planner.pvc.controller.PVCMainController">
<property name="dao" ref="clienteDao" />
</bean>
</beans>
And persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="ChatJpa">
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<!-- <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" /> -->
</properties>
</persistence-unit>
</persistence>
Any help would be appreciated.
Thanks
This line is the problem:
<bean id="daoAbstract" class="com.planner.pvc.dao.DaoAbstract">
<property name="emf" ref="entityManagerFactory" />
</bean>
Instead of doing this, let Spring inject the emf field annotated with #PersistenceContext, this will happen if you have a <context:component-scan.../> or <context:annotation-config/> tags in your application context or just add the latter.

ClassNotFoundException: org.hibernate.hql.internal.ast.HqlToken even after adding classic.ClassicQueryTranslatorFactory [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
org.hibernate.HibernateException: could not instantiate QueryTranslatorFactory: org.hibernate.hql.classic.ClassicQueryTransactionFactory
I am using Hibernate 4, Spring 3 and JSF 2.0. While running jsf page I am getting
org.hibernate.QueryException: ClassNotFoundException:
org.hibernate.hql.internal.ast.HqlToken [select generatedAlias0 from
net.test.model.Request as generatedAlias0]
I already have the following under hibernate properties in applicationContext.xml
<prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classic.
ClassicQueryTranslatorFactory
</prop>
Please note that I have added org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean and org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter, I suspect that after adding this I was again getting org.hibernate.QueryException: ClassNotFoundException
My question what is the equivalent of ClassicQueryTranslatorFactory for spring properties?
If no such properties how could I resolve this?
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Spring view scope customized -->
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="view">
<bean class="org.primefaces.spring.scope.ViewScope" />
</entry>
</map>
</property>
</bean>
<context:component-scan base-package="net.test" />
<!-- Data Source Declaration -->
<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="oracle.jdbc" />
<property name="jdbcUrl"
value="jdbc:oracle:thin:#server:1521:orcl" />
<property name="user" value="scott" />
<property name="password" value="tiger" />
<property name="maxPoolSize" value="10" />
<property name="maxStatements" value="0" />
<property name="minPoolSize" value="5" />
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<!-- JPA Entity Manager Factory -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="packagesToScan" value="net.test.model" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="${jdbc.dialectClass}" />
</bean>
</property>
</bean>
<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" />
<!-- Session Factory Declaration -->
<bean id="SessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>net.test.model.Request</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory
</prop>
</props>
</property>
</bean>
<!-- Enable the configuration of transactional behavior based on annotations
<tx:annotation-driven transaction-manager="txManager" />-->
<!-- Transaction Manager is defined
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory" />
</bean>-->
<!-- Transaction Config -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<tx:annotation-driven transaction-manager="txManager"/>
<context:annotation-config/>
<bean id="hibernateStatisticsMBean" class="org.hibernate.jmx.StatisticsService">
<property name="statisticsEnabled" value="true" />
<property name="sessionFactory" value="#{entityManagerFactory.sessionFactory}" />
</bean>
<bean name="ehCacheManagerMBean"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
<property name="locateExistingServerIfPossible" value="true" />
</bean>
<bean id="jmxExporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
<property name="server" ref="mbeanServer" />
<property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING"/>
<property name="beans">
<map>
<entry key="SpringBeans:name=hibernateStatisticsMBean" value-ref="hibernateStatisticsMBean" />
<entry key="SpringBeans:name=ehCacheManagerMBean" value-ref="ehCacheManagerMBean" />
</map>
</property>
</bean>
</beans>
edit modules/org/antlr/main/module.xml by adding the line <module name="org.hibernate" optional="true"/> inside the <dependencies> section. The issue must be resolved.
Thank you.
This issue has been resolved by doing the following
Use antlr 2.7.6 version which is compatible with Hibernate, add a reference in setDomainEnv.cmd
set CLASSPATH=\maven\repo\antlr\antlr\2.7.6\antlr-2.7.6.jar;%CLASSPATH%
More information here
Thanks

Categories