Spring sql无效的列名

时间:2018-11-30 15:27:44

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

获取

  

com.microsoft.sqlserver.jdbc.SQLServerException:无效的列名   'partnerIdPartner'。

application.properties:

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

表创建:

CREATE TABLE [partnersystem] (
  [idPartnerSystem] INT IDENTITY(1,1)  ,
  [partner_idPartner] INT NOT NULL  DEFAULT NULL  ,
  [Name] NVARCHAR(45) NULL  DEFAULT NULL  ,
  [Domain] NVARCHAR(45) NULL  DEFAULT NULL  ,
  [Code] NVARCHAR(45) NULL  DEFAULT NULL  ,
  [PartnerSystem_idSystem] INT NOT NULL  DEFAULT NULL  ,
  [UpdateUser] NVARCHAR(45) NULL  DEFAULT NULL  ,
  [UpdateDT] DATETIME NULL  DEFAULT NULL  ,
 CONSTRAINT [partnersystem_PRIMARY]  PRIMARY KEY  CLUSTERED  ([idPartnerSystem]), CONSTRAINT [partnersystem_fk_PartnerSystem_partner] FOREIGN KEY ("partner_idPartner") REFERENCES "partner" ( "idPartner" ) ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT [partnersystem_fk_PartnerSystem_System] FOREIGN KEY ("PartnerSystem_idSystem") REFERENCES "system" ( "idSystem" ) ON UPDATE NO ACTION ON DELETE NO ACTION);
 CREATE  INDEX [partnersystem_fk_PartnerSystem_partner] ON [partnersystem] ([partner_idPartner]);
 CREATE  INDEX [partnersystem_fk_PartnerSystem_System] ON [partnersystem] ([PartnerSystem_idSystem]);

JPA实体:

@Entity
@Table(name = "partnersystem")
public class PartnerSystem {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idPartnerSystem")
    private int idPartnerSystem;


    @Column(name = "partner_idPartner" )
    private int partnerIdPartner;

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

    @Column(name = "Domain" )
    private String domain;

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

    @Column(name = "PartnerSystem_idSystem" )
    private int partnerSystemIdSystem;

    @Column(name = "UpdateUser" )
    private String updateUser;

我的存储库:

 @Repository
    public interface PartnerSystemRepository extends JpaRepository<PartnerSystem, 
  Integer>{

        public PartnerSystem findByPartnerIdPartner(int partnerIdPartner);
    }

执行简单查询会引发错误。

public List<Object[]> findAllPartnerSystem(int id) {

    String test = 
            "SELECT idPartnerSystem, partnerIdPartner, name, domain, code, partnerSystemId" + 
            " FROM PartnerSystem  " +
            "WHERE partnerIdPartner = ?"
            ;
    Query query = em.createNativeQuery(test);

    query.setParameter(1, id);
    List<Object[]> results = query.getResultList();

    for (Object[] row : results) {

    }
    return results;
}

1 个答案:

答案 0 :(得分:1)

在本机查询中,您必须使用列名而不是属性名:

"SELECT idPartnerSystem, partner_idPartner, name, domain, code, PartnerSystem_idSystem" + 
            " FROM partnersystem " +
            "WHERE partner_idPartner= 

但是我建议使用JPQL查询而不是本机查询。