Beating my head against the wall for hours and can't resolve this issue, seems to be either a mistake in my config setup or maybe a dependency conflict or missing dependency. Any thoughts?
Relevant dependencies:
Spring Data Commons Core 1.2.0
Spring Data JPA 1.8.0
Hibernate Core 5.0
Hibernate JPA 2.1
Error message in Eclipse:
Configuration problem: Failed to import bean definitions from relative location [SpringDbContext.xml] Offending resource: class path resource [SpringAppContext.xml]; nested exception is
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 43 in XML document from class path resource [SpringDbContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; systemId:
http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd; lineNumber: 43; columnNumber: 63; src-resolve: Cannot resolve the name 'repository:auditing-attributes' to a(n) 'attribute group' component.at
org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
SpringAppContext.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:context="http://www.springframework.org/schema/context"
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-3.2.xsd">
<import resource="SpringWebContext.xml"/>
<import resource="SpringDbContext.xml"/>
<import resource="SpringSecurityContext.xml"/>
<import resource="SpringSocialContext.xml"/>
<context:component-scan base-package="com.metagravy.ark.security"/>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n/messages"/>
<property name="useCodeAsDefaultMessage" value="true"/>
</bean>
</beans>
Hibernate configs:
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create-drop
hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
hibernate.show_sql=false
SpringDbContext.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:jpa="http://www.springframework.org/schema/data/jpa"
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-4.1.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:property-placeholder location="classpath:application.properties"/>
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource">
<property name="driverClass" value="${db.driver}"/>
<property name="jdbcUrl" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven/>
<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
<property name="packagesToScan" value="com.metagravy.ark"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.ejb.naming_strategy">${hibernate.ejb.naming_strategy}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<jpa:repositories base-package="com.metagravy.ark.security"/>
</beans>
Thanks for the help, I upgraded spring-data-commons from 1.2 to 1.10 and it resolved the error.
Related
I am developing a java Spring Hibernate application with mssql database. But i am not able to establish connection with mssql database.
[Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection]
my applicationContext.xml is given below:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Enable autowire -->
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.adapt.smileboard"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:resources mapping="/resources/**" location="/resources/"></mvc:resources>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" ></property>
<property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=test;" ></property>
<property name="username" value="user" ></property>
<property name="password" value="pwd" ></property>
</bean>
<!-- Session Factory Declaration -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" ></property>
<property name="packagesToScan" value="com.adapt.smileboard.entity" ></property>
<property name="hibernateProperties">
<props>
<!-- SQL Dialect -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- Your required Database Name -->
<prop key="hibernate.default_schema">smileBoardDB</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="format_sql">true</prop>
<prop key="use_sql_comments">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
</beans>`
You are using a Microsoft SQL Server driver and MySQL dialect.
Use org.hibernate.dialect.SQLServerDialect for Microsoft SQL Server.
Refer this.
I have been trying to implement the FullCalendar plugin and when I run the program through Apache Tomcat I get this error:
Cannot find class [org.springframework.web.servlet.mvc.support] for bean with name
'org.springframework.web.servlet.mvc.support#0' defined in ServletContext resource
[/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException:
org.springframework.web.servlet.mvc.support
The Dispatcher-Servlet.xml class is where the error is originating from
<?xml version="1.0" encoding ="UTF-8"?>
<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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class = "org.springframework.web.servlet.mvc.support"/>
<context:component-scan base-package="demo.*;"/>
<mvc:annotation-driven/>
<context:annotation-config/>
<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/db1"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id ="sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name = "configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name = "hibernateProperties">
<props>
<prop key = "hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id = "transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name = "dataSource" ref ="dataSource"/>
<property name = "sessionFactory" ref = "sessionFactory"/>
</bean>
<bean id ="viewResolver"
class = "org.springframework.web.servlet.view.internalresourceviewresolver"
p:prefix = "/WEB-INF/jsp/"
p:suffix = ".jsp"/>
</beans>
I presume I am missing some code here but I am not sure what is missing and why the error is displaying.
The exception says it cannot find the Class org.springframework.web.servlet.mvc.support which comes under Web Servlet Jar :
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.web.servlet</artifactId>
<version>x.x.x</version>
</dependency>
Can you check if you have/need a one ?
I am trying to import a standard spring project P1 into a web project P2 and I am implementing the web aspect of P1 in P2.
I have imported all resources of P1 in P2 as P1.jar
Have explicitly imported the application context file as well using <import-resource> which happens successfully.
But the JpaRepositories does not get autowired in P2. It doesn't seem to be in the context of P2.
Can anyone suggest what I might be missing here.
UPDATE: 25Nov2016
P1-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:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.11.xsd">
<tx:annotation-driven proxy-target-class="true"/>
<context:component-scan base-package="com.home.p1.blog" />
<jpa:repositories base-package="com.home.p1.blog.repo" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="url"
value="jdbc:derby://localhost:1527/MyDerby" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="WorkUp" />
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan" value="com.home.p1.blog"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_sql_comments">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="authorDAO" class="com.home.p1.blog.repo.AuthorDAO">
</bean>
<bean id="authorService" class="com.home.p1.blog.service.AuthorServiceImpl"></bean>
<bean id="blogService" class="com.home.p1.blog.service.BlogServiceImpl"></bean>
</beans>
P2-ApplicationContext.xml (its actually named: rest-servlet.xml to hold the RestController scan package)
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.11.xsd">
<import resource="classpath:com.home.p1.blog.src/src/main/resources/P1-ApplicationContext.xml"/> <!-- this loads find -->
<context:component-scan base-package="com.home.p2.blog.controller" /> <!-- this will load the RestController -->
<!-- <context:component-scan base-package="com.home.**" /> -->
<!-- THIS GIVES SOME WIERD ERROR <jpa:repositories base-package="com.home.*" /> -->
<mvc:annotation-driven/>
<!-- <bean id="blogService" class="com.oracle.blog.service.BlogServiceImpl"></bean> -->
</beans>
UPDATE: 28Nov2016
Upon including <jpa:repositories base-package> configuration in P2's application context, I no longer get the wierd error which I was getting earlier.
Also, enabling a successful import of xml config in Spring bean support, everything else is falling appropriately into place. Only the JpaRepositories referred in P1 are not reflecting in P2's context.
I dont think what you are trying to do is possible. You need to enable repository scan in your p2 and mention the packages in that scan.
If your p1.jar is in the classpath of p2 then you only have enable repository scan in your p2,
<jpa:repositories base-package="com.home.p1.blog.repo" />
RESOLVED: Please note that to import the JpaRepositories from P1.jar in P2, we don't have to mention <jpa:repositories> tag in P2's config xml.
Just ensure that we are appropriately importing the P1's config.xml.
Also Eclipse users need to add Xml / Java config in Spring Beans Support tab as well as enable support for import element.
anybody has idea why hibernate is not creating tables in my example ?
Here's my web.xml - http://pastebin.com/ZaseSaBS
mvc-dispatcher-servlet.xml - http://pastebin.com/LbdxMSAb
applicationContext.xml - http://pastebin.com/bAHMaVNX
console logs - http://pastebin.com/tTZZbxkX
I have similiar project with almost the same configuration and everything seems to run just fine on the other project. Any ideas why it's not creating tables here?
I've one test enity in com.calculator.enity with #Entity #GeneratedValue annotations, i have it listed in persistence.xml file. There's also JpaRepository for this entity in com.calculator.repository
It looks like your persistence configuration is not properly done. Use this sample persistence configuration as per your requirement. This is working fine for me.
<?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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.xxx.xxx"/>
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryBean" />
</bean>
<bean id="entityManagerFactoryBean"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.xxx.xxx" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/test" />
<property name="username" value="postgres" />
<property name="password" value="prateek" />
</bean>
</beans>
I think this error has been asked here. Although questions have been answered, but I still do not know what to do. "I cloned repo của in an IDE and deployed to Tomcat" and "cloned your repo on the commandline and ran a Maven build, the produced WAR file is fine" I have searched on the net a lot but still do not understand what i need to do .
my error:
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to load class declared as <mapping class="net.viralpatel.contact.form.Contact"/> in the configuration:
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
spring-servlet.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:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config />
<context:component-scan base-package="net.viralpatel.contact" />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>