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

  • Home
  • SEARCH
  • 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 6922589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:25:33+00:00 2026-05-27T10:25:33+00:00

I have 3 beans: Organization, Role, User Role – Organization relation – @ManyToOne Role

  • 0

I have 3 beans: Organization, Role, User

Role – Organization relation – @ManyToOne

Role – User relation – @ManyToMany

Organization :

    @Entity
    @Table(name = "entity_organization")
    public class Organization implements Serializable {

        private static final long serialVersionUID = -646783073824774092L;

        @Id
        @GeneratedValue(strategy = GenerationType.TABLE)
        Long id;

        String name;

        @OneToMany(targetEntity = Role.class, mappedBy = "organization")
        List<Role> roleList;

...

Role :

    @Entity
    @Table(name = "entity_role")
    public class Role implements Serializable {

        private static final long serialVersionUID = -8468851370626652688L;

        @Id
        @GeneratedValue(strategy = GenerationType.TABLE)
        Long id;

        String name;

        String description;

        @ManyToOne
        Organization organization;

...

User :

    @Entity
    @Table(name = "entity_user")
    public class User implements Serializable {

        private static final long serialVersionUID = -4353850485035153638L;

        @Id
        @GeneratedValue(strategy = GenerationType.TABLE)
        Long id;
        @ManyToMany
        @JoinTable(name = "entity_user_role",
                joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
                inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName =                     "id"))
        List<Role> roleList;

...

So I need to get all Organizations for specified User ( first I need to select all user roles and than select all organizations that have this roles)

I have an sql statement that realizes this logic ( for e.g. I choose user with id = 1):

SELECT * FROM entity_organization AS o 
INNER JOIN entity_role r ON r.organization_id = o.id 
INNER JOIN entity_user_role ur ON ur.role_id=r.id 
WHERE ur.user_id = 1

How can I implement this, using hibernate named query mechanism?
Thanks!

  • 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-27T10:25:33+00:00Added an answer on May 27, 2026 at 10:25 am

    @NamedQuery

    I’ve created the following @NamedQuery on the Organization entity class.

    @NamedQuery(name = "query", query = "SELECT DISTINCT o " +
        "FROM Organization o, User u " +
        "JOIN o.roles oRole " +
        "JOIN u.roles uRole " +
        "WHERE oRole.id = uRole.id AND u.id = :uId")
    public class Organization { ...
    

    (I used standard JPA annotations, but my provider was Hibernate.)

    Test

    This is the test I ran.

    EntityManager em = ...
    TypedQuery<Organization> q = em.createNamedQuery("query", Organization.class);
    q.setParameter("uId", 1); // try it with 1L if Hibernate barks about it
    for (Organization o : q.getResultList())
      System.out.println(o.name);
    

    Using the tables and sample data below, this outputs

    A
    B
    

    Please see if it works for you.

    Tables

    CREATE TABLE `organization` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      PRIMARY KEY (`id`)
    );
    
    CREATE TABLE `role` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `description` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `organization_id` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`)
    );
    
    CREATE TABLE `user` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (`id`)
    );
    
    CREATE TABLE `user_has_role` (
      `user_id` int(11) NOT NULL DEFAULT '0',
      `role_id` int(11) NOT NULL DEFAULT '0',
      PRIMARY KEY (`user_id`,`role_id`)
    );
    
    ALTER TABLE `role` ADD CONSTRAINT `cst_organization_id` 
      FOREIGN KEY `fk_organiztaion_id` (`organization_id`)
        REFERENCES `organization` (`id`);
    

    (I’ve used a bit different from yours, but it shouldn’t matter too much.)

    Sample data

    `organization`
    +----+------+
    | id | name |
    +----+------+
    |  1 | A    |
    |  2 | B    |
    +----+------+
    
    `role`
    +----+------+-------------+-----------------+
    | id | name | description | organization_id |
    +----+------+-------------+-----------------+
    |  1 | A    | a           |               1 |
    |  2 | B    | b           |               1 |
    |  3 | C    | c           |               2 |
    +----+------+-------------+-----------------+
    
    `user`
    +----+
    | id |
    +----+
    |  1 |
    |  2 |
    |  3 |
    +----+
    
    `user_has_role`
    +---------+---------+
    | user_id | role_id |
    +---------+---------+
    |       1 |       1 |
    |       1 |       2 |
    |       1 |       3 |
    |       2 |       1 |
    |       3 |       1 |
    |       3 |       3 |
    +---------+---------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Value-Objects/Beans (only containing members, no logic): public class Parent { String first;
I have a large application that uses EJB 2.x entity beans (BMP). This is
We have a Hibernate/Spring application that have the following Spring beans: <bean id=transactionManager class=org.springframework.orm.hibernate3.HibernateTransactionManager
I've created a Dynamic Web Project in Eclipse. I have 2 beans, 1 @Entity
let's say I have a factory bean: <bean id=myFactory class=com.company.MyFactory lazy-init=true> <property name=myProperty ref=propA>
I have two beans which comes from the same class(id1 and id2), the difference
For example i have two beans: class Bean1 { private SomeService service1; private SomeService
I have the following beans Task, ServerDetails and ApplicationDetails. I wish to retrieve all
After following the great advice given in a thread about service beans I have
I have a List of beans, each of which has a property which itself

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.