0
  menu              menu_autorization                authorization
----------        -----------------------        -----------------------
|id|title|        |id|menu|authorization|        |id|authorization|info|
----------        -----------------------        -----------------------
  |                     |        |                |
  |---------------------|        |----------------|

I mapped this DB with Hibernate; when I get a Menu object from the database I also get, correctly, menu_autorization and authorization objects.

For each Menu object I have many menu_autorization objects.

The query I wrote so far makes me get all the menu objects in the DB; what I want to do is to write a query that makes me get a menu object with a specific menu_autorization.

How can I do it, since I don't have and authorization field in the table menu? Maybe I can do it using Criteria, but I don't know how.

I have this mapping in Menu.class

...
    @OneToMany(mappedBy="menu", fetch=FetchType.EAGER)  
        private Set<MenuAuthorization> menuAuthorization;
...

this mapping in MenuAuthorization.class

  ...

    @ManyToOne(targetEntity=Menu.class)
    @JoinColumn(name="menu",referencedColumnName="id")
    private Menu menu;

    @ManyToOne(targetEntity=Authorizazion.class)  
    @JoinColumn(name="authorizazion",referencedColumnName="id")
    private Authorizazion authorizazion;
        ....

and this in Authorization.class

...
@OneToMany(mappedBy="Authorization", fetch=FetchType.EAGER)
private Set<MenuAuthorization> MenuAuthorization;
...
MDP
  • 4,177
  • 21
  • 63
  • 119
  • 2
    How is the relation between `menu` and `menu_autorization` created in Java? Show us the relevant parts of your JPA entity classes. –  Feb 05 '15 at 11:20

1 Answers1

3

You can do it with JPQL using something like this

select m from Menu m join m.menuAuthorization ma where ma.id = :maId

Using Criteria API you could do it like this

Criteria criteria = session.createCriteria(Menu.class)
criteria.createAlias("menuAuthorization", "ma");
criteria.add(Restrictions.eq("ma.id", maId));
Menu menu = (Menu)criteria.uniqueResult();
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68