Hibernate Named Query - join 3 tables

前端 未结 2 575
刺人心
刺人心 2021-01-20 13:59

I have 3 beans: Organization, Role, User

Role - Organization relation - @ManyToOne

Role - User relation - @ManyToMany

Organization :



        
2条回答
  •  情话喂你
    2021-01-20 14:45

    @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 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 |
    +---------+---------+
    

提交回复
热议问题