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

  • SEARCH
  • Home
  • 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 1103853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:24:00+00:00 2026-05-17T01:24:00+00:00

I’m writing a JSF 2.0 application with Hibernate and Postgresql database. My problem is

  • 0

I’m writing a JSF 2.0 application with Hibernate and Postgresql database. My problem is quite poor understanding on how to handle session with Hibernate when inserting data into more than one table at a time. I have a method savePerson. The method is called as many times as there are addresses submitted by a user but the object Person is only created once. The method savePerson then calls the method addAddress. The code looks like this:

public Person savePerson(Person p, String address) {

        Transaction tx = null;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        if (p == null) {
             = new Person();

            try {
                tx = session.beginTransaction();
                session.save(p);
                tx.commit();
            } catch (RuntimeException e) {
                if (tx != null && tx.isActive()) {
                    try {

                        tx.rollback();
                    } catch (HibernateException e1) {
                    }

                    throw e;
                }
            }
        }

        int id = p.getId();
        addAddress(4, id, address);

        return p;
    }

And the method addAddress:

public void addAddress(int table_id, int row_id, String address) {

        Transaction tx = null;
        session = HibernateUtil.getSessionFactory().openSession();
        Address a = new Address(table_id, row_id, address);

        try {
            tx = session.beginTransaction();
            session.save(a);
            tx.commit();
        } catch (RuntimeException e) {
            System.out.println("first try");

            if (tx != null && tx.isActive()) {
                System.out.println("first try if");
                try {
                    System.out.println("first try if try");

                    tx.rollback();
                } catch (HibernateException e1) {

                }
                // throw again the first exception
                throw e;
            }
            throw e;
        }

But I get the following error:

org.hibernate.TransactionException: Transaction not successfully started

So what is the proper way of opening and closing session when inserting the data into more than one table at a time?

Best Regards,
sass.

  • 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-05-17T01:24:01+00:00Added an answer on May 17, 2026 at 1:24 am

    A TransactionException indicates that a transaction could not be begun, committed or rolled back. But providing the full stacktrace would probably helpful to fully understand the problem.

    That being said, while you can create multiple (serial) Transaction from a single Session, there is IMO a major flaw in your design. If you need to insert both the Person and the Address, you should very likely do it in a single unit of work. With your current approach, adding an Address can fail and leave a freshly inserted Person in an inconsistent state.

    So what is the proper way of opening and closing session when inserting the data into more than one table at a time?

    Do it in a single transaction.

    And if you want to remove the transaction management from your methods, you should consider using the Open Session In View pattern, as suggested by BalusC. Unless you want finer grained transaction control of course.

    Resources

    • Sessions and transactions
    • Open Session in View
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I want to construct a data frame in an Rcpp function, but when I
I am writing an app with both english and french support. The app requests
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in
I am using Paperclip to handle profile photo uploads in my app. They upload
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.