I have been updating the libraries on my application and adding new features
I migrated from Hibernate 3 to 4 I had to do some adjusts on the xml and the ImprovedNamingStrategy stopped working.
I'm sure it's a n issue with the configuration 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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- How to include more then one base package -->
<context:annotation-config/>
<context:component-scan base-package="com.lotjm"/>
<context:property-placeholder location="classpath:./properties/database.properties"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="com.lotjm"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true"/>
<property name="showSql" value="true" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider"></bean>
</property>
</bean>
<jpa:repositories base-package="com.lotjm.repository"
factory-class="org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.xx.domain.User</value>
<value>com.x.domain.Application</value>
<value>com.xx.domain.Project</value>
<value>com.xxx.domain.Document</value>
<value>com.xxx.domain.AbstractAuditingEntity</value>
</list>
</property>
<property name="namingStrategy">
<bean class="org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy"/>
</property>
</bean>
<bean id="auditingProvider" class="com.lotjm.config.audit.UsernameAuditorAware"/>
<bean id="dateTimeService" class="com.lotjm.config.audit.CurrentTimeDateTimeService"/>
<bean id="dateTimeProvider" class="com.lotjm.config.audit.AuditingDateTimeProvider">
<constructor-arg index="0" ref="dateTimeService"/>
</bean>
<jpa:auditing auditor-aware-ref="auditingProvider" set-dates="true" date-time-provider-ref="dateTimeProvider"/>
</beans>
Although there is a column named document_history on the database the application is creating a new one called documentHistory
Even the fact that it's creating a new column is wrong.
<prop key="hibernate.hbm2ddl.auto">validate</prop>
2 things to fix the issue
the property name was wrong, has to be jpaPropertyMap
and hibernate.ejb.naming_strategy was missing the ejb
<property name="**jpaPropertyMap**">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.**ejb**.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
</props>
</property>
Related
I am using spring(4.2.1) aop transaction management with hibernate(3.6.6 Final).
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<context:property-placeholder location="classpath:database.properties" />
<bean id="dataSource" class="com.company.product.datasource.EncryptedDataSource">
<property name="driverClassName">
<value>${db.driverClassName}</value>
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.timeout">14400</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">3000</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>
<prop key="hibernate.transaction.flush_before_completion">true</prop>
<prop key="hibernate.transaction.auto_close_session">true</prop>
<prop key="hibernate.jdbc.fetch_size">1000</prop>
<prop key="hibernate.jdbc.batch_size">100</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<prop key="hibernate.jdbc.batch_versioned_data">true</prop>
<prop key="hibernate.order_inserts">true</prop>
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>...</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="nestedTransactionAllowed" value="true" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="importConfig"
expression="execution(public * com.company.product.service.impl.ImportServiceImpl.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="importConfig" />
</aop:config>
<bean id="importDao" class="com.company.product.dao.impl.ImportDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="importService" class="com.company.product.service.impl.ImportServiceImpl">
<property name="importDao" ref="importDao" />
</bean>
now when i am call a update query in database it runs without any error and return the no of updated row count but after the end of the transaction if i check the db it is not getting reflected in db. This issue only occur if i use a sql update query.
Dao code snippet
public void updateIsSubmittableForIncompleteSections(Long documentId, Long supplierId, Set<Long> incompleteResponseDetailIds) {
Session session = getSession();
Query query = session.createSQLQuery("update response_details set is_submittable=0 where username_id=:supplierId and response_details_id in (:incompleteResponseDetailIds) and top_level_element_id in (select distinct top_level_element_id from element where document_id=:documentId)");
query.setLong("documentId", documentId);
query.setLong("supplierId", supplierId);
query.setParameterList("incompleteResponseDetailIds", incompleteResponseDetailIds);
query.executeUpdate();
}
appreciate your valuable responses. Thank you.
New to Hibernate and Spring. So I copied and pasted some configurations online and did the rest myself.
However when I try to start my Jetty server I am getting a Spring error.
Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/data/jpa]
What does this mean?
<?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:repository="http://www.springframework.org/schema/data/repository"
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-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<jpa:repositories base-package="com.testproject.testpackage.example1.repository"
transaction-manager-ref="example1TransactionManager"
entity-manager-factory-ref="example1EntityManagerFactory">
<repository:include-filter type="regex" expression=".*Repository"/>
</jpa:repositories>
<bean id="example1hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="example1Datasource"/>
<property name="connectiontestprojectQuery" value="SELECT 1"/>
<property name="dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"/>
<property name="minimumIdle" value="5"/>
<property name="maximumPoolSize" value="40"/>
<property name="idleTimeout" value="2000"/>
<property name="dataSourceProperties">
<props>
<prop key="url">${db.url}</prop>
<prop key="user">${db.username}</prop>
<prop key="password">${db.password}</prop>
</props>
</property>
</bean>
<bean id="example1Datasource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="example1hikariConfig"/>
</bean>
<bean id="hibernate.properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.show_sql">${hibernate.showSql}</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
</props>
</property>
</bean>
<bean id="example1EntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="example1"/>
<property name="dataSource" ref="example1Datasource"/>
<property name="persistenceProviderClass" value="org.hibernate.jpa.HibernatePersistenceProvider"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="packagesToScan">
<list>
<value>com.testproject.testpackage.example1.repository</value>
</list>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
<property name="jpaPropertyMap" ref="hibernate.properties"/>
</bean>
<bean id="example1TransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="example1EntityManagerFactory"/>
<qualifier value="example1"/>
</bean>
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven/>
</beans>
Replace the url in the beans tag
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
with
http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
It seems that the spring-data-jpa-{version}.jar is not included in you project.
Check the library or dependency.
I'm using Hibernate with this SessionFactory configuration
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="sessionFactory" scope="singleton"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>mappings/File1.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="show_sql">false</prop>
<prop key="hbm2ddl.auto">validate</prop>
<prop key="hibernate.c3p0.min_size">2</prop>
<prop key="hibernate.c3p0.max_size">3</prop>
<prop key="hibernate.c3p0.timeout">300</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">3000</prop>
</props>
</property>
</bean>
<bean id="dataSource" scope="singleton"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#127.0.0.1:1529:VIOLET" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
It works fine until I add one more XML mapping file as follows:
<property name="mappingResources">
<list>
<value>mappings/File1.hbm.xml</value>
<value>mappings/File2.hbm.xml</value>
</list>
</property>
The added mapping "File2.hbm.xml" is not affected at all. I even try to set the class in "File2.hbm.xml" with an invalid name but there is no error shown in Hibernate (When I do that in "File1.hbm.xml", there is exception).
Could you tell me why the mapping "File2.hbm.xml" is not affected? I use Eclipse and Tomcat, does it cache some where. I already tried to clean Tomcat and restart my PC but it does not help.
Thank you in advance!
The problem is Tomcat cached the old config file. After cleaning (Server -> Clean Tomcat Work Directory), the new file takes effect.
I try to run a spring project using LocalSessionFactory, I get a null pointer 'cause I've to init the classLoader. Any way I got this exception at the end!
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactoryClassLoader' of bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Bean property 'sessionFactoryClassLoader' is not writable or has an invalid setter method.
Does the parameter type of the setter match the return type of the getter?
any suggestions?
Here is the implementation:
<?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: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">
<!-- Data Source Declaration -->
<bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/Database" />
<property name="username" value="postgres" />
<property name="password" value="password" />
</bean>
<!-- Session Factory Declaration -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource"></property>
<property name="mappingLocations">
<list>
<value>classpath:META-INF/products.xml
</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
<property name="sessionFactoryClassLoader" ref="portletClassLoader" />
</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.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="portletClassLoader" class="com.liferay.portal.kernel.portlet.PortletClassLoaderUtil" factory-method="getClassLoader" />
</beans>
by the way I want to replace com.liferay.portal.kernel.portlet.PortletClassLoaderUtil by another classLoader using springFramework or hibernate Libraries!
sessionFactoryClassLoader is not a property of org.springframework.orm.hibernate3.LocalSessionFactoryBean.
It is available for PortletSessionFactory implementation which extends SessionFactoryImpl.
I currently have Spring configured to use HSQL, but I would like to use MySQL. What needs to change?
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/>
<property name="username" value="monty"/>
<property name="password" value="indian"/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>uk.co.vinoth.spring.domain.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="myUserDAO" class="uk.co.vinoth.spring.dao.UserDAOImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<bean name="/user/*.htm" class="uk.co.vinoth.spring.web.UserController" >
<property name="userDAO" ref="myUserDAO" />
</bean>
</beans>
Change the following properties:
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/schema"/>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
(where 'schema' is your mysql database name, assuming your mysql host is 127.0.0.1)
And add the mysql-connector jar to your classpath.
You'd have to change the driverClass and url properties of the DataSource.
driverClass should be com.mysql.jdbc.Driver
url should be jdbc:mysql://localhost:3306/dbname
A good example of using mysql with java in spring is detailed on the mysql website
The hibernate should be as noted in the answer by ivy.