Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Department Layers Improvement | Final Touches
Java Data Manipulation with Hibernate

Challenge: Department Layers Improvement

Свайпніть щоб показати меню

Task

This task should be quite simple for you.

Your task is to implement the updateDepartment() method in the DepartmentDaoImpl class and also implement the updateDepartment() and updateDepartmentLocation() methods in the DepartmentServiceImpl class. After completing the task, run the code in the Main class, where the code for testing is already written. There, you can verify the functionality of your methods.

Link to the Task

Hint
expand arrow

Use the same algorithm of actions that was used in the previous chapter. It won't differ at all here.

Solution
expand arrow
@Override
public Department updateDepartment(int departmentId, Department newDepartment) {
    Session session = null;
    Transaction transaction = null;
    Department department = null;

    if (newDepartment == null) {
        throw new NullPointerException("Department is null!");
    }

    try {
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();

        department = getById(departmentId);
        department.setName(newDepartment.getName());
        department.setLocation(newDepartment.getLocation());

        session.merge(department);
        transaction.commit();

    } catch (Exception e) {
        if (transaction != null) {
            transaction.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return department;
}

//DepartmentServiceImpl
@Override
public Department updateDepartment(int departmentId, Department newDepartment) {
    return departmentDao.updateDepartment(departmentId, newDepartment);
}

@Override
public Department updateDepartmentLocation(int departmentId, String newLocation) {
    Department department = getById(departmentId);
    department.setLocation(newLocation);
    updateDepartment(departmentId, department);
    return department;
}
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Challenge: Department Layers Improvement

Task

This task should be quite simple for you.

Your task is to implement the updateDepartment() method in the DepartmentDaoImpl class and also implement the updateDepartment() and updateDepartmentLocation() methods in the DepartmentServiceImpl class. After completing the task, run the code in the Main class, where the code for testing is already written. There, you can verify the functionality of your methods.

Link to the Task

Hint
expand arrow

Use the same algorithm of actions that was used in the previous chapter. It won't differ at all here.

Solution
expand arrow
@Override
public Department updateDepartment(int departmentId, Department newDepartment) {
    Session session = null;
    Transaction transaction = null;
    Department department = null;

    if (newDepartment == null) {
        throw new NullPointerException("Department is null!");
    }

    try {
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();

        department = getById(departmentId);
        department.setName(newDepartment.getName());
        department.setLocation(newDepartment.getLocation());

        session.merge(department);
        transaction.commit();

    } catch (Exception e) {
        if (transaction != null) {
            transaction.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return department;
}

//DepartmentServiceImpl
@Override
public Department updateDepartment(int departmentId, Department newDepartment) {
    return departmentDao.updateDepartment(departmentId, newDepartment);
}

@Override
public Department updateDepartmentLocation(int departmentId, String newLocation) {
    Department department = getById(departmentId);
    department.setLocation(newLocation);
    updateDepartment(departmentId, department);
    return department;
}
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2
some-alt