This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes JPA: Count object relations in n:m relationship - do I need a native query? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA: Count object relations in n:m relationship - do I need a native query?" Watch "JPA: Count object relations in n:m relationship - do I need a native query?" New topic
Author

JPA: Count object relations in n:m relationship - do I need a native query?

Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Hej,

I am stuck at n:m relationships. I want to count the number of relations with JPA.

Prerequisites: JPA (1.x atm), Hibernate as JPA provider, configuration by JPA annotations.

Example:

Item - Category (Student - Course would do also )



This results in a quite simple database schema:

- item(id,...)
- category(id, label...)
- item_category(item_id, category_id)

I just want to count the number of articles that belong to a category. Simple with a native query:

But a native query feels like an impurity. No IDE checks, no refactoring support, no startup checks by the persistence provider... I would prefer something in JPQL but I can't see how to access the "entity-less" relationship table.

Alternatively I could build an object for the relationship. Seems plausible, as I do work with it's elements.

Are those the ways to go or am I missing something important?

Thank you.
 
Consider Paul's rocket mass heater.
 
subject: JPA: Count object relations in n:m relationship - do I need a native query?