Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Implementing DAO | Fundamentals of Hibernate
Java Data Manipulation with Hibernate

Challenge: Implementing DAO

Pyyhkäise näyttääksesi valikon

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:

  1. Set up the database connection in hibernate.cfg.xml;
  2. Implement the interfaces in the implementation classes;
  3. Implement the necessary methods in the implementation classes;
  4. 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;
  5. Don't forget to use HibernateUtil;
  6. 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;
Hint
expand arrow
  1. Use the code from the previous chapter as a hint.
  2. Don't forget to change the data in hibernate.cfg.xml.
  3. Don't forget about the command for SQL Workbench.
  4. You can use main to test your solution.
Solution
expand arrow
// 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);
        }
    }
}
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 6

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Osio 2. Luku 6
some-alt