Challenge: Implementing DAO
Свайпніть щоб показати меню
Let's move on to practice. In the previous chapter, we implemented the DAO layer as well as the Service layer for the Employee entity. In this task, you need to do the same for the Department entity.
Interfaces such as DepartmentDao and DepartmentService have already been created for you. In this task, you should work in implementation classes, such as DepartmentDaoImpl and DepartmentServiceImpl.
Your task is to implement all the methods specified in the interfaces.
Here's the workflow for the task:
- Set up the database connection in
hibernate.cfg.xml; - Implement the interfaces in the implementation classes;
- Implement the necessary methods in the implementation classes;
- You can implement them similarly to how we did it in the previous chapter. It's not difficult. You just need to change the entity names;
- Don't forget to use
HibernateUtil; - Run the integration tests to check your solution.
Note
To ensure that the integration tests work correctly, you need to execute the following command in the MySQL Workbench query. This command will grant permission to delete data from the table for the tests to run correctly. Don't worry. This database is a test database; later, when we test all the necessary methods and are ready to complete the project, we will create another database that will no longer be a test one.
Command:
SET SQL_SAFE_UPDATES = 0;
- Use the code from the previous chapter as a hint.
- Don't forget to change the data in hibernate.cfg.xml.
- Don't forget about the command for SQL Workbench.
- You can use main to test your solution.
// DepartmentDaoImpl
public class DepartmentDaoImpl implements DepartmentDao {
private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
@Override
public Department add(Department department) {
Session session = null;
Transaction transaction = null;
try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
session.persist(department);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
throw new RuntimeException("Can't add new Department", e);
}
} finally {
if (session != null) {
session.close();
}
}
return department;
}
@Override
public Department getById(int id) {
Session session = null;
Department department = null;
try {
session = sessionFactory.openSession();
department = session.get(Department.class, id);
} catch (Exception e) {
throw new HibernateException("Can't get Department by ID " + id, e);
} finally {
if (session != null) {
session.close();
}
}
return department;
}
}
//DepartmentServiceImpl
public class DepartmentServiceImpl implements DepartmentService {
private final DepartmentDao departmentDao = new DepartmentDaoImpl();
@Override
public Department add(Department department) {
return departmentDao.add(department);
}
@Override
public Department getById(int id) {
Department department = departmentDao.getById(id);
if (department != null) {
return departmentDao.getById(id);
} else {
throw new NoSuchElementException("Can't get department by ID " + id);
}
}
@Override
public String getDepartmentNameById(int id) {
Department department = getById(id);
String departmentName = department.getName();
if (departmentName != null) {
return departmentName;
} else {
throw new NullPointerException("The department's name is null, " +
"or there is no name for an department with ID " + id);
}
}
}
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Challenge: Implementing DAO
Let's move on to practice. In the previous chapter, we implemented the DAO layer as well as the Service layer for the Employee entity. In this task, you need to do the same for the Department entity.
Interfaces such as DepartmentDao and DepartmentService have already been created for you. In this task, you should work in implementation classes, such as DepartmentDaoImpl and DepartmentServiceImpl.
Your task is to implement all the methods specified in the interfaces.
Here's the workflow for the task:
- Set up the database connection in
hibernate.cfg.xml; - Implement the interfaces in the implementation classes;
- Implement the necessary methods in the implementation classes;
- You can implement them similarly to how we did it in the previous chapter. It's not difficult. You just need to change the entity names;
- Don't forget to use
HibernateUtil; - Run the integration tests to check your solution.
Note
To ensure that the integration tests work correctly, you need to execute the following command in the MySQL Workbench query. This command will grant permission to delete data from the table for the tests to run correctly. Don't worry. This database is a test database; later, when we test all the necessary methods and are ready to complete the project, we will create another database that will no longer be a test one.
Command:
SET SQL_SAFE_UPDATES = 0;
- Use the code from the previous chapter as a hint.
- Don't forget to change the data in hibernate.cfg.xml.
- Don't forget about the command for SQL Workbench.
- You can use main to test your solution.
// DepartmentDaoImpl
public class DepartmentDaoImpl implements DepartmentDao {
private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
@Override
public Department add(Department department) {
Session session = null;
Transaction transaction = null;
try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
session.persist(department);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
throw new RuntimeException("Can't add new Department", e);
}
} finally {
if (session != null) {
session.close();
}
}
return department;
}
@Override
public Department getById(int id) {
Session session = null;
Department department = null;
try {
session = sessionFactory.openSession();
department = session.get(Department.class, id);
} catch (Exception e) {
throw new HibernateException("Can't get Department by ID " + id, e);
} finally {
if (session != null) {
session.close();
}
}
return department;
}
}
//DepartmentServiceImpl
public class DepartmentServiceImpl implements DepartmentService {
private final DepartmentDao departmentDao = new DepartmentDaoImpl();
@Override
public Department add(Department department) {
return departmentDao.add(department);
}
@Override
public Department getById(int id) {
Department department = departmentDao.getById(id);
if (department != null) {
return departmentDao.getById(id);
} else {
throw new NoSuchElementException("Can't get department by ID " + id);
}
}
@Override
public String getDepartmentNameById(int id) {
Department department = getById(id);
String departmentName = department.getName();
if (departmentName != null) {
return departmentName;
} else {
throw new NullPointerException("The department's name is null, " +
"or there is no name for an department with ID " + id);
}
}
}
Дякуємо за ваш відгук!