Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:05:24+00:00 2026-06-18T09:05:24+00:00

I have written this service class for database CRUD operations. visible public class CompanyServiceImpl

  • 0

I have written this service class for database CRUD operations.

visible public class CompanyServiceImpl extends PersistentRemoteService implements CompanyService {

    private GileadHibernateUtil gileadHibernateUtil;

    public CompanyServiceImpl() {
        gileadHibernateUtil = new GileadHibernateUtil();
        setBeanManager(gileadHibernateUtil.getPersistentBeanManager());
    }

    @Override
    public void addCompany(Company newCompany) {
        Objects.requireNonNull(newCompany, "newCompany is null.");

        Session session = gileadHibernateUtil.getCurrentSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.persist(newCompany);
            session.getTransaction().commit();
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
            session.close();
        }
    }

    @Override
    public Company updateCompany(Company company) {
        Objects.requireNonNull(company, "company is null.");

        Session session = gileadHibernateUtil.getCurrentSession();
        Transaction transaction = null;
        Company managedCompany = null;

        try {
            transaction = session.beginTransaction();
            managedCompany = (Company) session.merge(company);
            session.getTransaction().commit();
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
        }

        return managedCompany;
    }

    @Override
    public void deleteCompany(Company company) {
        Objects.requireNonNull(company, "company is null.");

        Session session = gileadHibernateUtil.getCurrentSession();
        Transaction transaction = null;

        try {
            transaction = session.beginTransaction();
            session.delete(company);
            session.getTransaction().commit();
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
        }

    }

    @Override
    public Company findCompany(int companyId) {
        if (companyId <= 0) {
            throw new IllegalArgumentException("companyId must be a positive integer.");
        }

        Session session = gileadHibernateUtil.getCurrentSession();
        Transaction transaction = null;
        Company company = null;

        try {
            transaction = session.beginTransaction();
            company = (Company) session.get(Company.class, companyId);
            session.getTransaction().commit();
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
        }
        return company;
    }

    @Override
    public List<Company> findMatchingCompanies(String companyName) {

        Session session = gileadHibernateUtil.getCurrentSession();
        Transaction transaction = null;
        List<Company> matchingCompanies = null;

        try {
            transaction = session.beginTransaction();
            Criteria searchCriteria = session.createCriteria(Company.class).add(
                    Restrictions.or(Restrictions.like("code", companyName), Restrictions.like("name", companyName)));
            session.getTransaction().commit();

            matchingCompanies = searchCriteria.list();
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
        }

        return matchingCompanies;
    }

    @Override
    public List<Company> findAllCompanies() {

        Session session = gileadHibernateUtil.getCurrentSession();
        Transaction transaction = null;
        List<Company> companies = null;

        try {
            transaction = session.beginTransaction();
            Criteria searchCriteria = session.createCriteria(Company.class);
            session.getTransaction().commit();

            companies = searchCriteria.list();
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
        }
        return companies;
    }

}

I decided to not use a separate DAO layer. Its a small app and I am not going to switch from Hibernate. All it would have included was Hibernate session commands. Beginning and ending a transaction still comes in business layer. So, Hibernate pretty much works as the DAO layer for me.
What’s pinching me is the seemingly boilerplate code for transactions, which has to be written in every CRUD method.
Is this any better way, or is this the minimum amount of code that is going to be repeated no matter what?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:05:25+00:00Added an answer on June 18, 2026 at 9:05 am

    If you are using spring and are able to use annotations. There is a good article on the link provided which shows how to use annotations to wrap a method in a transaction.

    http://springinpractice.com/2008/03/18/annotation-based-transactions-in-spring/

    using spring and hibernate together would make your code look cleaner and would look something like what is below.

    @Override
    @Transactional
    public void addCompany(Company newCompany) {
        Objects.requireNonNull(newCompany, "newCompany is null.");
        Session session = gileadHibernateUtil.getCurrentSession();
        session.persist(newCompany);
        //session.close(); this line would also not be needed because the session would close automatically when the transaction is complete.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a resteasy-spring service : @Controller @RequestMapping(/abr/asd) @Path(/ab/ac) public class JobSchedulerService {
have written this little class, which generates a UUID every time an object of
i have written this tree class for a familytree now i need a search
I have written this clone method for when the parent of the Employee class
I have a .NET window Service which extend a custom base class, this serves
I have written an ASP.NET web service. It looks like this: WebServices.logic pLogic =
I have written a criteria query in a Grails service class where I expect
I have written this code inside a servlet to delete certain records from three
I have written this code in JavaScript and works perfectly fine when I include
I have written this function to auto correct gender to M or F from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.