0

I have this structure:

public enum SaleItemType {
 CRUISE,
 DAILY_HOSTING
}
public class Estimate {
 ...
 private List<SaleItemType> interestedSaleItemTypes;
 @Column(name = "sale_item_type")
 @CollectionTable(name = "estimate_sale_item_type", joinColumns = @JoinColumn(name = "estimate_id"))
 @ElementCollection(targetClass = SaleItemType.class)
 @Enumerated(EnumType.STRING)
 public List<SaleItemType> getInterestedSaleItemTypes() {
 return interestedSaleItemTypes;
 }
}

And i'm trying to do a simple query:

String q = "FROM " + Estimate.class.getSimpleName() + " e" + " WHERE e.interestedSaleItemTypes IN :a";
TypedQuery<Estimate> query1 = getEm().createQuery(q, Estimate.class);
query1.setParameter("a", EnumSet.of(SaleItemType.CRUISE));
query1.getResultList();

I'm getting this query(and error) on the log:

DEBUG SQL:92 - select estimate0_.id as id1_25_, estimate0_.average_ticket as average_2_25_, estimate0_.description as descript3_25_, estimate0_.end_date as end_date4_25_, estimate0_.pax_quantity as pax_quan5_25_, estimate0_.start_date as start_da6_25_ from estimate estimate0_ cross join estimate_sale_item_type interested1_ where estimate0_.id=interested1_.estimate_id and (. in (?))

DEBUG SqlExceptionHelper:124 - could not extract ResultSet [n/a] org.postgresql.util.PSQLException: No value specified for parameter 1.

Why hibernate is doing this query?

Im using Hibernate 5.1 Final

flavio.donze
8,23810 gold badges69 silver badges102 bronze badges
asked Apr 12, 2016 at 14:10
1

2 Answers 2

1

The IN expression can be used to test if a value is in a collection but interestedSaleItemTypes is not a simple value but itself a collection. Therefore use MEMBER OF:

String q = "FROM Estimate e WHERE :a MEMBER OF e.interestedSaleItemTypes";
TypedQuery<Estimate> query1 = getEm().createQuery(q, Estimate.class);
query1.setParameter("a", SaleItemType.CRUISE);
answered Apr 12, 2016 at 14:28
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, it worked. Funny fact, i knew how to use member ofbut for some reason this time i didn't even considered it.
0

Did you try to put parenthesis in your IN clause?

I don't know if it's required, but in all tutorials that I found, always had the parenthesis. http://www.postgresqltutorial.com/postgresql-in/

Also, as the IN clause is expecting a list of values you can use the setParameterList instead of setParameter. Try this:

String q = "FROM " + Estimate.class.getSimpleName() + " e" + " WHERE e.interestedSaleItemTypes IN (:a)";
TypedQuery<Estimate> query1 = getEm().createQuery(q, Estimate.class);
query1.setParameterList("a", EnumSet.of(SaleItemType.CRUISE));
query1.getResultList();
answered Apr 12, 2016 at 14:24

3 Comments

the IN expression of SQL needs parentheses but the IN expresssion of JPA does not
@wero True, but Hibernate for example needs parenthesis in some versions, check out this post
There is no such method Query.setParameterList() in JPA

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.