Question

For some reason I don't get results when running this from method.

@SuppressWarnings("unchecked")
public Object[] getPointRaiting(Long id) {
    EntityManager em = createEntityManager();
    em.getTransaction().begin();
    Query allPointsQuery = em
            .createQuery("Select AVG(r.RATING) from Ratings r WHERE r.POINT_ID = :point");
    allPointsQuery.setParameter("point", id);
    Object[] rating = (Object[]) allPointsQuery.getSingleResult();
    em.getTransaction().commit();
    em.close();
    closeEntityManager();
    return rating;
}

SQL should be correct as it executes in HSQL db manager and returns the correct value. But java function stops running at query. It does'nt throw any errors just stops. I'm out of ideas, where should I look? (Other similiar methods with count and select all work correctly).

Using HSQLDB and Hibernate.

Found that the following error was thrown:

org.hibernate.QueryException: could not resolve property: RATING of: kaart.entities.Ratings [Select AVG(r.RATING) from kaart.entities.Ratings r WHERE r.POINT_ID = :point]

But this does not solve it for me as the RATING property is defined in table and in entity...

@Entity @Table(name = "RATINGS") 
public class Ratings implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    private Point point;

    @ManyToOne
    private User user;

    @Column(name = "RATING")
    private int rating;

    private static final long serialVersionUID = 1L;

    public Ratings() {
        super();
    }

    public Ratings(Point point, User user, int rating) {
        this.point = point;
        this.user = user;
        this.rating = rating;
    }

    /*all getters and setters here*/}


@Entity
@Table(name = "POINT")
public class Point implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(mappedBy = "point")
    private List<Category> pointsByCategory;

    @OneToMany(mappedBy = "point")
    private List<Ratings> pointRatings;

    @Column(name = "NAME")
    private String name;

    @Column(name = "LOCATION")
    private String location;

    @Column(name = "DESCRIPTION")
    private String description;

    @Column(name = "LINK")
    private String link;

    @ManyToOne
    private User user;
    private static final long serialVersionUID = 1L;

    public Point() {
        super();
    }

    public Point(String name, String location, String description, String link, User user) {
        this.name = name;
        this.location = location;
        this.description = description;
        this.link = link;
        this.user = user;
    } /* getters and setters*/
Was it helpful?

Solution 2

Most likely this problem is caused by caps-locked property names: RATING, POINT_ID.

Try replacing them with the ones that you use in Ratings class, probably:

Select AVG(r.rating) from Ratings r WHERE r.point.id = :point_id

OTHER TIPS

You can only pass JP-QL inside em.createQuery(). But seems you are using native SQL with values like r.RATING, r.POINT_ID, which may not be in the Java entity. Replace it with equivalent java entity variable, could be pointId

em.createQuery("Select AVG(r.RATING) from Ratings r WHERE r.POINT_ID = :point");

If you want to use native sql, you can use em.createNativeQuery().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top