1

I was trying to collect data from table ProductInformation using parameters ProductId with following code but all I get is

unexpected token: * near line 1, column 8 [select * from com.koshayali.Koshayalinepal.model.ProductInformation where ProductId = :ProductId]

Code:

@Override
@Transactional
public List<ProductInformation> getSearchId(String ProductId) {
    Session session=sessionFactory.openSession();
    String sql = "select * from ProductInformation where ProductId = :ProductId";
    Query query = session.createQuery(sql).setParameter("ProductId", ProductId);;
    List<ProductInformation> produ = query.list();
    session.close();
    return produ;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

1

check productId long type or String type first if database productId is long type convert string to long value

 public List<ProductInformation> getSearchId(String ProductId){
        String getproductInfromationQuery = "FROM ProductInformation where ProductId =:ProductId";
            Query queryObj = sessionFactory.getCurrentSession().createQuery (getproductInfromationQuery);
            queryObj.setParameter("ProductId", ProductId);
            @SuppressWarnings("unchecked")
            List<ProductInformation> productList = queryObj.list();
            return productList;
    }
Umapathi
  • 558
  • 2
  • 10
  • 25
  • Thank you!! for your answer it perfectly but it is now talking about "java.lang.NumberFormatException: For input string" do i have to convert every data to string as in "https://stackoverflow.com/questions/46846044/java-lang-numberformatexception-for-input-string-id-for-hibernate" ??? – Ganesh Thapa Jan 17 '18 at 14:35
0

You shouldn't use * to select everything from database. FROM class_name is enough. To have a brief intro into HQL queries look into this tutorial.

So, your code should be like this

@Transactional
public List<ProductInformation> getSearchId(String ProductId) {
    Session session = sessionFactory.openSession();
    String sql = "from ProductInformation where ProductId = :ProductId";
    Query query = session.createQuery(sql).setParameter("ProductId", ProductId);;
    List<ProductInformation> produ = query.list();
    session.close();
    return produ;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vyshak
  • 126
  • 1
  • 9