Hibernate doesn't start - java

I have the following hibernate config on spring and the server starts after a long time but doesn't connect to DB (no schema on DB). So It was supose to give a error message or create the schema with <prop key="hibernate.hbm2ddl.auto">create</prop>.
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method = "close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://dburl:3306"/>
<property name="user" value="user"/>
<property name="password" value="pass!"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>waf/resources/User.hbm.xml</value>
<value>waf/resources/Post.hbm.xml</value>
<value>waf/resources/Position.hbm.xml</value>
<value>waf/resources/Comment.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<!-- C3P0 CONNECTION POOL -->
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="c3p0.acquire_increment">1</prop>
<prop key="c3p0.idle_test_period">100</prop>
<prop key="c3p0.max_size">20</prop>
<prop key="c3p0.max_statements">50</prop>
<prop key="c3p0.min_size">1</prop>
<prop key="c3p0.timeout">10</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
Can you guys help me out?

Hibernate does not create schemas with hbm2ddl.auto. It just creates | creates-drop | etc tables.

Going through the HBM files, you have given as below..
Validate instead of Create
I hope this will not create the DDL. can you please check that? Or is it a typo in question?

Adding to what others have proposed if you are using connection pool (which you are as c3po is mentioned) then while the session Factory is created it will try to use the connection pool backed datasource which in turn will connect to database to pre-create and pool connection. When you say that it does not connect to database - how do you know that ? Is there error in logs? I have seen that if Hibernate session factory is not able to configure itself it throws an error in the logs.

Related

How to create a hibernate session bean for both Annotated and hbm.xml configured entities

I have legacy hbm.xml configured persistent classes and Annotated persistent classes, so currently we have to specify per which session factory is being used in the DAO bean.
Unfortunately, that means that I'm running into an issue where we have a mix of DAO's that I'd like to use, aren't all associated to a single session factory.
I'd like to combine both into a single session factory bean since we can't just move everything over all at once.
How would I go about doing this?
Note: my current workaround is to Annotate some of the xml configured ones and then create two DAO beans: one for each Session Factory.
HBM.XML:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingLocations">
<value>hbm/path/*.hbm.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
Annotated:
<bean id="annotatedSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="path.batch.persistent"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
Thanks M.Deinum and orid:
I was overthinking this.
There was never a need for two session factories.
So I just had to refactor the context file to:
<bean id="annotatedSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="path.batch.persistent"/>
<property name="mappingLocations">
<value>hbm/path/*.hbm.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>

Exit program on Spring jpa/Hibernate connection exception

I want my program to exit if it cannot connect to the database on startup. Currently this connection is setup using the following:
application-context.xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${db.driver}</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="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="packagesToScan" value="com.template" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.ejb.naming_strategy">${naming_strategy}</prop>
<prop key="hibernate.connection.autoReconnect">true</prop>
<prop key="hibernate.connection.autoReconnectForPools">true</prop>
<prop key="hibernate.connection.check-valid-connection-sql">SELECT 1</prop>
<prop key="hibernate.connection.failOverReadOnly">false</prop>
<prop key="hibernate.connection.maxReconnects">${maxreconnects}</prop>
<prop key="hibernate.connection.initialTimeout">${reconnect.interval}</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
Repository:
#Repository
public interface Repository extends CrudRepository<Object, String> {
}
It's not obvious where I should place code to catch the runtime exceptions created by the connection failure. Are there any other settings I can use to exit if the database doesn't exist.
Usually, spring will stop, if it could not create a bean at startup. If DB connection fails, then it will stop automatically anyway. Do you want to catch that exception, and do something before exit ?

JPA all the objects remain stored in EntityManager

I have a web application created with Spring data, which uses JpaRepository (org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean with org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter) after some server uptime I have noticed that heap is growing all the time.
After taking a heap dump it shows that heap contains much more entities than I am currently using.
So my question is how to manage EntityManager to clear entities that are not being used any more?
Here is jpa configuration:
<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" p:database="${jdbc.databaseType}"
p:generateDdl="true" p:showSql="false"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="hibernate.ejb.naming_strategy">com.example.db.repository.FixedDefaultComponentSafeNamingStrategy</prop>
<prop key="hibernate.connection.CharSet">utf8</prop>
<prop key="hibernate.connection.characterEncoding">utf8</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager" p:entityManagerFactory-ref="entityManagerFactory">
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
</bean>

Using Spring Util Schema to Override Bean Settings In App Context

First: yes, I've seen the docs.
They tell me how to create a util:list, util:set, etc. I get that part.
However, I have a library with an application context that contains a bean (specifically a Hibernate Session Factory bean) with settings I'd like the option of overriding. Several services use this library, not every service needs the same annotated classes.
The session factory bean currently looks something like this:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
</props>
</property>
<property name="annotatedClasses" >
<list>
<value>com.example.model.Person</value>
<value>com.example.model.Section</value>
</list>
</property>
</bean>
I would like to replace the annotatedClasses property with a list defined like this (in the app context of the service using the library):
<util:list id="serviceSpecificAnnotatedClasses">
<value>com.example.model.Person</value>
<value>com.example.model.Section</value>
<value>com.example.model.Location</value>
</util:list>
Do I simply have to name the util:list "annotatedClasses" and it will be automatically overriden?
No it wont automatically be overriden. You would have to declare the bean and wire it by default.
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
</props>
</property>
<property name="annotatedClasses" ref="annotatedClasses" />
</bean>
<util:list id="annotatedClasses">
<value>com.example.model.Person</value>
<value>com.example.model.Section</value>
<value>com.example.model.Location</value>
</util:list>
No others can simply override the list annotatedClasses.
But why not simply use a property-placeholder to specify the classes and add a comma delimited list to a properties file?
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
</props>
</property>
<property name="annotatedClasses" value="${service.annotatedClasses}" />
</bean>
Assuming that each service has its own property file for configuration they simply need to add the value for service.annotatedClasses.

separate mappingResources from spring hibernate LocalSessionFactoryBean

Ok, I have this spring hibernate xml configuration.
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/abc/def/a.xml</value>
<value>com/abc/def/b.xml</value>
<value>com/abc/def/c.xml</value>
<!--
And so on, about 50 xml for example
How can I separate list value above into 5 file for example?
ex I have h1.xml (or h1.txt) that contain
<value>com/abc/def/a.xml</value>
<value>com/abc/def/b.xml</value>
I have h2.xml (or h2.txt) that contain
<value>com/abc/def/c.xml</value>
<value>com/abc/def/d.xml</value>
so the mappingResources just read from the files (more than 1) than contain all mapping objects
-->
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
</bean>
I have commented the question in the xml configuration above.
Thanks
You can just use
<property name="mappingLocations">
<list>
<value>classpath:com/abc/def/*.xml</value>
</list>
</property>

Categories