/java/

Spring3 + Hibernate 3

2013-02-20 00:36:20

Generate tables when Tomcat start


<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

Important part is load-on-startup = 1

Extending Entities

@Entity
@Table(name="post")
@Inheritance(strategy = InheritanceType.JOINED)
public class Post implements Serializable{
...
}

@Entity
@Table(name="article")
public class Article extends Post{
...
}

important is @Inheritance(strategy = InheritanceType.JOINED)

Configuration Spring3 + Hibernate3

Only 3 files :)

WEB-INF/spring/mvc-config.xml

&lt; ?xml version="1.0" encoding="UTF-8"?&gt;

<beans beans="" context="" http:="" mvc="" schema="" spring-beans.xsd="" spring-context-3.1.xsd="" spring-mvc-3.1.xsd&quot;="" www.springframework.org="" xmlns='"http://www.springframework.org/schema/beans"' xmlns:context='"http://www.springframework.org/schema/context"' xmlns:mvc='"http://www.springframework.org/schema/mvc"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsi:schemalocation='"http://www.springframework.org/schema/beans'>
<mvc:annotation -driven=""></mvc:annotation>
<mvc:view -controller="" path='"/"' view-name='"home"'></mvc:view>
<bean class='"org.springframework.web.servlet.view.InternalResourceViewResolver"'>
<property name='"prefix"' value='"/WEB-INF/views/"'></property>
<property name='"suffix"' value='".jsp"'></property>
<property name='"contentType"' value='"text/html;charset=utf-8"'></property>
</bean>
</beans>

WEB-INF/spring/app-config.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<beans beans="" context="" http:="" lang="" schema="" spring-beans.xsd="" spring-context-3.1.xsd="" spring-lang-3.1.xsd="" spring-util-3.1.xsd&quot;="" util="" www.springframework.org="" xmlns='"http://www.springframework.org/schema/beans"' xmlns:context='"http://www.springframework.org/schema/context"' xmlns:lang='"http://www.springframework.org/schema/lang"' xmlns:util='"http://www.springframework.org/schema/util"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsi:schemalocation='"http://www.springframework.org/schema/beans'>
<context:component-scan base-package='"com.louis.core"/'>
</context:component-scan></beans>

WEB-INF/spring/database-config.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<beans beans="" context="" http:="" schema="" spring-beans.xsd="" spring-context-3.1.xsd="" spring-tx-3.1.xsd&quot;="" tx="" www.springframework.org="" xmlns='"http://www.springframework.org/schema/beans"' xmlns:context='"http://www.springframework.org/schema/context"' xmlns:tx='"http://www.springframework.org/schema/tx"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsi:schemalocation='"http://www.springframework.org/schema/beans'>
<context:annotation-config></context:annotation-config>
<!-- DataSource -->
<bean class='"org.springframework.jdbc.datasource.DriverManagerDataSource"' id='"dataSource"'>
<property name='"driverClassName"' value='"com.mysql.jdbc.Driver"'></property>
<property name='"url"' value='"jdbc:mysql://localhost/java_louis6"'></property>
<property name='"username"' value='"root"'></property>
<property name='"password"' value='"root"'></property>
</bean>
<!-- Hibernate 3 -->
<bean class='"org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"' id='"sessionFactory"'>
<property name='"dataSource"' ref='"dataSource"'></property>
<property name='"annotatedClasses"'>
<list>
<value>com.louis.core.model.Post</value>
<value>com.louis.core.model.Article</value>
</list>
</property>
<property name='"configurationClass"' value='"org.hibernate.cfg.AnnotationConfiguration"'></property>
<property name='"namingStrategy"' ref='"namingStrategy"'></property>
<property name='"packagesToScan"' value='"com.louis.core.model"'></property>
<property name='"hibernateProperties"'>
<props>
<prop key='"hibernate.show_sql"'>true</prop>
<prop key='"hibernate.dialect"'>org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key='"hibernate.hbm2ddl.auto"'>update</prop>
</props>
</property>
</bean>
<!-- Transaction management -->
<tx:annotation-driven></tx:annotation-driven>
<bean class='"org.springframework.orm.hibernate3.HibernateTransactionManager"' id='"transactionManager"'>
<property name='"sessionFactory"' ref='"sessionFactory"'></property>
</bean>
<bean class='"org.hibernate.cfg.ImprovedNamingStrategy"' id='"namingStrategy"'></bean>
</beans>