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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:28:39+00:00 2026-06-07T07:28:39+00:00

i got a problem by using CrudRepository. Example: i have two entities, entity A

  • 0

i got a problem by using CrudRepository. Example: i have two entities, entity A has a collection of entity B.

class A {
  int id;
  int name;
  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  Set<B> bs;
  // getters and setters
}
class B {
  int id;
  int name;
  @ManyToOne(mappedBy="bs")
  A a;
  // getters and setters
}

then i got 2 repositories.

class ARepository extends CrudRepository<A, int>{}
class BRepository extends CrudRepository<B, int>{}

but when i got this, i got a org.hibernate.LazyInitializationException, how can i avoid this?

@Service
@Transactional(readOnly=true)
class ServiceImpl implements Service {
@Resource ARepository ar;

@Override
A a = ar.findOne(int id);
}

here is the applicationContext.xml:

<jpa:repositories base-package="com.myproject.repository" />

<context:component-scan base-package="com.myproject.*" />
<context:annotation-config />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="keep-apm" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="true" />
            <property name="database" value="POSTGRESQL"/>
            <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect"/>
        </bean>
    </property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="username" value="root" />
    <property name="password" value="root" />
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://127.0.0.1:5432/db" />
</bean>
<bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory">
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

here is the web.xml

<filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>singleSession</param-name>
        <param-value>true</param-value>
    </init-param>

</filter>
<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>com.myproject.util.LogLocator</listener-class>
</listener>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

a.bs(the collection) would not be loaded, and always throw out a org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: no session or session was closed

Thank you in advance!!

  • 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-07T07:28:40+00:00Added an answer on June 7, 2026 at 7:28 am

    Here is the solution:

    i can not access the object on my web layer, i should access the lazy loading objects in service layer, which should be in a transaction.

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

Sidebar

Related Questions

I got a probably small problem using memcmp. I have two arrays (length =
I got problem with sending data in Android using httpPost. I found some example,
I got a problem in using listfind() . I have a list of strings.One
I've got this strange problem using py-amqp and the Flopsy module. I have written
I am starting using FMOD API and I have got problem with sound playing.
I have got a little problem using the CodeIgniter route function. I use the
I have got problem with this method: public List<int> menu_wid_w_kat() { DataSet1TableAdapters.menu_widac_wszystkoTableAdapter pk =
I got a problem using Rexexp in Java. The example code writes out ABC_012_suffix_suffix
i'm doing some code now and got some problem using restrict keyword. typedef int*
I've got a problem with using django's {% trans %} template function. I have

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.