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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:09:30+00:00 2026-06-06T23:09:30+00:00

i have a problem with Hiberante and Spring. When i get an entity everything

  • 0

i have a problem with Hiberante and Spring.

When i get an entity everything works, but if I use a subproperty the lazy load fails due to a close session… why hibernate close session so early? Can not wait to exit the service or take one session per thread?

This is my configuration

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <list>
            <value>com.cinebot.db.entity</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

This is my dao:

@Transactional
public class Dao {


    @Autowired
    private SessionFactory sessionFactory;

    public Session getSession(){
        return sessionFactory.getCurrentSession();
    }

    @SuppressWarnings("unchecked")
    public <T> T get(Class<T> classe, Serializable id) throws Exception {
        if(id==null) return null;
        T obj = (T) getSession().get(classe, id);
        return obj;
    }

}

and this is where i get the error (getEventi() is lazy loaded) inside a @Service class:

    Spettacoli spettacolo = dao.get(Spettacoli.class, spettacoloId);
   if(spettacolo.getEventi().getScadenza()>0) throw new LogicalException("Spettacolo scaduto");
  • 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-06T23:09:35+00:00Added an answer on June 6, 2026 at 11:09 pm

    You are accessing the entity outside the transaction.. You need to mark service method as transactional.

    In your current code your transaction ends when your dao method completes so when you access that entity in your service method it is detached entity which will certainly thorw exception.

    Remember you need to start transaction in service and not DAO which will allow you to access child entities. So move @transactional annotation to your service method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with a query using hibernate. The entity class EntityClass has
I have a problem on the following situation: In my Spring, Hibernate application I
I have a problem removing the parent entity from the database. The code looks
I have a Spring/JPA/Hibernate application and am trying to get it to pass my
I have figured out that I can use hibernate to get the sum of
I am using Spring 3.0.5, Hibernate 3.6.7 and Vaadin. I have a simple entity
I'm new in Spring Framework (2.5.6) and I have Hibernate integration problem on Glassfish
I have problem and don't know how to get some clues on what might
Problem is i have two bags in my entity which i would like to
I have a problem with Hibernate Validator 3.1.0.GA not looking up custom validation messages

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.