com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:Table' demo-db.common_bean'不存在

时间:2015-12-23 18:26:04

标签: spring hibernate spring-boot generics spring-data-jpa

我正在尝试使用JPARepository创建Spring启动应用程序。我的目标是创建应用程序通用。 在我的应用程序中,我为所有实体提供了4个常用功能,如下所示:

  • GETALL

  • getAllNewAfterLastSyncDate

  • getAllModifiedAfterLastSyncDate

  • getAllDeletedAfterLastSyncDate

为了实现这一目标并避免代码的冗余,我创建了一个扩展JPARepository的通用基本存储库,如下所示:

BaseRepository.java

package dev.ashish.syncdemo.utlities;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.data.jpa.repository.Query;

import org.springframework.data.repository.NoRepositoryBean;


@NoRepositoryBean

public interface BaseRepository<T>  extends JpaRepository<T, Long>{

**@Query("select t from #{#entityName} t where t.deleteFlag = 'F' ")**
public List<T> getAll();

/*public List<T> getAllNewAfterLastSyncDate();
public List<T> getAllModifiedAfterLastSyncDate();
public List<T> getAllDeletedAfterLastSyncDate();
*/

}

我创建了一个公共bean,它将由我的aplication中的所有实体扩展,因为它有5个用于所有实体的公共属性或字段。

CommonBean.java

package dev.ashish.syncdemo.beans;

import java.sql.Timestamp;

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

@Entity
public class CommonBean {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private Long id;

@Column(name = "code")
private String code;

@Column(name = "created_by")
private Long createdBy;

@Column(name = "created_oy")
private Timestamp createdOn;

@Column(name = "modified_by")
private Long modifiedBy;

@Column(name = "modified_on")
private Timestamp modifiedOn;

@Column(name = "delete_flag")
private String deleteFlag;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public Long getCreatedBy() {
    return createdBy;
}

public void setCreatedBy(Long createdBy) {
    this.createdBy = createdBy;
}

public Timestamp getCreatedOn() {
    return createdOn;
}

public void setCreatedOn(Timestamp createdOn) {
    this.createdOn = createdOn;
}

public Long getModifiedBy() {
    return modifiedBy;
}

public void setModifiedBy(Long modifiedBy) {
    this.modifiedBy = modifiedBy;
}

public Timestamp getModifiedOn() {
    return modifiedOn;
}

public void setModifiedOn(Timestamp modifiedOn) {
    this.modifiedOn = modifiedOn;
}

public String getDeleteFlag() {
    return deleteFlag;
}

public void setDeleteFlag(String deleteFlag) {
    this.deleteFlag = deleteFlag;
}

}

现在考虑我想将此用于客户实体

CustomerEntity.java

package dev.ashish.syncdemo.beans;

import javax.persistence.Column;

public class CustomerEntity extends CommonBean{

@Column(name="first_name")
private String firstName;

@Column(name="middle_name")
private String middleName;

@Column(name="last_name")
private String lastName;

@Column(name="address1")
private String address1;

@Column(name="address2")
private String address2;

@Column(name="landline_no")
private String landlineNo;

@Column(name="mobile_no")
private String mobileNo;

@Column(name="email_id")
private String emailId;

@Column(name="city")
private String city;

@Column(name="state")
private String state;

@Column(name="country")
private String country;

@Column(name="pin_code")
private String pinCode;

@Column(name="fax_number")
private String faxNumber;

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getMiddleName() {
    return middleName;
}

public void setMiddleName(String middleName) {
    this.middleName = middleName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getAddress1() {
    return address1;
}

public void setAddress1(String address1) {
    this.address1 = address1;
}

public String getAddress2() {
    return address2;
}

public void setAddress2(String address2) {
    this.address2 = address2;
}

public String getLandlineNo() {
    return landlineNo;
}

public void setLandlineNo(String landlineNo) {
    this.landlineNo = landlineNo;
}

public String getMobileNo() {
    return mobileNo;
}

public void setMobileNo(String mobileNo) {
    this.mobileNo = mobileNo;
}

public String getEmailId() {
    return emailId;
}

public void setEmailId(String emailId) {
    this.emailId = emailId;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public String getPinCode() {
    return pinCode;
}

public void setPinCode(String pinCode) {
    this.pinCode = pinCode;
}

public String getFaxNumber() {
    return faxNumber;
}

public void setFaxNumber(String faxNumber) {
    this.faxNumber = faxNumber;
}

@Override
public String toString() {
    return "CustomerEntity [firstName=" + firstName + ", middleName=" + middleName + ", lastName=" + lastName
            + ", address1=" + address1 + ", address2=" + address2 + ", landlineNo=" + landlineNo + ", mobileNo="
            + mobileNo + ", emailId=" + emailId + ", city=" + city + ", state=" + state + ", country=" + country
            + ", pinCode=" + pinCode + ", faxNumber=" + faxNumber + ", getId()=" + getId() + ", getCode()="
            + getCode() + ", getCreatedBy()=" + getCreatedBy() + ", getCreatedOn()=" + getCreatedOn()
            + ", getModifiedBy()=" + getModifiedBy() + ", getModifiedOn()=" + getModifiedOn() + ", getDeleteFlag()="
            + getDeleteFlag() + "]";
}



}

我创建了CustomerService,它扩展了BaseRepositoy,如下所示:

CustomerService.java

package dev.ashish.syncdemo.service;

import org.springframework.stereotype.Service;

import dev.ashish.syncdemo.beans.CustomerEntity;
import dev.ashish.syncdemo.utlities.BaseRepository;

@Service("customerService")
public interface CustomerService extends BaseRepository<CustomerEntity>{

}

FrontController.java

package dev.ashish.syncdemo.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import dev.ashish.syncdemo.service.CustomerService;
import dev.ashish.syncdemo.utlities.Constants;

@RestController
@RequestMapping("/frontgate")
public class FrontController {

@Autowired
private  CustomerService customerService;

@RequestMapping(value = "/getres", method = RequestMethod.POST)
public String getRequest(HttpServletRequest request, HttpServletResponse           response) throws Exception {

    String reqStr = request.getReader().readLine();

    System.out.println("Request is : " + reqStr);

    Map<String, Object> reqMap = new Gson().fromJson(reqStr, new TypeToken<HashMap<String, Object>>() {
    }.getType());

    System.out.println("Req Map " + reqMap);

    return parseRequest(reqMap);

}   


public String parseRequest(Map<String, Object> reqMap)
{
    String entity = (String)reqMap.get(Constants.ENTITY);
    String action = (String)reqMap.get(Constants.ACTION);
    String pageSize = (String)reqMap.get(Constants.PAGE_SIZE);
    String pageNumber = (String)reqMap.get(Constants.PAGE_NUMBER);
    String lastSyncDate = (String)reqMap.get(Constants.LAST_SYNC_DATE);


    return customerService.getAll().toString();
}

}

SyncDemoApplication.java

package dev.ashish;

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

@SpringBootApplication
public class SyncDemoApplication {

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

申请流程如下:

请求将转到FrontController然后它将被转发到customerservice,它扩展了JPArepository类型的基本存储库。 由于存在所有常见功能,我不想单独为所有实体创建存储库,并为每个实体编写查询。正如您所看到的,我正在使用SPEL #{#entityName} 在运行时传递实体名称以在@Query注释中进行查询。 当我尝试运行应用程序时,它给了我以下异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'demo-db.common_bean' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_67]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.7.0_67]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.7.0_67]
at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:1.7.0_67]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:389) ~[mysql-connector-java-5.1.35.jar:5.1.35]

被解雇的查询如下:

Hibernate: select customeren0_.id as id2_0_, customeren0_.code as code3_0_,   customeren0_.created_by as created_4_0_, customeren0_.created_oy as created_5_0_, customeren0_.delete_flag as delete_f6_0_, customeren0_.modified_by as modified7_0_, customeren0_.modified_on as modified8_0_, customeren0_.address1 as address9_0_, customeren0_.address2 as address10_0_, customeren0_.city as city11_0_, customeren0_.country as country12_0_, customeren0_.email_id as email_i13_0_, customeren0_.fax_number as fax_num14_0_, customeren0_.first_name as first_n15_0_, customeren0_.landline_no as landlin16_0_, customeren0_.last_name as last_na17_0_, customeren0_.middle_name as middle_18_0_, customeren0_.mobile_no as mobile_19_0_, customeren0_.pin_code as pin_cod20_0_, customeren0_.state as state21_0_ 
 from **common_bean** customeren0_ where customeren0_.dtype='CustomerEntity' and     customeren0_.delete_flag='F'

而不是 common_bean in from子句,它应该是客户,因为我正在为实体客户进行操作。

请让我知道我做错了什么。

0 个答案:

没有答案