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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:24:08+00:00 2026-06-02T15:24:08+00:00

I’m working with a jpa query and i can find a proper solution. I

  • 0

I’m working with a jpa query and i can find a proper solution. I hope you can help me. I will start describing my model. I have a Resource which belongs to a company. The company has many Branches. The resource has associated a couple of resource ownership configuration. A resource ownership describe which branches can use the resource and a period of time in which this configuration is valid. But, if no branches are indicated the resource can be used by all the branches of the company.

here is the model. Getters and setters are omitted

@Entity
public class Resource extends ActivableAbstractModel{

    /**
     * the serial version uid
     */
    private static final long serialVersionUID = -8568230011058859716L;

    public Resource() {
        this.ownerShipConfigurations = new ArrayList<>();
    }

    @Column(length=30)
    private String name;

    private String description;         

    @ManyToOne(cascade = {}, fetch = FetchType.LAZY, targetEntity=Company.class)
    @ForeignKey(name = "FK_company_resource")
    @JoinColumn(nullable = false)
    private Company company;



    @OneToMany(cascade={CascadeType.ALL}, orphanRemoval=true, mappedBy="resource")
    private List<ResourceOwnerShipConfiguration> ownerShipConfigurations;

}

Entity

@Entity
public class ResourceOwnerShipConfiguration extends AbstractModel{

    /**
     * the serial version uid
     */
    private static final long serialVersionUID = -4560593105136625002L;

    @Embedded
    private Period period;

    @ManyToOne(targetEntity=Company.class)
    private Company company;

    @ManyToMany(targetEntity=Branch.class)
    private List<Branch> branches;

    @ManyToOne
    private Resource resource;  
}

What is important about Branch Model and Company Model is that both has a Id.

Here is my query:

@Query("select R from Resource R join R.ownerShipConfigurations oc join oc.branches b  where R.active = true and R.company.id = :companyId and (oc.branches IS EMPTY or  b.id = :branchId)")

What i want to do? I want all the resources that belong to a company ( using companyId) and can be used by a particular branch (using branchId). But if the resource does not has a list of branches (defined in ResourceOwnerShipConfiguration ) must be returned.

Hope it is clear.

Using that query i can´t retrieve resources that does not have a list a branches. Just the ones which has the particular branch associated.

Thanks in advance.

  • 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-06-02T15:24:10+00:00Added an answer on June 2, 2026 at 3:24 pm

    You need to use a LEFT JOIN,

    select R from Resource R join R.ownerShipConfigurations oc left join oc.branches b  where R.active = true and R.company.id = :companyId and (b.id is null or  b.id = :branchId)
    

    See,
    http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#LEFT_JOIN

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.