I created a jpa xml EntityManagerFactory. After running tomcat, and Hibernate create table in console, but when I checked the database , I didn't see the table. What's the problem ?
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
<property name="packagesToScan" value="com.example.hospitalmanage.model"></property>
</bean>
Ensure all models are in package com.example.hospitalmanage.model
User
user_diagnosis
user_treatments
user_videos
All should be inside the same package.
I need a solution about tomcat datasource configuration.
I have 2 nginx front of my tomcats.These are feeding my app, these are Webservice.
Tomcat machines and database are in different ip blog so its going firewall in each request.
When i started my tomcats everythigs well.But for example after 10 hour our after 20 hour later everythings going bad.That tomcats can not set connection.All service down.I share my datasource file and hope you find some solution or some other suggestions other than datasource file.
http://www.springframework.org/schema/beans ----
http://www.springframework.org/schema/context/spring-context.xsd">
<property name="connectionProperties">
<props merge="default">
<prop key="v$session.program">ws_${server}</prop>
</props>
</property>
<property name="connectionCacheProperties">
<props merge="default">
<prop key="MinLimit">1</prop>
<prop key="MaxLimit">120</prop>
<prop key="InitialLimit">5</prop>
<prop key="MaxStatementsLimit">50</prop>
<prop key="ConnectionWaitTimeout">30</prop>
<prop key="InactivityTimeout">600</prop>
<prop key="AbandonedConnectionTimeout">180</prop>
<prop key="PropertyCheckInterval">300</prop>
<prop key="ValidateConnection">true</prop>
<prop key="TimeToLiveTimeout">600</prop>
</props>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="nativeJdbcExtractor">
<bean class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" />
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="com.ws.oracle.pool.JndiExporter">
<property name="bean" ref="dataSource" />
<property name="jndiName" value="dsName" />
</bean>
I just upgraded my project's hibernate version to 5.0.0.FINAL. But than I realise that, I am getting this warning. And I want to get rid of it. I don't know if it will effect my application or not.
2015-08-24 14:29:22.235 WARN --- [ main] org.hibernate.orm.deprecation : HHH90000003: Use of DOM4J entity-mode is considered deprecated
Since I never used entity-mode explicitly, I searched online but there is almost no information about it. Here is the EntityMode enum. Since, there is no DOM4J mode any more, I am suspecting that I might get an error in production if I continue to use hibernate in version 5.0.0.
I am also using envers with hibernate. If I disable envers the warning also disappears.I am using spring alongside with hibernate and envers. And here is the versions of them.
<spring.version>4.2.0.RELEASE</spring.version>
<hibernate.version>5.0.0.Final</hibernate.version>
<hibernate.envers.version>5.0.0.Final</hibernate.envers.version>
<hibernate-jpa-2.1-api.version>1.0.0.Final</hibernate-jpa-2.1-api.version>
<project.java.version>1.8</project.java.version>
And here is my hibernate-jpa 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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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">
<bean id="commonsEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="commonDataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.ejb.interceptor">com.examples.dao.utils.AbstractEntityInterceptor</prop>
<!--<prop key="hibernate.listeners.envers.autoRegister">false</prop>-->
<prop key="hibernate.implicit_naming_strategy">org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl</prop>
<prop key="hibernate.physical_naming_strategy">org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.showSql">${hibernate.showSql}</prop>
<prop key="hibernate.formatSql">${hibernate.formatSql}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
<prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
<prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="org.hibernate.envers.audit_table_suffix">${org.hibernate.envers.audit_table_suffix}</prop>
<prop key="javax.persistence.sharedCache.mode">${javax.persistence.sharedCache.mode}</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.examples.entity</value>
</list>
</property>
</bean>
<bean id="commonsTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="commonsEntityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="commonsTransactionManager"/>
<context:component-scan base-package="com.examples.dao.*"/>
</beans>
And here is an example entity.
#Entity
#Table(name = "T_USER")
#Access(AccessType.FIELD)
#Audited
#Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "C_USERNAME", unique = true)
private String username;
#Column(name = "C_PASSWORD")
private String password;
#Column(name = "C_EMAIL")
private String email;
// Getters && Setters etc
}
Update
I created a project on github that demonstrates this behaviour. After debugging a little, I found out that the warning message is created on ModelBinder#L2441.
Here is the sample code:
public class ModelBinder
...
private void bindProperty(
MappingDocument mappingDocument,
AttributeSource propertySource,
Property property) {
property.setName( propertySource.getName() );
if ( StringHelper.isNotEmpty( propertySource.getName() ) ) {
// Here is the line that print outs the log I was mentioned
DeprecationLogger.DEPRECATION_LOGGER.logDeprecationOfDomEntityModeSupport();
}
...
}
}
And when I looked into the value of mappingDocument.getOrigin(), It was Origin(name=envers,type=OTHER). So I still suspecting that envers is causing this warning.
By the way, If you remove #Audit annotation, or use the property I was mentioned, this warning still disappears.
I think that those messages are cause by bug in ModelBinder. Instead of getName should be getXmlNodeName. I've reported that issue and I hope that this will be fixed in next release. Anyway, besides extra log lines, there are no any other consequences.
I use the same env just like #bhdrkn
<!-- Jpa Entity Manager 配置 -->
<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="database">
<bean factory-method="getDatabase" class="net.gelif.core.persistence.Hibernates">
<constructor-arg ref="dataSource"/>
</bean>
</property>
</bean>
</property>
<property name="packagesToScan">
<array>
<value>net.gelif.**.entity</value>
</array>
</property>
<property name="sharedCacheMode" value="ENABLE_SELECTIVE"/>
<property name="jpaProperties">
<props>
<prop key="#{T(org.hibernate.cfg.AvailableSettings).HBM2DDL_AUTO}">update</prop><!-- hibernate.hbm2ddl.auto -->
<prop key="#{T(org.hibernate.cfg.AvailableSettings).DEFAULT_ENTITY_MODE}">pojo</prop><!-- hibernate.default_entity_mode -->
<prop key="#{T(org.hibernate.cfg.AvailableSettings).CURRENT_SESSION_CONTEXT_CLASS}">thread</prop><!-- hibernate.current_session_context_class -->
<prop key="#{T(org.hibernate.cfg.AvailableSettings).CACHE_REGION_FACTORY}">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop><!-- hibernate.cache.region.factory_class -->
<prop key="#{T(org.hibernate.cfg.AvailableSettings).USE_SECOND_LEVEL_CACHE}">true</prop><!-- hibernate.cache.use_second_level_cache -->
<prop key="#{T(org.hibernate.cfg.AvailableSettings).USE_QUERY_CACHE}">true</prop><!-- hibernate.cache.use_query_cache -->
<prop key="#{T(org.hibernate.cfg.AvailableSettings).SHOW_SQL}">false</prop><!-- hibernate.show_sql -->
<prop key="#{T(org.hibernate.cfg.AvailableSettings).MAX_FETCH_DEPTH}">3</prop><!-- hibernate.max_fetch_depth -->
<prop key="#{T(org.hibernate.cfg.AvailableSettings).IGNORE_EXPLICIT_DISCRIMINATOR_COLUMNS_FOR_JOINED_SUBCLASS}">false</prop><!-- hibernate.discriminator.ignore_explicit_for_joined -->
<prop key="#{T(org.hibernate.cfg.AvailableSettings).USE_NEW_ID_GENERATOR_MAPPINGS}">true</prop><!-- hibernate.id.new_generator_mappings -->
<prop key="#{T(org.hibernate.jpa.AvailableSettings).VALIDATION_MODE}">none</prop><!-- javax.persistence.validation.mode -->
<prop key="#{T(org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory).NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME}">cache/ehcache-hibernate-local.xml</prop><!-- net.sf.ehcache.configurationResourceName -->
<prop key="#{T(org.hibernate.envers.configuration.EnversSettings).AUDIT_TABLE_PREFIX}"></prop><!-- org.hibernate.envers.audit_table_prefix 配置数据修改记录表名的前缀规则 默认值:空 -->
<prop key="#{T(org.hibernate.envers.configuration.EnversSettings).AUDIT_TABLE_SUFFIX}">_audit</prop><!-- org.hibernate.envers.audit_table_suffix 配置数据修改记录表名的后缀规则 默认值:_AUD -->
<prop key="#{T(org.hibernate.envers.configuration.EnversSettings).REVISION_FIELD_NAME}">revision</prop><!-- org.hibernate.envers.revision_field_name 配置数据修改记录表版本号字段名 默认值: REV -->
<prop key="#{T(org.hibernate.envers.configuration.EnversSettings).REVISION_TYPE_FIELD_NAME}">revision_type</prop><!-- org.hibernate.envers.revision_type_field_name 配置数据修改记录表修改类型字段名 默认值: REVTYPE . 0表示新增加,1修改 2删除 -->
<prop key="#{T(org.hibernate.envers.configuration.EnversSettings).REVISION_ON_COLLECTION_CHANGE}">true</prop><!-- org.hibernate.envers.revision_on_collection_change 配置是否支持关联表修改时记录修改记录 默认值:true-->
<prop key="#{T(org.hibernate.envers.configuration.EnversSettings).DO_NOT_AUDIT_OPTIMISTIC_LOCKING_FIELD}">true</prop><!-- org.hibernate.envers.do_not_audit_optimistic_locking_field 配置是否不对乐观锁字段修改时记录修改记录,即使用(#Version)字段 默认值:true-->
<prop key="#{T(org.hibernate.envers.configuration.EnversSettings).STORE_DATA_AT_DELETE}">true</prop><!-- org.hibernate.envers.store_data_at_delete 配置是否在删除操作时,只保存id值还是全部的值。 默认值:false 只记录id-->
<prop key="#{T(org.hibernate.envers.configuration.EnversSettings).DEFAULT_SCHEMA}"></prop><!-- org.hibernate.envers.default_schema 配置数据修改记录表的schema 默认值:null-->
<prop key="#{T(org.hibernate.envers.configuration.EnversSettings).DEFAULT_CATALOG}"></prop><!-- org.hibernate.envers.default_catalog 配置数据修改记录表的catalog 默认值:null-->
</props>
</property>
</bean>
<!-- Spring Data Jpa配置, 扫描base-package下所有继承于Repository<T,ID>的接口 -->
<jpa:repositories base-package="net.gelif.ems.**.dao.**" repository-impl-postfix="Impl" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="jpaTransactionManager"/>
<!-- 事务管理器配置, Jpa单数据源事务 -->
<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="jpaTransactionManager" proxy-target-class="true"/>
and while I removed the hibernate envers setting and #Audit from entities, these warning disappeared.
This problem is fixed a new hibernate version as released here: with http://in.relation.to/2015/09/30/hibernate-orm-502-final-release/.
See https://hibernate.atlassian.net/browse/HHH-10115
After an upgrade the problem should be solved.
I have a problem integrating Spring, Hibernate, and Apache DBCP. I have used the DBCPConnectionProvider from here.
I have the following xml configuration for the above said.
<context:component-scan base-package="com.my.app"/>
<tx:annotation-driven/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>com.my.app.model</value>
</list>
</property>
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
<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">update</prop>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/app</prop>
<prop key="hibernate.connection.username">foo</prop>
<prop key="hibernate.connection.password">bar</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.DBCPConnectionProvider</prop>
<!-- Connection pooling related properties -->
<prop key="hibernate.dbcp.initialSize">8</prop>
<prop key="hibernate.dbcp.maxActive">20</prop>
<prop key="hibernate.dbcp.maxIdle">5</prop>
<prop key="hibernate.dbcp.minIdle">0</prop>
<prop key="hibernate.dbcp.maxWait">10000</prop>
<prop key="hibernate.dbcp.minEvictableIdleTimeMillis">180000</prop>
<prop key="hibernate.dbcp.timeBetweenEvictionRunsMillis">180000</prop>
<prop key="hibernate.dbcp.testWhileIdle">true</prop>
<prop key="hibernate.dbcp.testOnReturn">true</prop>
<prop key="hibernate.dbcp.validationQuery">select 1</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
When the database schema i.e. app is empty two tables will be created in mysql. But I am getting NullPointerException in the getConnection() method of the DBCPConnectionProvider that I mentioned. That means the dataSource is not instantiated. I think I have configured everything in the xml. What am I missing. How do you configure DBCP version 1.4 with Spring and Hibernate? Please provide your insights.
Here is the stack trace:
Caused by: java.lang.NullPointerException
at org.hibernate.connection.DBCPConnectionProvider.getConnection(DBCPConnectionProvider.java:209)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:555)
... 82 more
Don't do it like that. Configure the datasource you want to use in Spring as well as Hibernate. Ditch the hibernate.dbcp and hibernate.connection properties.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/app"/>
<property name="username" value="foo"/>
<property name="password" value="bar"/>
// Other DBCP properties here
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"
<property name="packagesToScan">
<list>
<value>com.my.app.model</value>
</list>
</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">update</prop>
</props>
</property>
</bean>
Just add the dataSource property to your AnnotationSessionFactoryBean as dependency and done. Note you don't need the configurationClass property as it is already annotation based.
A tip I wouldn't suggest using Commons-DBCP anymore as a datasource instead take a look at HikariCP as a better datasource implementation.
For more information in integrating/configuring Hibernate with Spring I suggest this section of the Reference Guide.
So here it goes..
I have configured two jndi in my glassfish server for communicating two different databases.And in my applicationContext.xml I have following configuration
<!-- Database connection 1 STARTS HERE-->
<jee:jndi-lookup id="JNDI1" jndi-name="jdbc/db1"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="JNDI1" />
<!-- <property name="annotatedClasses">
<list>
<value>com.test.db1.Person</value> //THIS WORKS FINE
</list>
</property>-->
<property name="packagesToScan" value="com.test.db1" /> //THIS IS STRANGE
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
</bean>
<!-- Database connection 1 ENDS HERE-->
<!-- Database connection 2 STARTS HERE-->
<jee:jndi-lookup id="JNDI2" jndi-name="jdbc/db2" />
<bean id="sessionFactory2"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="JNDI2" />
<property name="packagesToScan" value="com.test.db2" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
</bean>
<!-- Database connection 2 ENDS HERE-->
In package com.test.db1 I have only Person class and in com.test.db2 package I have two entities named Employee and Company.
The problem occurs when I use packageToScan property in both SessionFactory beans all 3 tables are created in both databases.And when I explicitly define entity classes using annotatedClasses property tables are generated in relevant databases as configured in jndi.I am not getting why this kind of results I am getting while using packageToScan.
I'll be grateful If someone can explain me in detail.
Thank you