ExCode
ExCode

Reputation: 444

Spring jpa(Hibernate) with tomcat

I'm working with JPA(Hibernate) and spring project these days, I configured spring, jpa and try to deploy the application on tomcat7, As I know jpa/hibernate creates tables using entity class first loading, but here I couldn't see any connection between database and application when applications starts, below you can see my configuration,I have seen lot of questions posted regarding this matter, I couldn't find the proper solution to my problem.

Can any one tell me what is the problem here.I have created a application without any issues in jetty server sometime back, Thank you in advance

applicationContext-dao.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: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-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.0.xsd"
       default-lazy-init="true">

    <!-- Use @Transaction annotations for managing transactions  -->
    <!--<tx:annotation-driven/>-->

    <!-- holding properties for database connectivity / -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <tx:annotation-driven proxy-target-class="true" />
    <!-- Activates scanning of @Autowired -->
    <context:annotation-config/>

    <!-- Activates scanning of @Repository -->
    <context:component-scan base-package="lk.gov.elg"/>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          p:driverClassName="${jdbc.driverClassName}"
          p:url="${jdbc.url}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}"/>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
          p:entityManagerFactory-ref="entityManagerFactory"
          p:dataSource-ref="dataSource"/>

    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
          p:dataSource-ref="dataSource"
          p:jpaVendorAdapter-ref="jpaAdapter">
        <property name="persistenceUnitName" value="eLGPersistenceUnit"></property>
        <property name="loadTimeWeaver">
            <bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>
        </property>
    </bean>

    <!-- Jpa adapter for the Hibernate -->
    <bean id="jpaAdapter"
          class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
          p:databasePlatform="org.hibernate.dialect.MySQLDialect"
          p:generateDdl="true"
          p:showSql="true"  >
    </bean>

    <bean
            class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

    <bean
            class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>

persistance.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
    <persistence-unit name="eLGPersistenceUnit"
                      transaction-type="RESOURCE_LOCAL">
    <!--transaction-type="JTA">-->
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="validate"/>
        </properties>
        <!--<provider>org.hibernate.ejb.HibernatePersistence</provider>-->
    </persistence-unit>
</persistence>

Upvotes: 2

Views: 2238

Answers (2)

Solubris
Solubris

Reputation: 3763

try changing:

<property name="hibernate.hbm2ddl.auto" value="validate"/>

to:

<property name="hibernate.hbm2ddl.auto" value="update"/>

or:

<property name="hibernate.hbm2ddl.auto" value="create"/>

Upvotes: 0

123Ex
123Ex

Reputation: 926

I think there is no issue with your configuration, try after removing "default-lazy-init=true" attribute

Cheers

Upvotes: 1

Related Questions