1

I have such a two classes:

public class Average {
long id;
String name;
double average;
public Average(long id , String name , double payment)
{
    this.id = id;
    this.name = name;
    this.payment = payment;
}

@Override
public String toString()
{
    return id + "\t" + name + " " + payment;
}
}

and

@Entity
public class Payment{
@Id
@GeneratedValue
long    id;
Student student;
Subject subject;
double  payment;

public Payment()
{
}

and I want to perform query on this class with java, but it is not working correctly. What can be wrong. Bellow I posted my query from another class where I call it:

public List<Object> getPayment()
{
    Query q = entityManager.createQuery("Select NEW Average( g.subject.id , g.subject.name , AVG(g.payment) ) from Payment g GROUP BY g.subject.id, gge.subject.name");
    return q.getResultList();
}

Please be patient with me, this is my first post!

3
  • 2
    Welcome to Stack Overflow! Please include information about what exactly is going wrong. Commented Jul 22, 2014 at 18:13
  • I am not sure exactly what is wrong, because query does not show anything. There are also no errors... Commented Jul 22, 2014 at 18:17
  • Have you turned on Hibernate debug logging? Whenever something's not working in the database (as opposed to in your Java code), that's the thing to do. Commented Jul 22, 2014 at 19:04

2 Answers 2

2

Average is not an entity (you should annotate it with @Entity - like the one you did for Payment) so you cannot perform entityManager.createQuery(). Also you should specify table @Table(name = "XXX") after @Entity and before class name.

Query should be something like:

Select g.subject.id , g.subject.name , AVG(g.payment) from Average a, Payment g GROUP BY g.subject.id, g.subject.name

-- UPDATE

If Average is a Bean class used for projection then do the following:

 Object[] payments = (Object[]) entityManager.createQuery("Select g.subject.id,
 g.subject.name, AVG(g.payment) from Payment g
 GROUP BY g.subject.id, g.subject.name").getSingleResult();

then iterative through the objects of payments:

for (Object object : payments) {
    System.out.println(object);
}

if the result is just one row then put getSingleResult otherwise you need a List of the objects and iterate through the list:

List<Object[]> payments = (List<Object[]>) entityManager.createQuery("Select g.subject.id,
     g.subject.name, AVG(g.payment) from Payment g
     GROUP BY g.subject.id, g.subject.name").getResultList();

then iterative through the objects of payments:

if (payments != null){        
    for (Object[] object : payments) {
        System.out.println(object[0] + " " + object[1] + " " + object[2]);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Why I need this part: from Average a?
Base on the query you provided I thought you need it. If don't need it then remove it.
The Average object is just used for projection, so why should he add the @Entity annotation? The query you proposed does not make sense.
That's a wrong way for projection though. You cannot perform entityManager.createQuery() on non entities. He should do the createQuery() on Payment first and then get the results back (List of array of objects) and then map them to the Bean Class (Average). I thought Average is an entity, and that's why I proposed to annotate it with @Entity.
1

Could it be the gge instead of g in GROUP BY g.subject.id, gge.subject.name?

Comments

Your Answer

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.