0

The following is my Spring boot Rest API Application.

Vendor.Java

package hello;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity(name="vendor")
public class Vendor {

    @Id
    @Column(name="vendorId")
    private int vendorId;
    @Column(name="vendorName")
    private String vendorName;
    @Column(name="vendorPhone")
    private int vendorPhone;
    @Column(name="vendorBalance")
    private int vendorBalance;
    @Column(name="vendorChequeAmount")
    private int vendorChequeAmount;


    public int getVendorId() {
        return vendorId;
    }
    public void setVendorId(int vendorId) {
        this.vendorId = vendorId;
    }
    public String getVendorName() {
        return vendorName;
    }
    public void setVendorName(String vendorName) {
        this.vendorName = vendorName;
    }
    public int getVendorPhone() {
        return vendorPhone;
    }
    public void setVendorPhone(int vendorPhone) {
        this.vendorPhone = vendorPhone;
    }
    public int getVendorBalance() {
        return vendorBalance;
    }
    public void setVendorBalance(int vendorBalance) {
        this.vendorBalance = vendorBalance;
    }
    public int getVendorChequeAmount() {
        return vendorChequeAmount;
    }
    public void setVendorChequeAmount(int vendorChequeAmount) {
        this.vendorChequeAmount = vendorChequeAmount;
    }

}

VendorRepository.Java

package hello;

import org.springframework.data.repository.CrudRepository;

import hello.Vendor;

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete

public interface VendorRepository extends CrudRepository<Vendor, Integer> {

}

MainController.Java

package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import hello.Vendor;
import hello.VendorRepository;

@Controller    // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController {
    @Autowired // This means to get the bean called userRepository
               // Which is auto-generated by Spring, we will use it to handle the data
    private VendorRepository vendorRepository;

    @GetMapping(path="/all")
    public @ResponseBody Iterable<Vendor> getAllVendors() {
        // This returns a JSON or XML with the users
        return vendorRepository.findAll();
    }
    @GetMapping(path="/msg")
    public @ResponseBody String getMsg(){
        return "Hi";
    }
}

Application.Java

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

When I try to access @ http://localhost:8089/demo/all I get the following errors

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Feb 10 14:59:39 GST 2018
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

Please see the errors :-

Eclipse error log

http://localhost:8089/demo/all

Table details

Application.properties

server.port=8089
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.type=trace 
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.datasource.url=jdbc:mysql://localhost:3306/web_customer_tracker
spring.datasource.username=springstudent
spring.datasource.password=springstudent

Still, I am getting the error:

eclipse log

What is this error? How to resolve it?

gmc
  • 3,910
  • 2
  • 31
  • 44
ShefZee
  • 61
  • 2
  • 8

3 Answers3

2

From the error message, it seems that the column names are being generated wrong in the SQL query. I recommend when developing to set spring.jpa.show-sql=true in application.properties to see the generated query in console.

The error is weird, because it complaints about column vendor_id when you are scpecifically setting its name to vendorId (werid database column naming policy, btw). Researching a little bit, I came across this question, where according to its first answer, it seems to be some kind of bug with column names. As said there, try adding this in your application.preferences:

spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
gmc
  • 3,910
  • 2
  • 31
  • 44
  • Hi, Please see my application.properties. Also, I am not able to see the SQL query generated in the console. I am running the app as a Java application. – ShefZee Feb 10 '18 at 13:09
  • I ran as Spring boot App and I could see the SQL generated in the log. Yes, it is wrong..Hibernate: /* select generatedAlias0 from vendor as generatedAlias0 */ select vendor0_.vendor_id as vendor_i1_0_, vendor0_.vendor_balance as vendor_b2_0_, vendor0_.vendor_cheque_amount as vendor_c3_0_, vendor0_.vendor_name as vendor_n4_0_, vendor0_.vendor_phone as vendor_p5_0_ from vendor vendor0_ – ShefZee Feb 10 '18 at 13:11
2

If you are using Hibernate v5 use the following line in application.properties

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Sergey Sargsyan
  • 452
  • 6
  • 10
1

I just tried with column names in lowercase and it worked !

@Id
@Column(name="vendorid")
private int vendorId;
@Column(name="vendorname")
private String vendorName;
@Column(name="vendorphone")
private int vendorPhone;
@Column(name="vendorbalance")
private int vendorBalance;
@Column(name="vendorchequeamount")
private int vendorChequeAmount;
ShefZee
  • 61
  • 2
  • 8