Spring Hibernate Integration Example:
Here is a code for Spring and Hibernate integration in Netbeans 8.0.2.
Create Project:
File–>New Project –>Java –>Web Application
Give a name to the application and click Next–>Next–>
Choose Spring and Hibernate in Frameworks and choose the database for Hibernate. and click Finish
Create 2 packages inside Source Package and name them as,
1) Dao
2) pojo
Inside pojo, create 2 java class
1) Student.java
2) insert.java
3) Right click pojo –>New–>HibernateUtil.java and click Finish
Inside Dao, Create a java class
1) StudentDao.java
student table (MySQL)
create table student ( id int primary Key Not Null, name varchar(20), Department varchar(20) )
Right Click Source packages, New–> Hibernate Reverse Engineering –> select the table –> Add –>Finish
Right click pojo –>New–>Hibernate Mapping files and Pojo’s from Database–>Finish
Student.java
@Transactional public class Student implements java.io.Serializable { private int id; private String name; private String department; public Student() { } public Student(int id) { this.id = id; } public Student(int id, String name, String department) { this.id = id; this.name = name; this.department = department; } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getDepartment() { return this.department; } public void setDepartment(String department) { this.department = department; } }
StudentDao.java
@Transactional(readOnly = false) public class StudentDao { public StudentDao() { } HibernateTemplate template; public HibernateTemplate getTemplate() { return template; } public void setTemplate(HibernateTemplate template) { this.template = template; } public void saveStudent(Student student) { template.save(student); } public void deleteStudent(Student student) { template.delete(student); } public Student getById(int id) { Student s=(Student)template.get(Student.class,id); return s; } public List<Student> getStudents() { List<Student> list=new ArrayList(); list=template.loadAll(Student.class); return list; } }
applicationContext.xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="url"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="mysessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mappingResources"> <list> <value>Student.hbm.xml</value> </list> </property> </bean> <tx:annotation-driven transaction-manager="txManager"/> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" name="txManager"> <property name="sessionFactory" ref="mysessionFactory" /> </bean> <bean id="template" class="org.springframework.orm.hibernate4.HibernateTemplate"> <property name="sessionFactory" ref="mysessionFactory"></property> </bean> <bean id="stud" class="Dao.StudentDao"> <property name="template" ref="template"> </property> </bean>
hibernate.cfg.xml
<hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibercrud</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <mapping resource="/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
insert.java
public class insert { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); StudentDao sdao=(StudentDao)ac.getBean("stud"); Student s=new Student(); s.setName("Victor"); s.setDepartment("Advance"); s.setId(1011); sdao.saveStudent(s); System.out.println("----------------------------"); System.out.println("delete"); Scanner input=new Scanner(System.in); System.out.println("Enter the id: "); int n=input.nextInt(); Student s1=new Student(); s1.setId(n); sdao.deleteStudent(s1); System.out.println("Get BY ID"); Student s2=new Student(); Scanner input1=new Scanner(System.in); int j=input1.nextInt(); s2=sdao.getById(j); System.out.println(s2.getName()); System.out.println(s2.getDepartment()); System.out.println("----------------------------"); System.out.println("All Details"); for(Student show1:sdao.getStudents()) { System.out.println(show1.getName()); } } }
Output:
(insert some sample values in your student table – I have inserted a name with victor 3 times with different id’s)