JAVA Hibernate FULL STACK DEVELOPMENT
Objectives of this Lab Manual This lab manual includes - Exercises for hands on practice Guidelines to complete the exercise Learning Outcome
Hibernate Environment Setup
Problem What will you do? Statement Set up the environment for the Hibernate framework.
Solution What will be the steps Steps to get the output? • Step-1: Open your IDE and create a maven project. We are creating a maven project because we can easily add the dependencies.
Solution What will be the steps Steps to get the output? • Step-2: Select maven-archetype-quickstart as the archetype. • Step-3: Give your project an appropriate name, group id, and artifact id. Finish the setup and your project will be created.
Solution What will be the steps Steps to get the output? • Step-4: Now, open the pom.xml file to add the dependencies. • Step-5: Now update the project to include the JAR files of mentioned dependencies.
Solution What will be the steps Steps to get the output? • Step-6: Now, as you can see, all the JAR files are added.
Code Code for Step 4: class ExceptionTest { public static void main(String[] args){ } } try { int a,b; a = 10; b = 0; c = a / b; }
Code catch(Exception e) { System.out.println(e); } finally { System.out.println(“Inside finally block”); }
Learning Outcome After Performing this Add teExxt ehrecriese You will be able to: ⦁ Set up Environment for Hibernate and database ⦁ Set up dependencies through a maven project
Creation of a configuration file
Problem What will you do? Statement Create a configuration file to provide information about the environment and the database.
Solution What will be the steps Steps to get the output? • Step-1: Firstly, let us create a database in MySQL Workbench. The name of the database will be hibernate. • Step-2: Then, create an XML file and name it hibernate.cfg.xml. • Step-3: Now, write an XML element to specify that this is an XML file <?xml version=\"1.0\" encoding=\"UTF-8\"?>
Solution What will be the steps Steps to get the output? • Step-4: Mention the DTD for validation of XML file. • Step-5: Create the root element as hibernate configuration. All the other elements will be nested inside this root element. Then, create a child element of the session factory. • Step-6: Create a property element to specify the driver class. • Step-7: Create a property to specify the database URL. • Step-8: Create a property to specify the database username. • Step-9: Create a property to specify the dialect of the queries to be written. • Step-10: Create a property to specify that the tables must be updated. • Step-11: Create a property to specify that SQL query statements should be displayed.
Code Code for Step 3: <?xml version=\"1.0\" encoding=\"UTF-8\"?> Code for Step 4: <!DOCTYPE hibernate-configuration PUBLIC \"-//Hibernate/Hibernate Configuration DTD 3.0//EN\" \"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd\"> Code for Step 5: <hibernate-configuration> <session-factory> </session-factory> </hibernate-configuration>
Code Code for Step 6: <property name = \"connection.driver_class\"> com.mysql.jdbc.Driver</property> Code for Step 7: <property name=\"connection.url\">jdbc:mysql://localhost:3306/hibernate </property> Code for Step 8: <property name=\"connection.username\">root</property> Code for Step 9: <property name=\"dialect\">org.hibernate.dialect.MySQLDialect</property> Code for Step 10: <property name=\"hbm2ddl.auto\">update</property> Code for Step 11: <property name=\"show_sql\">true</property>
Learning Outcome After Performing this Add teExxt ehrecriese You will be able to: ⦁ Create a Configuration File ⦁ Link the database with hibernate
Creation of HBM document
Problem What will you do? Statement • Create an Employee class • Define employee’s id, name, and salary as properties • The employee id will be the primary key • This employee class will be a POJO class • Map this class using an HBM document
Solution What will be the steps Steps to get the output? • Step-1: Firstly, let us create the Employee class. Define id (int type), name (String type), and salary (long type) as properties. Define default constructor and parameterized constructor as well. Define getter and setter methods for the properties. Override toString method to display the student properties in a systematic fashion. • Step-2: Create a root element as hibernate mapping. Create a class element with a name and table attribute to specify the class name and table name. Meta element specifies a brief class description. Id element specifies the primary key i.e., the id property of the class. Specify the generation strategy with the generator element’s class attribute. Finally, specify the remaining properties with the property element. Also, specify the column name in the database and the data type of the property.
Code Code for Step 1: public class EmployeeHBM { private int id; private String name; private long salary; public EmployeeHBM() {} public EmployeeHBM(int id, String name, long salary) { this.id = id; this.name = name; this.salary = salary; }
Code public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; }
Code public void setName(String name) { this.name = name; } public long getSalary() { return salary; } public void setSalary(long salary) { this.salary = salary; }
Code @Override public String toString() { return \"Employee [id=\" + id + \", name=\" + name + \", salary=\" + salary + \"]\"; } }
Code Code for Step 2: <?xml version=\"1.0\" encoding=\"UTF-8\"?> <hibernate-mapping> <class name = \"Employee\" table = \"EMPLOYEE\"> <meta attribute = \"class-description\"> This class contains the employee detail. </meta> <id name = “id\" type = \"int\" column = “emp_id\"> <generator class=\"identity \"/>
Code </id> <property name = “name\" column = “emp_name\" type = \"string\"/> <property name = \"salary\" column = \"salary\" type = \"long\"/> </class> </hibernate-mapping>
Learning Outcome After Performing this Add teExxt ehrecriese You will be able to: ⦁ Create an HBM document ⦁ Map of class through XML file
Creation of a POJO Class
Problem What will you do? Statement Create a POJO class for employees. It should have a default constructor. The properties of the employee class are: ⦁ Employee id of integer type which will be the primary key later in the database ⦁ Employee name of String type ⦁ Employee salary of long type
Solution What will be the steps Steps to get the output? • Step-1: Let us create an employee class and define the properties. • Step-2: Now, let us create a default constructor. • Step-3: Now, let us create getter and setter methods. • Step-4: Finally, close the class brackets and your POJO class is ready.
Code Code for Step 1: public class Employee { private int id; private String name; private long salary; } Code for Step 2: public Employee() { }
Code Code for Step 3: public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; }
Code public void setName(String name) { this.name = name; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; }
Learning Outcome After Performing this Add teExxt ehrecriese You will be able to: Create a POJO class
Creation of an application to find persistent objects
Problem What will you do? Statement Create an Employee class. Define employee’s id, name, and salary as properties. The employee id will be the primary key. Find the mentioned employee by specifying the employee id. Use the get method to fetch the object. Display the object found.
Solution What will be the steps Steps to get the output? • Step-1: Firstly, let us create the Employee class. Define id (int type), name (String type) and salary (long type) as properties. Define default and parameterized constructor as well. Define getter and setter methods for the properties. Override toString method to display the employee’s properties in a systematic fashion. • Step-2: In the configuration file, mention the class to map using the mapping element. • Step-3: Write an application program to find the employee as findEmployee method. • Step-4: Close all the classes and methods brackets and execute the program.
Code Code for Step 1: import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Employee { @Id private int id; private String name; private long salary;
Code public Employee() {} public Employee(int id, String name, long salary) { this.id = id; this.name = name; this.salary = salary; } public int getId() { return id; }
Code public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }
Code public long getSalary() { return salary; } public void setSalary(long salary) { this.salary = salary; } @Override public String toString() { return \"Employee [id=\" + id + \", name=\" + name + \", salary=\" + salary + \"]\"; } }
Code Code for Step 2: <mapping class=\"com.TetchLease.Employee\"/> Code for Step 3: public static void findEmployee() { Configuration cfg = new Configuration(); cfg.configure(\"hibernate.cfg.xml\"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); Transaction t = session.beginTransaction(); Scanner sc = new Scanner(System.in);
Code System.out.println(\"Enter empid to find the record:\"); int empid = sc.nextInt(); Employee e = (Employee) session.get(Employee.class,empid); System.out.println(e); t.commit(); session.close(); }
Input Input for the Addexterxctihserwe ill be The input of the program: Enter empid to find the record: 101
Output Output for the Addexterxctihserwe ill be The output of the program: Employee [id=101, name=Rajesh, salary=200000] Mapping Composite key through annotation (Setting up all types of Mapping)
Learning Outcome After Performing AddttheixstEhxeerercise You will be able to: ⦁ Fetch records in the database ⦁ Use of get method
Setting up composite key mapping through annotation
Problem What will you do? Statement • Create a Student class • Define id and name as properties • Create a Certificate class and define course and university as properties • Cert property i.e., an object of Certificate class will be mapped as a composite key in the Student class
Solution What will be the steps Steps to get the output? • Step-1: Firstly, let us create the Certificate class. Define course (String type) and university (String type) as properties. Define the default constructor and parameterized constructor. Define getter and setter methods for the properties. • Step-2: Next, let us create the Student class. Declare id (int type), and name (String type) as properties. Map certificate class as composite key in this class. Use @EmbeddedId annotation on the object of the Certificate class. Define default constructor and parameterized constructor as well. Define getter and setter methods for the properties. Override toString method to display the student properties in a systematic fashion. • Step-3: In the configuration file, mention the class to map using the mapping element.
Search