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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:57:39+00:00 2026-05-26T16:57:39+00:00

I would like to limit the data that is coming from OneToMany relation. In

  • 0

I would like to limit the data that is coming from OneToMany relation. In my program the boss account can view all the company’s that have orders in the given month and year. The company class can f.e. count the income from these orders. I used @FilterJoinTable but since i have to use EntityManager I cant do it anymore. Here is the code I used with the Session object.

Order class

@Entity(name="order")
public class Order implements Serializable {
private static final long serialVersionUID = 1L;

private Company company;
private int month;
private int year;
[...]

@JoinColumn(name = "company_idcompany", referencedColumnName = "idcompany")
@ManyToOne(optional = false, fetch = FetchType.EAGER)
public Company getCompany() {
    return company;
}

public void setCompany(Company company) {
    this.company = company;
}
[...]

Company class

@Entity(name="company")
@FilterDef(name="orderFilter", parameters = {
                       @ParamDef(name = "month", type = "int"),
                       @ParamDef(name = "year", type = "int")
                       })
public class Company implements Serializable 
{
private static final long serialVersionUID = 1L;

private List<Order> orderList = new ArrayList<Order>();
[...]

@OneToMany(
        fetch = FetchType.LAZY,
        mappedBy="company"
)
@JoinTable(
        name = "order",
        joinColumns = { @JoinColumn(name = "idcompany") },
        inverseJoinColumns = { @JoinColumn(name = "idorder") }
)
@FilterJoinTable(name="orderFilter", condition=":month = month and :year = year")
public List<Order> getOrderList() {
    return orderList;
}
[...]

the get method looked like this

public List<Company> get(int month, int year) throws AdException
{
    try
    {
        begin();

        //will get only the orders in given month and year
        Filter filtr = getSession().enableFilter("orderFilter");

        filtr.setParameter("month", month);
        filtr.setParameter("year", year);

        //this criteria didn't work well so I've added a loop below 
        Criteria crit = getSession().createCriteria(Company.class);
        crit.add(Restrictions.isNotEmpty("companyList"));


        List<Company> companys = crit.list();

        Iterator iter = companys.iterator();

        //throwing out the companys that have no orders in given month and year
        while(iter.hasNext())
        {
            Company com = (Company)iter.next();
            if(com.getOrderList().isEmpty())
            {
                companys.remove(com);
                iter = companys.iterator();
            }
        }

        getSession().disableFilter("orderFilter");
        commit();

        return companys;
    }
    catch( HibernateException e )
    {
        rollback();
        throw new AdException("ex message",e);
    }
 }

The get method returns only the company’s that have orders in given month and year. The orderList in each company contains only the orders from given month and year. I know the get method is not pretty but it worked. Can You please tell me how can I resolve this problem? I am clueless.

RESOLVED thanks to Joshua Davis

   Query q = em.createQuery("SELECT DISTINCT o.company FROM Order z WHERE o.month=:month AND z.year=:year");

            q.setParameter("month", month);
            q.setParameter("year", year);

            List<Company> companys = q.getResultList();

            for(Company com : companys)
            {
                Query q2 = em.createQuery("from Order where month = :month and year = :year and company.idCompany = :id");
                q2.setParameter("month", month);
                q2.setParameter("year", year);
                q2.setParameter("id", com.getIdCompany());

                com.setOrderList(q2.getResultList());
            }
  • 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-26T16:57:39+00:00Added an answer on May 26, 2026 at 4:57 pm

    A simpler way to do this would be:

    em.createQuery("select o from Order o " +
            "where o.company.id=:id and o.month=:m and o.year=:y")
            .setParameter("id",someCompany.getId())
            .setParameter("y",year)
            .setParameter("m",month)
            .getResultList();
    

    Essentially, you are using object graph navigation to solve a problem that is easily solved with a plain JPA query.

    If you need to modify the orders in the list, that will work transparently, as the query will return persistent instances. If you need to add or delete orders, then you might need to access the collection in Firma (company).

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

Sidebar

Related Questions

I'd like to write a decorator that would limit the number of times a
I have a table that contains some sensitive data that I would like to
I just can't figure this out. I have a script that gets data from
I have a java application and I would like to get some data from
I have a web service that I would like to use from a few
I would like to limit an application to having only one instance running on
I have a DTD and I would like to limit a few attribute properties'
I have a text field whose length I would like to limit at the
I'm using the TServerSocket component in my Delphi application. I would like to limit
I'm using an h:outputText to display a value. I would like to limit 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.