Blog
Spring Hibernate Configuration
DOWNLOAD SOURCE CODE HERE⬇️ (SpringORM Configuration)
This project serves as a fundamental exploration of Spring ORM configuration, designed to provide a foundational comprehension. There are CRUD (Create, Read, Update, Delete) function avilable to play with the application. It will to gain insight into the operations facilitated by the HibernateTemplate class.
pom.xml ⬇️
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springcore.orm</groupId>
<artifactId>springorm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springorm</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring Core dependency-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-context -->
<!-- Mysql dependency -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.2.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>
</project>
config.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
https://www.springframework.org/schema/util/spring-util.xsd">
<tx:annotation-driven/>
<!-- <context:component-scan base-package="com.springcore.config"/> -->
<bean name="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/springorm" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" name="factory">
<property name="dataSource" ref="ds"/>
<property name="hibernateProperties">
<props>
<!-- <prop key="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</prop> -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>
com.springcore.orm.springorm.entity.Student
</value>
</list>
</property>
</bean>
<bean class="org.springframework.orm.hibernate5.HibernateTemplate" name="hibernateTemplate">
<property name="sessionFactory" ref="factory"/>
</bean>
<bean class="com.springcore.orm.springorm.dao.StudentDao" name="studentDao">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" name="transactionManager">
<property name="sessionFactory" ref="factory"/>
</bean>
</beans>
Student.java
⬇️
package com.springcore.orm.springorm.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "student_details")
public class Student {
@Id
@Column(name = "student_id")
private int studentId;
@Column(name = "student_name")
private String studentName;
@Column(name = "student_city")
private String studentCity;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int studentId, String studentName, String studentCity) {
super();
this.studentId = studentId;
this.studentName = studentName;
this.studentCity = studentCity;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentCity() {
return studentCity;
}
public void setStudentCity(String studentCity) {
this.studentCity = studentCity;
}
@Override
public String toString() {
return "Student [studentId=" + studentId + ", studentName=" + studentName + ", studentCity=" + studentCity
+ "]";
}
}
StudentDao.java
⬇️
package com.springcore.orm.springorm.dao;
import java.util.List;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;
import com.springcore.orm.springorm.entity.Student;
public class StudentDao {
private HibernateTemplate hibernateTemplate;
@Transactional
public int insert(Student student) {
Integer i = (Integer) this.hibernateTemplate.save(student);
return i;
}
// get the single data(Object)
@Transactional
public Student getStudent(int studentId) {
if(this.hibernateTemplate!=null) {
Student student = this.hibernateTemplate.get(Student.class, studentId);
return student;
}else {
System.out.print("hibernateTemplate is null");
}
return null;
}
// get All Student Object
public List<Student> getAllStudent() {
List<Student> students = this.hibernateTemplate.loadAll(Student.class);
System.out.println("record has been inserted successfully");
return students;
}
// to delete Student from Database
@Transactional
public void deleteStudent(int studentId) {
Student student = this.hibernateTemplate.get(Student.class, studentId);
this.hibernateTemplate.delete(student);
System.out.println("Student for id "+studentId+" has been deleted");
}
@Transactional
public void updateStudent(Student student) {
this.hibernateTemplate.update(student);
System.out.println("Updated");
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
}
App.java
⬇️
package com.springcore.orm.springorm;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.springcore.orm.springorm.dao.StudentDao;
import com.springcore.orm.springorm.entity.Student;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
StudentDao studentDao = context.getBean("studentDao", StudentDao.class);
studentDao.insert(new Student(10,"Nitin","Delhi"));
}
}
Database Name: sqlorm