Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 . Open up your MySQL database interface using your preferred method. 2 . Create a database called lab 7 db , it will contain
Open up your MySQL database interface using your preferred method.
Create a database called labdb it will contain one table called students Fields
are: id varchar and primary key firstname varchar lastname
varchar and email varchar
Truncate the database table to ensure it is empty.
Download the hibernatereleaseFinal.zip or the hibernaterelease
Final.rar file, from the Lab Support folder available through the Google
Classroom
Create a new project entitled HibernateLab
Extract all files from the hibernatereleaseFinal.zip file, and add all jars
to your projects Classpath, following the SETUP HIBERNATE
PROJECT tutorial, available in the Google Classroom, if you havent already
done so
Create a new package called factories.
Create a new class called SessionFactoryBuilder inside the factories package.
Inside the SessionFactoryBuilder create a private static attribute of type
SessionFactory orghibernate.SessionFactory and two public static methods
called getSessionFactory accepts no parameters and returns a
SessionFactory; and closeSessionFactory accepts no parameters and
returns nothing
Complete the getSessionFactory method by first checking if the SessionFactory
attribute is null, if it is then instantiate it using the following statement: new
ConfigurationconfigurehibernatecfgxmladdAnnotatedClass
StudentclassbuildSessionFactory;
orghibernate.cfgConfiguration Complete the method by returning the
factory object.
Complete the closeSessionFactory method by checking if the SessionFactory
attribute is not null, if it is then using the attribute, call its close method.
Inside the src folder create a new XML file and call it hibernate.cfgxml In
Eclipse Right Click on the Projects src folder New Other XML folder
XML File Next name the file as indicated above Finish.
Please view the image of an hibernate.cfgxml file at the end of this document
for the exact contents. The contents of the file were explained in the lecture
session.
Create a new package called models.
Create a new class called Student inside the models package, with four private
attributes: id first name, last name and email.
Create a default constructor, primary constructor, accessors, mutators and
toString methods for the Student class.
Add the methods and contents seen in the image below to your Student class.
Similar methods were explained to you in the lecture session.
AnnotateAdd annotations to the attributes of the Student class, to map them to
the database table columns. This was explained to you in the lecture session.
Prepared by: Christopher Panther November Page of
Updated: March
DO NOT include the following in your annotations:
This is only needed for primary keys that are autogenerated, and may cause a
problem when sending data to the database server.
Create a main package.
Create a driver class inside the main package with a main method. Inside the
main method instantiate your Student class and call the following public
methods in the stated order: create, readAll, update, readAll, delete, readAll.
After calling the methods call the closeSessionFactory method from the
SessionFactoryBuilder class.
NB You will need to use the Scanner class to accept user input for student
creation, update and delete. Only update and delete a user that you know exists.
Please use to foreach loop when iterating over the List object and display each
individual student.
Run your application.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started