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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:18:18+00:00 2026-06-15T19:18:18+00:00

I have implemented Spring/Hibernate’s OpenSessionInView Pattern for Lazy Loading. I am facing either No

  • 0

I have implemented Spring/Hibernate’s OpenSessionInView Pattern for Lazy Loading. I am facing either “No Sessoon” or “2 or more sessions” issue. Let me know If I am missing any steps:

Here are detail code fragments:

Web.XML

      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
      </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

      <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <init-param>
          <param-name>singleSession</param-name>
          <param-value>false</param-value>
        </init-param>
        <init-param>
          <param-name>flushModeName</param-name>
          <param-value>FLUSH_AUTO</param-value>
        </init-param>
        <init-param>
          <param-name>sessionFactoryBeanName</param-name>
          <param-value>sessionFactory</param-value>
        </init-param>
      </filter>


       <filter-mapping>
         <filter-name>hibernateFilter</filter-name>
         <url-pattern>/*</url-pattern>
         <dispatcher>REQUEST</dispatcher>
         <dispatcher>FORWARD</dispatcher>
       </filter-mapping>

applicationContext.xml

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan"
        value="com.mypack.common.model, com.mypack.school.model" />
        .
        .
        <!-- Other properties -->
        .
</bean>

customer.xhtml

XHTML user interface has autocomplete box showing customer code. Autocomplete query make use of CompleteCustomer Method, whereas getCustomerbyCode is called by converter bean.

Upon selection of cust code, screen is populated by selected customer details. Application User is allowed to make changes and save updates in DB.

CustomerBean.java

@ManagedBean (name="customerBean")
@ViewScoped
public class CustomerBean implements Serializable {

    public List<Customer> completeCustomer(String query) {
        List<Customer> suggestions;

        suggestions = (List<Customer>) customerService.getCustomerslikeCodeOrName(query, authenticationBean.getTenantId());

        return suggestions;
    }

    public void saveCustomerDetails() {
        // Save or Update User Information in Database
        customerService.saveCustomer(custMaster);
        return;            
    }

CustomerServiceImpl.java

@Service("customerService")
@Transactional(readOnly = true, propagation=Propagation.REQUIRED)
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerDAO custMasterDAO;

    @Override
    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    public void saveCustomer(Customer custDetails) {
        custMasterDAO.saveCustomer(custDetails);
    }

    @Override
    public Customer getCustomerbyCode(String custCode, TenantId tenantId) {
        return custMasterDAO.getCustomerbyCode(custCode, tenantId);
    }

    @Override
    public List<Customer> getCustomerslikeCodeOrName(String custIDName, TenantId tenantId) {
        return custMasterDAO.getCustomerslikeCodeOrName(custIDName, tenantId);
    }
}

CustomerMasterDAO.java

    @SuppressWarnings("unchecked")
    @Override
    public List<Customer> getCustomerslikeCodeOrName(String custIDName, TenantId tenantId) {
        List<Customer> listCustomers = null;
        custIDName = "%" + custIDName + "%";

        Session session = SessionFactoryUtils.getSession(sessionFactory, false);
        Transaction tx = session.beginTransaction();

        try {   
            query = session.createQuery("from Customer " +
                    "where (custId like :codename " +
                    "or custFirstName like :codename " +
                    "or custMiddleName like :codename " +
                    "or custLastName like :codename)")
                .setParameter("codename", custIDName);
            listCustomers = query.list();

        } catch(Exception ex) {
            tx.rollback();
            System.out.println("*** getCustomerslikeCodeOrName ***" + ex.getMessage());
        }

        return listCustomers;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Customer getCustomerbyCode(String custCode, TenantId tenantId) {
        List<Customer> list = null ;
        Query query;

        Session session = SessionFactoryUtils.getSession(sessionFactory, Boolean.FALSE);
        Transaction tx = session.beginTransaction();

        Filter sessionFilter = session.enableFilter("BankRecordFilter");
        sessionFilter.setParameter("bankCode", tenantId.getBankCode());

        try {
            query = session.createQuery("from Customer where custId = :custCode");
            list = query.setParameter("custCode", custCode).list();
        } catch (Exception ex) {
            tx.rollback();
            System.out.println("*** getCustomerbyCode ***" + ex.getMessage());
        }
        if (list == null || list.size() < 1){
//          System.out.println("No Records Found");
            return null;
        }
        return (Customer)list.get(0);
    }

    @Override
    public void saveCustomer(Customer custDetails) {
        Session session = getSessionFactory().getCurrentSession();  
        Transaction tx = session.beginTransaction();

        try {
            session.saveOrUpdate("bkCustomer", custDetails);
            session.getTransaction().commit();

        } catch (Exception e) {
            session.getTransaction().rollback();
            e.printStackTrace();
        }

    }

Table definations are stored in .hbm.xml files.

I have tried lot of combinations of placing following statements to get session:
session = getSessionFactory().getCurrentSession();
Session session = SessionFactoryUtils.getSession(sessionFactory, Boolean.FALSE);

Also transaction in different ways and different location. I am either getting error messages for No Session or Two or more sessions are attached error message.

Can you please let me know what is wrong with my code.

Regards,

Shirish

  • 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-15T19:18:19+00:00Added an answer on June 15, 2026 at 7:18 pm

    Finally I am able to use OpensessionInView. After spending lot of time on this issue, I observed that LAZY loading was working as per expection, but when I am trying to save date, Session was not properly propagted.

    After updating propagation strategy, code started working.. Here is updated code

    @Transactional(readOnly = false,propagation = Propagation.NESTED)
    @Override
        public void saveCustomer(Customer custDetails) {
            Session session = getSessionFactory().getCurrentSession();  
            Transaction tx = session.beginTransaction();
    
            try {
                session.saveOrUpdate("bkCustomer", custDetails);
                session.getTransaction().commit();
    
            } catch (Exception e) {
                session.getTransaction().rollback();
                e.printStackTrace();
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented spring security to protect sections of our website. I am using
I'm a newbie in Spring Batch. I have inherited a batch process implemented with
Well, I have implemented a distinct query in hibernate. It returns me result. But,
I'm pretty much a newb with spring-hibernate and I have been trying to make
I have an application that uses Hibernate/JPA, with Spring and Jersey. In my application
I have an issue with getting session on anonymus inner class in hibernate with
I have spring/hibernate web app. I use Hibernate to implement almost all of my
I have a project coded using Spring-hibernate-activeMq What I would like to know is
I have a web application that using Struts + Spring + Hibernate . In
I have a database (in postgres) and I'm working with spring 3 and hibernate

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.