To make your Spring MVC program work with Hibernate, Following files need to be modified:
For more info, see: Spring MVC Config
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" /> <beans:property name="url" value="jdbc:mysql://localhost:3306/ns201501" /> <beans:property name="username" value="root" /> <beans:property name="password" value="nobu" /> </beans:bean>
This is a part of servlet-context.xml. In this case, DB is MySQL.
<!-- Hibernate 4 SessionFactory Bean definition --> <beans:bean id="hibernate4AnnotatedSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <beans:property name="dataSource" ref="dataSource" /> <beans:property name="annotatedClasses"> <beans:list> <beans:value>com.ns.spring.model.RMA_HDR</beans:value> <beans:value>com.ns.spring.model.RMA_LINE</beans:value> <beans:value>com.ns.spring.model.RTRN_TP</beans:value> <beans:value>com.ns.spring.model.RTRN_RSN</beans:value> <beans:value>com.ns.spring.model.RTRN_TP_RSN</beans:value> <beans:value>com.ns.spring.model.RTRN_TP_RSN_CMBN</beans:value> <beans:value>com.ns.spring.model.RMA_HDR_STS</beans:value> <beans:value>com.ns.spring.model.MDSE</beans:value> <beans:value>com.ns.spring.model.RmaLinePk</beans:value> <beans:value>com.ns.spring.model.RtrnTpRsnPk</beans:value> <beans:value>com.ns.spring.model.ui.RmaHdrModel</beans:value> <beans:value>com.ns.spring.model.ui.RmaHdrStsModel</beans:value> <beans:value>com.ns.spring.model.ui.RmaLineModel</beans:value> <beans:value>com.ns.spring.model.ui.RtrnRsnModel</beans:value> <beans:value>com.ns.spring.model.ui.RtrnTpModel</beans:value> </beans:list> </beans:property> <beans:property name="hibernateProperties"> <beans:props> <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop> <beans:prop key="hibernate.show_sql">true</beans:prop> </beans:props> </beans:property> </beans:bean>
This part defines the classes for CRUD operation. For example, line 7 is the class for Hibernate object (below) represent the table RMA_HDR.
package com.ns.spring.model; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import com.ns.spring.model.ui.RmaHdrModel; /** * Entity bean with JPA annotations Hibernate provides JPA implementation */ @Entity @Table(name = "RMA_HDR") public class RMA_HDR { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "rma_num") private String rmaNum; @Column(name = "rma_hdr_sts_cd") private String rmaHdrStsCd; @Column(name = "rtrn_tp_cd") private String rtrnTpCd; @Column(name = "rtrn_rsn_cd") private String rtrnRsnCd; @Column(name = "sell_to_cust_cd") private String sellToCustCd; @OneToMany(mappedBy = "rmaHdr", targetEntity = RMA_LINE.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL) private List<RMA_LINE> rmaLines; public RMA_HDR(){} public RMA_HDR(RmaHdrModel hdr) { this.id = hdr.getId(); this.rmaNum = hdr.getRmaNum(); this.rmaHdrStsCd = hdr.getRmaHdrStsCd(); this.rtrnTpCd = hdr.getRtrnTpCd(); this.rtrnRsnCd = hdr.getRtrnRsnCd(); this.sellToCustCd = hdr.getSellToCustCd(); } // getter and setter }
CREATE TABLE `rma_hdr` ( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `rma_num` VARCHAR(20) NOT NULL DEFAULT '', `rma_hdr_sts_cd` VARCHAR(20) NULL DEFAULT NULL, `rtrn_tp_cd` VARCHAR(20) NOT NULL DEFAULT '', `rtrn_rsn_cd` VARCHAR(20) NULL DEFAULT NULL, `sell_to_cust_cd` VARCHAR(20) NULL DEFAULT NULL, PRIMARY KEY (`id`) ) COLLATE='utf8_general_ci' ENGINE=InnoDB AUTO_INCREMENT=27 ;
<!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <!-- Apache Commons DBCP --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.9</version> </dependency>
This is a part of pom.xml. As line 18 to 23, DB is MySQL.