- Overview / Setting Up the Database / Creating the Web Project
- Configuring Spring-MVC
- Configuring Hibernate
- Creating the Table View, Interface, and Implementation Classes
- Creating the Table Controller / Creating the Controller View
Creating the Table View, Interface, and Implementation Classes
In your project's src folder, create a package called com.springMVCHibernate.dt. This example creates a dt package that will hold all the table classes. (You may prefer to arrange your classes and packages in a different way.) Next, create a table class called RegistrationDT in this package with the following code:
package com.springMVCHibernate.dt; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="REGISTRATION") public class RegistrationDT { @Id @Column(name="ID") @GeneratedValue private int idRegistration = 0; @Column(name="NAME") private String name; @Column(name="ADDRESS") private String address; @Column(name="PHONE") private String phone; @Column(name="LOGIN") private String login; @Column(name="PASSWORD") private String password; public int getIdRegistration() { return idRegistration; } public void setIdRegistration(int idRegistration) { this.idRegistration = idRegistration; } 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 String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
In this example, I use Java Persistence API (JPA) annotations to tell Hibernate which fields will map to which table columns. You could also use Hibernate annotations, which are very similar; both use the same Java Enterprise Edition (JEE) specification for annotations—Java Specification Request (JSR) 250.
Next, create the interface and implementation classes that will handle the database calls for the Registration table using Hibernate. These classes are often referred to as Data Access Objects (DAOs) because the classes perform common database operations.
In a new package called com.springMVCHibernate.dao, add the interface class for the Registration table:
package com.springMVCHibernate.dao; import com.springMVCHibernate.dt.RegistrationDT; public interface IRegistrationDAO { public void addRegistration(RegistrationDT registrationDT); }
Now add the implementation class for the Registration table:
package com.springMVCHibernate.dao; import com.springMVCHibernate.dt.RegistrationDT; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class RegistrationDAO implements IRegistrationDAO { RegistrationDT registrationDT = null; private SessionFactory sessionFactory; public void addRegistration(RegistrationDT registrationDT){ sessionFactory = new Configuration() .configure() // configures settings from hibernate.cfg.xml .buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); session.saveOrUpdate(registrationDT); session.getTransaction().commit(); session.close(); } }
Notice that this is where we use Hibernate to add a record to the Registration table. The key to making this work is loading our Hibernate configuration settings into a new configuration object instantiated with the SessionFactory object. From there, we open the session and use the session methods to perform our operations on the database.