How to fix "java.sql.SQLSyntaxErrorException: Unknown column 'product0_.return_policy' in 'field list' " exception?

svms54 :

I've been getting this "SQLSyntaxErrorException:Unknown column 'product0_.return_policy' in 'field list'" when I try to run the URL on my browser to GET all the products.

Look here

The browser shows the below as well:

There was an unexpected error (type=Internal Server Error, status=500). could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

returnPolicy is the only variable that's causing this problem. I'm able to successfully retrieve all the values from the database when I remove the variable itself from both the database and from the Product class in Java.

This is the getAllProducts method that is part of the RESTController:

@RequestMapping(method=RequestMethod.GET, value="/products")
    public List<Product> getAllProducts() {
        return productService.getAllProducts();
    }

which works fine when I remove the returnPolicy variable altogether.

This is the MySQL table description:

Values stored in returnPolicy column:

returnPolicy

  0 
  0 
  1 
  1 
  1 

This is the code for the 'Product' model's variables:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="product")
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String name;
    private int price;
    private String vendor;
    private String description;
    private Boolean returnPolicy;

ProductRepository

@Repository public interface ProductRepository extends JpaRepository<Product, String>{ }

Is there a problem with the mapping between SQL tinyint(Boolean) and the Boolean type of Java?

Nikhil Sahu :

Hibernate is assuming that the entity field returnPolicy corresponds to the table column return_policy. But actually, the column name is returnPolicy

Hibernate follows a naming strategy as to what column name should it derive from entity field names. You should explicitly specify whether to use ImplicitNamingStrategy or PhysicalNamingStrategy. Hibernate provides out of the box classes for this.

Alternatively, for this specific issue, annotating the field with explicit column name will make is understand.

See this SO answer for more.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=75403&siteId=1