簡體   English   中英

本機查詢可以使用sqlDeveloper,但無法使用休眠模式

[英]Native query works sqlDeveloper but fails using hibernate

我有一個查詢,我想使用休眠的本機sqlQuery運行它

當我使用sql Developer運行查詢時,它工作正常,但是當hibernate運行它時,它將拋出此異常

java.sql.SQLException: Invalid column name
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) ~[ojdbc14-10.2.0.4.0.jar:Oracle JDBC Driver version - "10.2.0.4.0"]
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146) ~[ojdbc14-10.2.0.4.0.jar:Oracle JDBC Driver version - "10.2.0.4.0"]
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208) ~[ojdbc14-10.2.0.4.0.jar:Oracle JDBC Driver version - "10.2.0.4.0"]
    at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3319) ~[ojdbc14-10.2.0.4.0.jar:Oracle JDBC Driver version - "10.2.0.4.0"]
    at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:1926) ~[ojdbc14-10.2.0.4.0.jar:Oracle JDBC Driver version - "10.2.0.4.0"]
    at oracle.jdbc.driver.OracleResultSet.getLong(OracleResultSet.java:1575) ~[ojdbc14-10.2.0.4.0.jar:Oracle JDBC Driver version - "10.2.0.4.0"]
    at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$2.doExtract(BigIntTypeDescriptor.java:63) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:238) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:234) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:224) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:300) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:789) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:714) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.processResultSet(Loader.java:972) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.doQuery(Loader.java:930) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]

這是來自休眠日志的查詢:

Hibernate: select o.GUICHET,count(*) from OPERATIONS o, Guichet g, Centre c where DATE_OPERATIONS between trunc(sysdate,'mm') and add_months(trunc(sysdate,'mm'),1) and c.centre_id=? and c.centre_id=g.centre_id and g.GUICHET_ID=o.GUICHET group by o.GUICHET

這是我的表類定義:

@Entity
@Table(name = "operations")
public class Operations implements Serializable  {

    private static final long serialVersionUID = 1L;

    @GeneratedValue(strategy= GenerationType.AUTO)
    @Id
    private Long operationsId;
    private Date dateOperations;
    @ManyToOne()
    @JoinColumn(name = "guichet", referencedColumnName = "guichetId")
    private Guichet guichet;

編輯:這是我在其中定義和使用此方法的代碼

@Repository
public interface OperationsRepository extends CrudRepository<Operations, Long> {

    @Query(value="select o.guichet,count(*) from OPERATIONS o, Guichet g, Centre c where DATE_OPERATIONS between trunc(sysdate,'mm') and add_months(trunc(sysdate,'mm'),1) " + 
            "and c.centre_id=?1 and c.centre_id=g.centre_id and g.GUICHET_ID=o.GUICHET " + 
            "group by o.guichet",nativeQuery=true)
    Iterable<Operations> operationsStat( Long centreId);

使用此方法:

@Override
    public Iterable<Operations> operationsStat(Long centreId) {
        return operationsRepository.operationsStat(centreId);
    }


@GetMapping(value="/statistique")
    @ResponseBody()
    Iterable<Operations> doStatistique()
    {
        return operationsServiceImpl.operationsStat(new Long(selectedCentre)); 
    }

更改返回類型后的代碼:

@Repository
public interface OperationsRepository extends CrudRepository<Operations, Long> {

    @Query(value="select o.guichet,count(*) from OPERATIONS o, Guichet g, Centre c where DATE_OPERATIONS between trunc(sysdate,'mm') and add_months(trunc(sysdate,'mm'),1) " + 
            "and c.centre_id=?1 and c.centre_id=g.centre_id and g.GUICHET_ID=o.GUICHET " + 
            "group by o.guichet",nativeQuery=true)
    Iterable<Object> operationsStat( Long centreId);

然后這個:

@Override
    public Iterable<Object> operationsStat(Long centreId) {
        return operationsRepository.operationsStat(centreId);
    }


@GetMapping(value="/statistique")
    @ResponseBody()
    Iterable<Object> doStatistique()
    {
        return operationsServiceImpl.operationsStat(new Long(selectedCentre)); 
    }

Operations類中有一個字段

private Date dateOperations;

但是查詢不會為該字段返回任何值。 一樣

private Guichet guichet;

查詢中沒有列guichetId,並且您的count(*)在類中沒有映射的字段。

看來@Query(value="select o.guichet,count(*) from OPERATIONS o ..正在返回Guichet對象的列表,但您期望使用List<Operations>

一種選擇是為Operations靜態對象創建外部pojo(例如,使用包名稱:com.myorg.model)

package com.myorg.model;
 public class OperationsStas {
  private Guichet guichet;
  private Long   count;

  public OperationsStas(Guichet guichet, Long count) {
    this.guichet = guichet;
    this.count  = count;
  }

}

回購可能是這樣的:

@Repository
public interface OperationsRepository extends CrudRepository<Operations, Long> {

    @Query(value="select new com.myorg.model.com.myorg.model.OperationsStats(o.guichet,count(*)) from OPERATIONS o, Guichet g, Centre c where DATE_OPERATIONS between trunc(sysdate,'mm') and add_months(trunc(sysdate,'mm'),1) " + 
            "and c.centre_id=?1 and c.centre_id=g.centre_id and g.GUICHET_ID=o.GUICHET " + 
            "group by o.guichet",nativeQuery=true)
    List<OperationsStas> operationsStat( Long centreId);

而且控制也必須改變

@Override
    public List<OperationsStas> operationsStat(Long centreId) {
        return operationsRepository.operationsStat(centreId);
    }


@GetMapping(value="/statistique")
    @ResponseBody()
    List<OperationsStas> doStatistique()
    {
        return operationsServiceImpl.operationsStat(new Long(selectedCentre)); 
    }

希望它能對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM