com.bookselling.dao.RatingDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.bookselling.dao.RatingDaoImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bookselling.dao;

import com.bookselling.domain.Rating;
import com.bookselling.domain.SellingPost;
import java.io.Serializable;
import java.math.BigDecimal;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Phan Phat
 */
@Repository
public class RatingDaoImpl extends GenericDao<Rating> implements RatingDao {

    @Override
    public Class<Rating> registeredClass() {
        return Rating.class;
    }

    @Override
    public double getAveragePoint(Serializable id) {
        SQLQuery query = getSession().createSQLQuery("  select avg(point) " + "  from rating "
                + "  where sellingPostId = :id " + "  group by sellingPostId  ");
        query.setInteger("id", (int) id);
        BigDecimal averagePoint = (BigDecimal) query.uniqueResult();
        return averagePoint != null ? averagePoint.doubleValue() : -1.0;
    }

}