public void AverageRatingByProductName(String prodName) { SessionFactory sf = HibernateUtility.getSessionfactory(); Session session = sf.openSession(); session.beginTransaction(); String sql1 = "SELECT * FROM product where productName = :productName"; SQLQuery query1 = session.createSQLQuery(sql1); query1.setParameter("productName", prodName); query1.addEntity(Product.class); Product product = (Product) query1.uniqueResult(); String sql2 = "SELECT * FROM ratingdetails where productId = :productId"; SQLQuery query2 = session.createSQLQuery(sql2); query2.setParameter("productId", product.getProductId()); query2.addEntity(RatingDetails.class); List ratings = query2.list(); session.getTransaction().commit(); float count = ratings.size(); int sum = 0; for (Object r : ratings) { RatingDetails rate = (RatingDetails) r; sum = sum + rate.getRating(); } float averageRating = (sum / count); System.out.println("pid " + product + " avg " + averageRating); }
public void calulateAverageRating() { SessionFactory sf = HibernateUtility.getSessionfactory(); Session session = sf.openSession(); session.beginTransaction(); String sql = "SELECT * FROM product"; SQLQuery query = session.createSQLQuery(sql); query.addEntity(Product.class); List listProductIds = query.list(); for (Object pro : listProductIds) { Product p = (Product) pro; String sql1 = "SELECT * FROM ratingdetails where productId = :productId"; SQLQuery query1 = session.createSQLQuery(sql1); query1.setParameter("productId", p.getProductId()); query1.addEntity(RatingDetails.class); List ratings = query1.list(); session.getTransaction().commit(); float count = ratings.size(); int sum = 0; for (Object r : ratings) { RatingDetails rate = (RatingDetails) r; sum = sum + rate.getRating(); } float averageRating = (sum / count); avgProdList.put(p, averageRating); System.out.println("pid " + p.getProductId() + " avg " + averageRating); } System.out.println(avgProdList); System.out.println(); sortAverageRating(); /*for (Iterator iterator = listProductIds.iterator(); iterator.hasNext();) { RatingDetails ratingobj = (RatingDetails) iterator.next(); System.out.print("First Name: " + employee.getFirstName()); System.out.print(" Last Name: " + employee.getLastName()); System.out.println(" Salary: " + employee.getSalary()); }*/ }
@Override public int hashCode() { int result = id.hashCode(); result = 31 * result + entryId.hashCode(); result = 31 * result + userId.hashCode(); result = 31 * result + type.hashCode(); result = 31 * result + state; result = 31 * result + ratingDetails.hashCode(); result = 31 * result + comment.hashCode(); result = 31 * result + rating; result = 31 * result + episode; result = 31 * result + helpfulVotes; result = 31 * result + (int) (time ^ (time >>> 32)); result = 31 * result + username.hashCode(); result = 31 * result + imageId.hashCode(); return result; }
@SuppressWarnings("SimplifiableIfStatement") @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Comment comment1 = (Comment) o; if (state != comment1.state) return false; if (rating != comment1.rating) return false; if (episode != comment1.episode) return false; if (helpfulVotes != comment1.helpfulVotes) return false; if (time != comment1.time) return false; if (!id.equals(comment1.id)) return false; if (!entryId.equals(comment1.entryId)) return false; if (!userId.equals(comment1.userId)) return false; if (!type.equals(comment1.type)) return false; if (!ratingDetails.equals(comment1.ratingDetails)) return false; if (!comment.equals(comment1.comment)) return false; if (!username.equals(comment1.username)) return false; return imageId.equals(comment1.imageId); }