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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:49:08+00:00 2026-05-11T02:49:08+00:00

How to get Hibernate session inside a Hibernate Interceptor? I’m trying to use Hibernate

  • 0

How to get Hibernate session inside a Hibernate Interceptor?

I’m trying to use Hibernate to enforce data access by organization id transparently. I have set a global Filter to filter all queries by organization id. Now, I need to use an Entity interceptor to set Organizational Id on all Entities before Save/Update.

The organization id comes from HttpSession

I’ve set Organizational Id as a Filter property in Hibernate session which i want to retrieve inside my interceptor and use for all Inserts and Updates as well. The problem is i dont seem to have access to Session inside the Interceptor. Any workarounds for this?

  • 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. 2026-05-11T02:49:08+00:00Added an answer on May 11, 2026 at 2:49 am

    You can, but I would use a simple POJO just to keep things cleanly separated. Keep in mind that the value stored in the singleton will only be accessible by the same thread that handled the servlet request, so if you’re doing any asynch, you will need to account for that. Here’s a super basic impl:

    public class OrgId {    public static ThreadLocal<Integer> orgId = new ThreadLocal<Integer>(); } 

    Since the Organizational Id is resident in the session, you could set the value of the ThreadLocal in an early servlet filter like this (not much error checking):

    public class OrgIdFilter implements Filter {    public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain) throws java.io.IOException, javax.servlet.ServletException {       int orgId = 0;       HttpServletRequest req = (HttpServletRequest) servletRequest;       HttpSession session = req.getSession();       orgId = Integer.parseInt(session.getAttribute('OrganizationalIdAttr'));       try {          OrgId.orgId.set(orgId);          filterChain.doFilter(servletRequest, servletresponse);       } finally {          OrgId.orgId.set(null); // Important to clear after request !!       }    } } 

    This assumes that the orgId is in the session when the filter is called, but if not, you get the idea….

    Then in your interceptor (or pretty much anywhere) you can get the thread’s current orgId with:

    OrgId.orgId.get();   // Might be null..... 

    A potential snafu here is that all these components (filter, OrgId and interceptor) need to be loaded by the same class loader to ensure that the OrgId class is effectively a singleton, otherwise, with multiple instances of the ThreadLocal hanging around it won’t work consistently, or at all. Needless to say, all this needs to be happening in the same VM.

    I am not sure if this is the cleanest way to solve this problem, but it does get you your orgId where you need it.

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

Sidebar

Related Questions

I want to get jdbc connection from hibernate session.There is method inside hibernate session
I am trying to inject a hibernate session to use with hibernate search and
If I save an entity using a Hibernate session, and then probe to get
I am trying to get hibernate to write new objects each time I do
I am looking for a way to get hibernate to use oracle's SYS_GUID() function
I have the following code and I am still unable to get Hibernate to
(see here for the problem I'm trying to solve) How do you get hibernate
I'm trying to get started with Hibernate, and when executing my program I get
I'm trying to build a Hibernate Criteria query to find entities that have a
We need to be able to get the associated java.sql.Connection of a hibernate session.

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.