Spring com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:Table';demo db.common#u bean';不';不存在

Spring com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:Table';demo db.common#u bean';不';不存在,spring,hibernate,spring-boot,generics,spring-data-jpa,Spring,Hibernate,Spring Boot,Generics,Spring Data Jpa,我正在尝试用JPARepository创建Spring启动应用程序。我的目标是创建通用应用程序。 在我的应用程序中,我为所有实体提供了4项常见功能,如下所示: 盖特尔 getAllNewAfterLastSyncDate getAllModifiedAfterLastSyncDate getAllDeletedAfterLastSyncDate 为了实现这一点并避免代码冗余,我创建了一个通用的基本存储库,它扩展了JPARepository,如下所示: BaseRepository.java

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

  • 盖特尔

  • 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();
*/

}
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() + "]";
}



}
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>{

}

现在我想对客户实体

使用这个 CustomerEntity.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();
*/

}
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() + "]";
}



}
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>{

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

CustomerService.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();
*/

}
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() + "]";
}



}
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,然后转发给customerservice,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'
在from子句中,它应该是customer,而不是common\u bean,因为我正在为实体客户执行操作


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

问题在于客户属性-因此请发布此类代码。相关部分是在类级别应用的注释。@Ralph我已经发布了CustomerEntity。请检查并让我知道如何修复此问题,因为我发现您的客户属性没有用
@Entity
注释。因此它不是一个实体。@JB Nizet thx很多………@实体起作用了!!!。。。。。。。。。。这是一个愚蠢的错误。现在当我运行应用程序时,我得到表不存在错误,请检查更新的代码。请帮助我,因为我是第一次使用SPEL。不要将
@Entity
注释放在CommonBean上。把它放在客户机上。CommonBean不是一个实体。它应该用
@MappedSuperclass
注释。