About Question enthuware.oce-jpad.v6.2.492 :

Moderator: admin

Post Reply
johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

About Question enthuware.oce-jpad.v6.2.492 :

Post by johnlong »

"The path expression argument to COUNT may terminate in either a state field or a association field, or the argument to COUNT may be an identification variable."

Is s.presentations a state field or a collection_valued_path_expression?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.492 :

Post by admin »

The option clearly says, "(Assume that presentations is a Collection field in Student)". So s.presentations is a state field. Though it can be considered collection_valued_path_expression also because s.presentations is an expression as well, after all.
If you like our products and services, please help us by posting your review here.

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.492 :

Post by johnlong »

I see, thanks.

nunoago
Posts: 1
Joined: Wed Dec 28, 2016 5:04 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.492 :

Post by nunoago »

JPA 2.0 spec says:
A state _field is designated by the name of an entity or embeddable class state field that corresponds to a basic type.
A collection_valued_field is designated by the name of an association field in a one-to-many or a
many-to-many relationship or by the name of an element collection field. The type of a collection_valued_field is a collection of values of the abstract schema type of the related entity or element type.
It also says:
It is illegal to use a collection_valued_path_expression other than in the FROM clause of a query except in an empty_collection_comparison_expression, in a collection_member_expression, or
as an argument to the SIZE operator.
So I believe that count(s.presentations) is wrong. It should be size(s.presentations).

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.492 :

Post by johnlong »

You can't do count(s.presentations) as s.presentations is not single_valued_field, it is collection_valued_field, you can't count collection, you can count only collection members, so it has to be count(p) where p is s.presentations

And yes, you can do size(p.presentations)

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.492 :

Post by himaiMinh »

In the spec, it says count()'s argument can be a state field , an association or and identification variable.
In this example, s.presentations is a list of Presentations , which is an entity.
What if Presentation is a list of embeddables?
Can count() uses embeddables as an argument?


By the way, when I ran this example using ToyAppForJPA, "select count (s.presentations) from Student s", it does not allow the collection variable as count's argument.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.492 :

Post by __JJ__ »

JPA 101:
You cannot use a collection-value path-expression in a SELECT clause; therefore, the following query
is not valid:
SELECT f.topics FROM Forum f

Pro JPA2:
The result type of a select query cannot be a collection; it must be a single valued object such as an
entity instance or persistent field type. Expressions such as e.phones are illegal in the SELECT clause
because they would result in Collection instances (each occurrence of e.phones is a collection, not an
instance). Therefore, just as with SQL and tables, if we want to navigate along a collection association
and return elements of that collection, we must join the two entities together.
Is it the case that

Code: Select all

select e.phones 
is invalid but

Code: Select all

select count(e.phones)
is not, because the latter resolves to a scalar and the former resolves to a collection?

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.492 :

Post by __JJ__ »

I cannot get it to work, even with a onetomany that is owned:

Code: Select all

@Entity
public class Employee {
...
	@OneToMany(cascade= {CascadeType.PERSIST, CascadeType.MERGE})
	private Set<Competency> competencies;

Code: Select all

        Query q = em.createQuery("select count(e.competencies) from Employee e");
        System.out.println(q.getResultList());

Code: Select all

 06:08:03 DEBUG [org.hibernate.SQL].logStatement - select count(.) as col_0_0_ from Employee employee0_ cross join Employee_Competency competenci1_, Competency competency2_ where employee0_.id=competenci1_.Employee_id and competenci1_.competencies_competencyArea=competency2_.competencyArea
 06:08:03 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper].logExceptions - SQL Error: 1064, SQLState: 42000
 06:08:03 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper].logExceptions - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.) as col_0_0_ from Employee employee0_ cross join Employee_Competency competenc' at line 1
 Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:149)
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1423)
	at org.hibernate.query.Query.getResultList(Query.java:146)
	at jpa.Main.selectCountFromCVF(Main.java:110)
	at jpa.Main.main(Main.java:75)
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Post Reply

Who is online

Users browsing this forum: No registered users and 30 guests