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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:27:59+00:00 2026-06-04T21:27:59+00:00

I have two models, Purchase and Tag . A Purchase can have many Tags

  • 0

I have two models, Purchase and Tag.

A Purchase can have many Tags (One to Many relationship).

What is the best (Efficent/Cleanest/etc) way to return all purchases that have one or more tags? (The actual tag entities dont have to be returned).

I’m currently using a column in the purchase table to determine if it has been tagged but would like to remove it (See PurchaseDAO for it been used)

PurchaseController:

@RequestMapping(value = "purchases/tagged", method = RequestMethod.GET)
@ResponseBody
public final List<Purchase> getTagged()
{
    return RestPreconditions.checkNotNull(purchaseService.getTagged());
}

@RequestMapping(value = "purchases/pending", method = RequestMethod.GET)
@ResponseBody
public final List<Purchase> getPending()
{
    return RestPreconditions.checkNotNull(purchaseService.getPending());
}

PurchaseService:

@Service
public class PurchaseService implements IPurchaseService
{
    @Autowired
    private IPurchaseDAO purchaseDAO;

    public PurchaseService()
    {

    }

    @Transactional
    public List<Purchase> getAll()
    {
        return purchaseDAO.findAll();
    }

    @Transactional
    public List<Purchase> getPending()
    {
        return purchaseDAO.getPending();
    }

    @Transactional
    public List<Purchase> getTagged()
    {
        return purchaseDAO.getTagged();
    }
}

PurchaseDAO:

@Repository
public class PurchaseDAO extends AbstractJpaDAO<Purchase> implements IPurchaseDAO {

    @PersistenceContext
    EntityManager entityManager;

    public PurchaseDAO() {
        setClazz(Purchase.class);
    }

    public  List<Purchase> getPending() {
        return entityManager.createQuery("from Purchase where tagged = 0")
                .getResultList();
    }

    public  List<Purchase> getTagged() {
        return entityManager.createQuery("from Purchase where tagged = 1")
                .getResultList();
    }
}

Both my Purchase and Tag DAOs extend the following AbstractJpaDAO:

public abstract class AbstractJpaDAO<T extends Serializable> implements
        IAbstractJpaDAO<T> {

    private Class<T> clazz;

    @PersistenceContext
    EntityManager entityManager;

    public void setClazz(final Class<T> clazzToSet) {
        this.clazz = clazzToSet;
    }

    public T findOne(final Long id) {
        return entityManager.find(clazz, id);
    }

    public List<T> findAll() {
        return entityManager.createQuery("from " + clazz.getName())
                .getResultList();
    }

    public void save(final T entity) {
        entityManager.persist(entity);
    }
}

Thanks

JB Suggestion Update

After changing PurchaseDAO getTagged() to:

public  List<Purchase> getTagged() {
        return entityManager.createQuery("SELECT p FROM Puchase p INNER JOIN p.tags")
                .getResultList();
    }

I get following error:

TRACE [http-bio-8080-exec-3] o.s.w.c.s.AnnotationConfigWebApplicationContext [AbstractApplicationContext.java:322] Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/PurchaseAPIServer/api/purchases/tagged]; client=[192.168.1.17]; method=[GET]; servlet=[PurchaseAPIServer]; session=[null]; user=[null]; time=[411ms]; status=[failed: org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.ast.QuerySyntaxException: Puchase is not mapped [SELECT p FROM Puchase p JOIN p.tags tag]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: Puchase is not mapped [SELECT p FROM Puchase p JOIN p.tags tag]]
  • 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-04T21:28:02+00:00Added an answer on June 4, 2026 at 9:28 pm
    select p from Purchase p inner join p.tags
    

    The inner join just makes there is at least one tag for the purchase.

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

Sidebar

Related Questions

I have two models, Image and Tag . Each Image object can have more
I have two models, one is Group, the other Item. A group has many
I have two models and controllers: Snippets, and Tags. There is a belongs_to relationship,
I have two models, A and B, and one light, L. I would like
I have two Models, one that has the name Exam and one that has
I have two models: Users and Projects. The idea is that Users can follow
I have two models jobs and clients . A user can simple create a
I have two models generated with generate scaffolding, one is a LogBook the other
I have a rails app that has two models one is vender and the
I have two models, one is called labor_report and the other is employee, My

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.