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 613553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:00:56+00:00 2026-05-13T18:00:56+00:00

I’m trying to implement a Hibernate persistence layer in a Java application and I’m

  • 0

I’m trying to implement a Hibernate persistence layer in a Java application and I’m having some trouble with it. I’m encountering a no proxy error every time I attempt to access a simple, one way association. I haven’t implemented Hibernate in quite the right way – I am using the thread method of Session control, which they do suggest you don’t use for production. However, they use it in their tutorials. I am still trying to get the basics working, so I figured following the tutorials would be fine. However, I can’t get the simple association to work. I have a class that looks something like this:

public class Foo {
    private Long id;
    private String name;
    private Bar bar;

    // Static Handler Methods - I'll flesh these out further down in the question.
    public static List getAllFoo();


    // Convenience methods
    public String getBarName() {
        return bar.getName();
    }

    // Accessor methods
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Bar getBar() {
        return bar;
    }

    public void setBar(Bar bar) {
        this.bar = bar;
    }
}

Foo’s Bar class is implemented as a simple one-way association. It’s in the .hbm.xml like so:

<many-to-one name="bar" column="bar_id" not-null="true" />

I create the Foo’s in a static method inside Foo like so:

public static List getAllFoo() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction t = session.beginTransaction();
    List foos = session.createCriteria(Foo.class).list();
    t.commit();
    return foos;
}

The hibernate instance is configured to use a connection pool of 1 and uses the threading method for session handling like so:

<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>

I’m encountering a exception every time I try to access the association using the getBarName() function on one of the created objects. It’s a proxy error, it looks like this:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:132)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:174)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
    at Bar_$$_javassist_7.getName(Bar_$$_javassist_7.java)
    at Foo.getBarName(Bar.java:126)

So to make a long story short, this is what I started with and the error I first encountered. Since then I’ve spent the last several days reading the Hibernate docs and posts here to try and figure out how to make this work.

What I’ve learned – I think – is that Hibernate objects need to be connected to a Session. Namely, the Session they are created by. When I am creating my objects, in getAllFoo() that is the Session that those objects relate to. In that session the Bar proxy exists and makes sense. However, when I call t.commit() – because I am using the Thread method of Session handling – I am ending that Session. The result of this is that when I go to call bar.getName() later, bar is now an orphaned proxy. It is a proxy who’s session has been closed.

I found two possible solutions:

1) don’t close the initial session. Leave it open by not calling t.commit() in getAllFoo().

This worked – however, I don’t think I can use it. I’m going to have thousands of these objects loaded at once. They are going to need to stay open for an extended period of time while the user has think time – but I need to be able to access the associations freely. And there may, in the future, be concurrency issues with database locks.

2) reattach the object to a new Session before calling on the association.

This didn’t work. Maybe I did it wrong – the documentation I found isn’t clear. I attempted to start a new session and call session.load(this, this.id) before I accessed the association. Like this:

public Bar getBar() {
    Bar tmp = null;
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction t = session.beginTransaction();
    session.load(this, this.id);
    tmp = bar;
    t.commit();
    return tmp;
}

I then modified getBarName() to call getBar() instead of accessing bar. This resulted in a new error:

org.hibernate.PersistentObjectException: attempted to load into an instance that was already associated with the session: [Foo#1]

I guess even after days of reading tutorials, posts and the hibernate documentation I still can’t wrap my head around this structure. So I ask StackOverflow to shed some light on this.

First, if you can figure it out, what’s happening in the code I currently have? Is the session opened or closed? Why does it have a proxy error in the first method, but claim the session is still opened and the object still attached in the second?

Second, what is the correct way to handle Sessions – how do I actually make this work? The way I think I want to use is session per request – that seems to be the most popular and applicable to my situation. But then how is that actually implemented with associations and lazy loading?

Yes, I know I shouldn’t be using the thread method – but I have a whole other batch of questions related to JTA vs Thread vs Managed and this question is already too long.

  • 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-13T18:00:56+00:00Added an answer on May 13, 2026 at 6:00 pm

    I believe Hibernate defaults to lazy initialization. So while you loaded the list of Foos in the session, none of their associated Bars are loaded until any attempts to use them. By that time, you’ve already closed the session, which results in the error.

    Some potential solutions:

    • enable eager fetching on Foo-Bar association
    • “join” on the fetch (effectively an eager fetch)
    • try to access the Bar within the session (this will work, but you’re calling Foo.getBar() just to get the associated Bar object loaded)

    Update from comments:

    Idiom for session/transaction management:

    Session sess = factory.openSession();
    Transaction tx;
      try {
        tx = sess.beginTransaction();
        //do some work
        ...
        tx.commit();
      }
      catch (Exception e) {
        if (tx!=null) tx.rollback();
          throw e;
      }
      finally {
        sess.close();
      }
    

    To reattach the “detached” objects (based on reference docs):

    if object has been modified: update the object in new session

    // in the first session
    Cat cat = (Cat) firstSession.load(Cat.class, catId);
    Cat potentialMate = new Cat();
    firstSession.save(potentialMate);
    
    // in a higher layer of the application
    cat.setMate(potentialMate);
    
    // later, in a new session
    secondSession.update(cat);  // update cat
    secondSession.update(mate); // update mate
    

    if object is unmodified, can use lock():

    //just reassociate:
    sess.lock(fritz, LockMode.NONE);
    //do a version check, then reassociate:
    sess.lock(izi, LockMode.READ);
    //do a version check, using SELECT ... FOR UPDATE, then reassociate:
    sess.lock(pk, LockMode.UPGRADE);
    
    • 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'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.