Spring ORM - JPA Example
JPA Example
Mapping Java objects to database tables and vice versa is called Object-relational mapping(ORM). The Java Persistence API (JPA) is one possible approach to ORM.
-
JPA is a specification and several implementations are available. Popular implementations are Hibernate, EclipseLink and Apache OpenJPA.
-
JPA permits the developer to work directly with objects rather than with SQL statements.
-
The mapping between Java objects and database tables is defined via persistence metadata. JPA metadata is typically defined via annotations or xml files.
Spring Data JPA API provides JpaTemplate class to integrate spring application with JPA.
1.Student.java : It is a simple POJO class
package smlcodes;
public class Student {
private int sno;
private String name;
private String address;
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Student(int sno, String name, String address) {
super();
this.sno = sno;
this.name = name;
this.address = address;
}
public Student() {
super();
}
}
2.Student.xml: This mapping file contains all the information of the persistent class
<entity-mappings version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd ">
<entity class="smlcodes.Student">
<table name="student"></table>
<attributes>
<id name="sno">
<column name="sno" />
</id>
<basic name="name">
<column name="name" />
</basic>
<basic name="address">
<column name="address" />
</basic>
</attributes>
</entity>
</entity-mappings>
3.StudentDao.java : DAO Class
@Transactional
public class StudentDao {
JpaTemplate template;
public void setTemplate(JpaTemplate template) {
this.template = template;
}
public void saveStudent(int sno, String name, String address) {
Student student = new Student(sno, name, address);
template.persist(student);
}
public void updateStudent(int sno, String name) {
Student student = template.find(Student.class, sno);
if (student != null) {
student.setName(name);
}
template.merge(student);
}
public void deleteStudent(int sno) {
Student student = template.find(Student.class, sno);
if (student != null)
template.remove(student);
}
public List<Student> getAllStudents() {
List<Student> students = template.find("select s from student s");
return students;
}
}
META-INF/persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="jppPU" transaction-type="RESOURCE_LOCAL">
<mapping-file>smlcodes/Student.xml</mapping-file>
<class>smlcodes.Student</class>
</persistence-unit>
</persistence>
SpringConfig.xml
<beans>
<tx:annotation-driven transaction-manager="jpaTxnManagerBean" proxy-target-class="true"/>
<bean id="dataSourceBean" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/springdb" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="emfBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSourceBean"></property>
<property name="jpaVendorAdapter" ref="hbAdapterBean"></property>
</bean>
<bean id="jpaTemplateBean" class="org.springframework.orm.jpa.JpaTemplate">
<property name="entityManagerFactory" ref="emfBean"></property>
</bean>
<bean id="studentsDaoBean" class="smlcodes.StudentDao">
<property name="template" ref="jpaTemplateBean"></property>
</bean>
<bean id="jpaTxnManagerBean" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emfBean"></property>
</bean>
</beans>
StudentJPAExample.java
public class StudentJPAExample {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("SpringConfig.xml");
StudentDao studentsDao = context.getBean("studentsDaoBean", StudentDao.class);
studentsDao.saveStudent(101, "Satyar", "HYDERABAD");
studentsDao.saveStudent(105, "RAJESH", "BANGLORE");
System.out.println("Students created");
studentsDao.updateStudent(105, "KARTHICK");
System.out.println("Student Name updated");
List<Student> students = studentsDao.getAllStudents();
for (Student s : students) {
System.out.println(s.getSno() + " : " + s.getName() + " , " + s.getAddress());
}
studentsDao.deleteStudent(111);
System.out.println("Student deleted");
}
}